Skip to content

Commit db2b43a

Browse files
aahelsanon-dev
andauthored
Ajsanon/ux updates (#80)
* Lambda Reg UX changes to simplify for users * - added pull through cache - removed all provisioners and used providers * bumped lambda_registrator_image tag * fixed terraform fmt * moved providers to examples/lambda providers.tf * fixed provider and other minor fixes * added pull_through var in examples * fixed terraform lint * fix minor fmt issues * review fixes * minor name change * fix tf lint * variable name changes * added changelog --------- Co-authored-by: AJ Sanon <[email protected]>
1 parent 953a1ff commit db2b43a

File tree

7 files changed

+145
-64
lines changed

7 files changed

+145
-64
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ FEATURES
44
* Add support for storing parameter values greater than 4 KB. The `lambda-registrator` module and source code have been updated to accept a configurable value for the [SSM parameter tier](https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-store-advanced-parameters.html). This allows users to choose if they want to use the `Advanced` tier feature. Charges apply for the `Advanved` tier so if the tier is not expressly set to `Advanced`, then the `Standard` tier will be used. Using the `Advanced` tier allows for parameter values up to 8 KB. The Lambda-registrator Terraform module can be configured using the new `consul_extension_data_tier` variable.
55
[[GH-78]](https://github.com/hashicorp/terraform-aws-consul-lambda/pull/78)
66

7+
* Add support for pushing `consul-lambda-registrator` public image to private ecr repo through terraform.
8+
[[GH-80]](https://github.com/hashicorp/terraform-aws-consul-lambda/pull/80)
9+
710
## 0.1.0-beta4 (Apr 28, 2023)
811

912
IMPROVEMENTS

examples/lambda/README.md

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -51,57 +51,16 @@ cd terraform-aws-consul-lambda/examples/lambda
5151
git checkout v${VERSION}
5252
```
5353

54-
## Set your AWS account ID and region
54+
## Set your AWS region
5555

56-
Subsequent steps require knowledge of your AWS account ID and the AWS region that you want to deploy the example resources to.
56+
Subsequent steps require knowledge of the AWS region that you want to deploy the example resources to.
5757
Export these values to environment variables using the commands below.
58-
Replace `<account_id>` and `<region>` with your AWS account ID and region, respectively.
58+
Replace `<region>` with your AWS region.
5959

6060
```shell
61-
export AWS_ACCOUNT_ID=<account_id>
6261
export AWS_REGION=<region>
6362
```
6463

65-
## Publish `consul-lambda-registrator`
66-
67-
In this section you will pull the `consul-lambda-registrator` image from the AWS Public ECR Gallery and publish it to a private ECR repository using `docker`. This is required because AWS Lambda functions must use images from a private ECR repository. They are not able to use images from the Public ECR Gallery.
68-
69-
### Pull `consul-lambda-registrator`
70-
71-
Use the following command to pull the `consul-lambda-registrator` from the AWS Public ECR to your local machine.
72-
73-
```shell
74-
docker pull public.ecr.aws/hashicorp/consul-lambda-registrator:${VERSION}
75-
```
76-
77-
### Log in to AWS ECR
78-
79-
```shell
80-
aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
81-
```
82-
83-
### Create a private ECR repository
84-
85-
Use the following command to create a private ECR repository for `consul-lambda-registrator`.
86-
87-
```shell
88-
aws ecr create-repository \
89-
--repository-name consul-lambda-registrator \
90-
--image-scanning-configuration scanOnPush=true \
91-
--region ${AWS_REGION}
92-
```
93-
94-
### Push `consul-lambda-registrator`
95-
96-
Use the following commands to push the `consul-lambda-registrator` image to the private ECR repository you created in the previous step.
97-
98-
```shell
99-
docker tag \
100-
public.ecr.aws/hashicorp/consul-lambda-registrator:${VERSION} \
101-
${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/consul-lambda-registrator:${VERSION}
102-
103-
docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/consul-lambda-registrator:${VERSION}
104-
```
10564

10665
## Download the `consul-lambda-extension`
10766

@@ -110,7 +69,7 @@ This example Terraform workspace will use the zip package to deploy the `consul-
11069
add it to the `lambda-app-2` function so that it can call services within the Consul service mesh.
11170

11271
```shell
113-
curl -o consul-lambda-extension.zip https://releases.hashicorp.com/consul-lambda-extension/${VERSION}/consul-lambda-extension_${VERSION}_linux_amd64.zip
72+
curl -o consul-lambda-extension.zip "https://releases.hashicorp.com/consul-lambda-extension/${VERSION}/consul-lambda-extension_${VERSION}-beta4_linux_amd64.zip"
11473
```
11574

11675
## Build the example Lambda function
@@ -139,7 +98,6 @@ terraform init
13998
terraform apply \
14099
-var "name=${USER}" \
141100
-var "region=${AWS_REGION}" \
142-
-var "ecr_image_uri=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/consul-lambda-registrator:${VERSION}" \
143101
-var "ingress_cidrs=[\"${MY_IP}\"]"
144102
```
145103

@@ -261,7 +219,6 @@ Use the following command to clean up the resources managed by Terraform.
261219
terraform destroy \
262220
-var "name=${USER}" \
263221
-var "region=${AWS_REGION}" \
264-
-var "ecr_image_uri=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/consul-lambda-registrator:${VERSION}" \
265222
-var "ingress_cidrs=[\"${MY_IP}\"]"
266223
```
267224

examples/lambda/lambda/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ variable "invocation_mode" {
9494
default = "SYNCHRONOUS"
9595
validation {
9696
condition = contains(["SYNCHRONOUS", "ASYNCHRONOUS"], var.invocation_mode)
97-
error_message = "invocation_mode must be one of SYNCHRONOUS or ASYNCHRONOUS"
97+
error_message = "Variable invocation_mode must be one of SYNCHRONOUS or ASYNCHRONOUS."
9898
}
9999
}
100100

examples/lambda/registrator.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
module "consul_lambda_registrator" {
55
source = "../../modules/lambda-registrator"
66
name = "${var.name}-lambda-registrator"
7-
ecr_image_uri = var.ecr_image_uri
87
consul_http_addr = "http://${module.dev_consul_server.server_dns}:8500"
98
consul_extension_data_prefix = "/${var.name}"
109
subnet_ids = module.vpc.private_subnets
1110
security_group_ids = [module.vpc.default_security_group_id]
1211
sync_frequency_in_minutes = 1
12+
enable_pull_through_cache = var.enable_pull_through_cache
13+
region = var.region
1314
}

examples/lambda/variables.tf

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ variable "name" {
66
type = string
77
}
88

9-
variable "ecr_image_uri" {
10-
description = "The private ECR image URI for consul-lambda-registrator."
9+
variable "lambda_registrator_image" {
10+
description = "The Consul Lambda Registrator image for consul-lambda-registrator."
1111
type = string
12+
default = "public.ecr.aws/hashicorp/consul-lambda-registrator:0.1.0-beta4"
1213
}
1314

1415
variable "region" {
@@ -38,3 +39,9 @@ variable "consul_lambda_extension_arn" {
3839
type = string
3940
default = ""
4041
}
42+
43+
variable "enable_pull_through_cache" {
44+
description = "Flag to determine if a pull-through cache method will be used to obtain the appropriate ECR image"
45+
type = bool
46+
default = false
47+
}

modules/lambda-registrator/main.tf

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
11
# Copyright (c) HashiCorp, Inc.
22
# SPDX-License-Identifier: MPL-2.0
33

4+
terraform {
5+
required_providers {
6+
docker = {
7+
source = "kreuzwerker/docker"
8+
version = "3.0.2"
9+
}
10+
}
11+
}
412
locals {
513
on_vpc = length(var.subnet_ids) > 0 && length(var.security_group_ids) > 0
614
vpc_config = local.on_vpc ? [{
715
subnet_ids = var.subnet_ids
816
security_group_ids = var.security_group_ids
917
}] : []
10-
cron_key = "${var.name}-cron"
11-
lambda_events_key = "${var.name}-lambda_events"
18+
cron_key = "${var.name}-cron"
19+
lambda_events_key = "${var.name}-lambda_events"
20+
image_parts = split(":", var.consul_lambda_registrator_image)
21+
image_tag = local.image_parts[1]
22+
image_path_parts = split("/", local.image_parts[0])
23+
image_username = local.image_path_parts[1]
24+
image_name = local.image_path_parts[2]
25+
ecr_image_uri = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${var.region}.amazonaws.com/${var.private_ecr_repo_name}:${local.image_tag}"
26+
ecr_image_uri_pull_through = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${var.region}.amazonaws.com/${var.ecr_repository_prefix}/${local.image_username}/${local.image_name}:${local.image_tag}"
27+
}
28+
29+
# Equivalent of aws ecr get-login
30+
data "aws_ecr_authorization_token" "ecr_auth" {}
31+
32+
provider "docker" {
33+
host = var.docker_host
34+
registry_auth {
35+
username = data.aws_ecr_authorization_token.ecr_auth.user_name
36+
password = data.aws_ecr_authorization_token.ecr_auth.password
37+
address = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${var.region}.amazonaws.com"
38+
}
1239
}
1340

41+
data "aws_caller_identity" "current" {}
42+
1443
resource "aws_iam_role" "registration" {
1544
name = var.name
1645

@@ -127,8 +156,51 @@ resource "aws_iam_role_policy_attachment" "lambda_logs" {
127156
policy_arn = aws_iam_policy.policy.arn
128157
}
129158

159+
resource "aws_ecr_repository" "lambda-registrator" {
160+
count = var.enable_pull_through_cache ? 0 : 1
161+
name = var.private_ecr_repo_name
162+
force_delete = true
163+
}
164+
165+
166+
resource "aws_ecr_pull_through_cache_rule" "pull_through_cache_rule" {
167+
count = var.enable_pull_through_cache ? 1 : 0
168+
ecr_repository_prefix = var.ecr_repository_prefix
169+
upstream_registry_url = var.upstream_registry_url
170+
}
171+
172+
resource "docker_image" "lambda_registrator" {
173+
name = var.enable_pull_through_cache ? local.ecr_image_uri_pull_through : var.consul_lambda_registrator_image
174+
depends_on = [
175+
aws_ecr_pull_through_cache_rule.pull_through_cache_rule
176+
]
177+
}
178+
179+
resource "docker_tag" "lambda_registrator_tag" {
180+
count = var.enable_pull_through_cache ? 0 : 1
181+
source_image = docker_image.lambda_registrator.name
182+
target_image = local.ecr_image_uri
183+
}
184+
185+
resource "null_resource" "push_image" {
186+
count = var.enable_pull_through_cache ? 0 : 1
187+
188+
provisioner "local-exec" {
189+
command = "docker push ${local.ecr_image_uri}"
190+
}
191+
192+
depends_on = [
193+
docker_tag.lambda_registrator_tag
194+
]
195+
}
196+
resource "time_sleep" "wait_30_seconds" {
197+
count = var.enable_pull_through_cache ? 1 : 0
198+
depends_on = [docker_image.lambda_registrator]
199+
200+
create_duration = "30s"
201+
}
130202
resource "aws_lambda_function" "registration" {
131-
image_uri = var.ecr_image_uri
203+
image_uri = var.enable_pull_through_cache ? local.ecr_image_uri_pull_through : local.ecr_image_uri
132204
package_type = "Image"
133205
function_name = var.name
134206
role = aws_iam_role.registration.arn
@@ -168,6 +240,11 @@ resource "aws_lambda_function" "registration" {
168240
security_group_ids = vpc_config.value["security_group_ids"]
169241
}
170242
}
243+
depends_on = [
244+
null_resource.push_image,
245+
time_sleep.wait_30_seconds,
246+
]
247+
171248
}
172249

173250
module "eventbridge" {

modules/lambda-registrator/variables.tf

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,6 @@ variable "reserved_concurrent_executions" {
7575
default = -1
7676
}
7777

78-
variable "ecr_image_uri" {
79-
description = <<-EOT
80-
The ECR image URI for consul-lambda-registrator. The image must be in the
81-
same AWS region and in a private ECR repository. Due to these constraints,
82-
the public ECR images (https://gallery.ecr.aws/hashicorp/consul-lambda-registrator)
83-
cannot be used directly. We recommend either creating and using a new ECR
84-
repository or configuring pull through cache rules (https://docs.aws.amazon.com/AmazonECR/latest/userguide/pull-through-cache.html).
85-
EOT
86-
type = string
87-
}
8878

8979
variable "sync_frequency_in_minutes" {
9080
description = "The interval EventBridge is configured to trigger full synchronizations."
@@ -109,3 +99,49 @@ variable "tags" {
10999
type = map(string)
110100
default = {}
111101
}
102+
variable "region" {
103+
type = string
104+
description = "AWS region to deploy Lambda registrator."
105+
}
106+
107+
variable "private_ecr_repo_name" {
108+
description = "The name of the repository to republish the ECR image if one exists. If no name is passed, it is assumed that no repository exists and one needs to be created. Note :- If 'enable_pull_through_cache' is true this variable is ignored."
109+
type = string
110+
default = "consul-lambda-registrator"
111+
}
112+
113+
variable "enable_pull_through_cache" {
114+
description = "Flag to determine if a pull-through cache method will be used to obtain the appropriate ECR image"
115+
type = bool
116+
default = false
117+
}
118+
119+
120+
variable "consul_lambda_registrator_image" {
121+
description = "The Lambda registrator image to use. Must be provided as <registry/repository:tag>"
122+
type = string
123+
default = "public.ecr.aws/hashicorp/consul-lambda-registrator:0.1.0-beta4"
124+
125+
validation {
126+
condition = can(regex("^[a-zA-Z0-9_.-]+/[a-z0-9_.-]+/[a-z0-9_.-]+:[a-zA-Z0-9_.-]+$", var.consul_lambda_registrator_image))
127+
error_message = "Image format of 'consul_lambda_registrator_image' is invalid. It should be in the format 'registry/repository:tag'."
128+
}
129+
}
130+
131+
variable "docker_host" {
132+
description = "The docker socket for your system"
133+
type = string
134+
default = "unix:///var/run/docker.sock"
135+
}
136+
137+
variable "ecr_repository_prefix" {
138+
description = "The repository namespace to use when caching images from the source registry"
139+
type = string
140+
default = "ecr-public"
141+
}
142+
143+
variable "upstream_registry_url" {
144+
description = "The public registry url"
145+
type = string
146+
default = "public.ecr.aws"
147+
}

0 commit comments

Comments
 (0)