Skip to content

Commit 54b8256

Browse files
authored
chore: Added example for container-image with ECR module (#559)
1 parent c42e758 commit 54b8256

File tree

4 files changed

+88
-30
lines changed

4 files changed

+88
-30
lines changed

examples/container-image/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AWS Lambda Function deployed from Docker Container Image example
22

3-
Configuration in this directory creates AWS Lambda Function deployed with a Container Image.
3+
Configuration in this directory creates several AWS Lambda Functions deployed from Container Images (using `modules/docker-build` and `terraform-aws-modules/terraform-aws-ecr`).
44

55
## Usage
66

@@ -35,8 +35,11 @@ Note that this example may create resources which cost money. Run `terraform des
3535

3636
| Name | Source | Version |
3737
|------|--------|---------|
38-
| <a name="module_docker_image"></a> [docker\_image](#module\_docker\_image) | ../../modules/docker-build | n/a |
39-
| <a name="module_lambda_function_from_container_image"></a> [lambda\_function\_from\_container\_image](#module\_lambda\_function\_from\_container\_image) | ../../ | n/a |
38+
| <a name="module_docker_build"></a> [docker\_build](#module\_docker\_build) | ../../modules/docker-build | n/a |
39+
| <a name="module_docker_build_from_ecr"></a> [docker\_build\_from\_ecr](#module\_docker\_build\_from\_ecr) | ../../modules/docker-build | n/a |
40+
| <a name="module_ecr"></a> [ecr](#module\_ecr) | terraform-aws-modules/ecr/aws | n/a |
41+
| <a name="module_lambda_function_with_docker_build"></a> [lambda\_function\_with\_docker\_build](#module\_lambda\_function\_with\_docker\_build) | ../../ | n/a |
42+
| <a name="module_lambda_function_with_docker_build_from_ecr"></a> [lambda\_function\_with\_docker\_build\_from\_ecr](#module\_lambda\_function\_with\_docker\_build\_from\_ecr) | ../../ | n/a |
4043

4144
## Resources
4245

examples/container-image/main.tf

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ provider "docker" {
3232
}
3333
}
3434

35-
module "lambda_function_from_container_image" {
35+
module "lambda_function_with_docker_build" {
3636
source = "../../"
3737

38-
function_name = "${random_pet.this.id}-lambda-from-container-image"
39-
description = "My awesome lambda function from container image"
38+
function_name = "${random_pet.this.id}-lambda-with-docker-build"
39+
description = "My awesome lambda function with container image by modules/docker-build"
4040

4141
create_package = false
4242

@@ -46,10 +46,27 @@ module "lambda_function_from_container_image" {
4646
package_type = "Image"
4747
architectures = ["arm64"] # ["x86_64"]
4848

49-
image_uri = module.docker_image.image_uri
49+
image_uri = module.docker_build.image_uri
5050
}
5151

52-
module "docker_image" {
52+
module "lambda_function_with_docker_build_from_ecr" {
53+
source = "../../"
54+
55+
function_name = "${random_pet.this.id}-lambda-with-docker-build-from-ecr"
56+
description = "My awesome lambda function with container image by modules/docker-build and ECR repository created by terraform-aws-ecr module"
57+
58+
create_package = false
59+
60+
##################
61+
# Container Image
62+
##################
63+
package_type = "Image"
64+
architectures = ["arm64"] # ["x86_64"]
65+
66+
image_uri = module.docker_build_from_ecr.image_uri
67+
}
68+
69+
module "docker_build" {
5370
source = "../../modules/docker-build"
5471

5572
create_ecr_repo = true
@@ -87,6 +104,42 @@ module "docker_image" {
87104
}
88105
}
89106

107+
############################################
108+
# Docker Image and ECR by terraform-aws-ecr
109+
############################################
110+
111+
module "docker_build_from_ecr" {
112+
source = "../../modules/docker-build"
113+
114+
ecr_repo = module.ecr.repository_name
115+
116+
use_image_tag = false # If false, sha of the image will be used
117+
118+
# use_image_tag = true
119+
# image_tag = "2.0"
120+
121+
source_path = local.source_path
122+
platform = "linux/amd64"
123+
build_args = {
124+
FOO = "bar"
125+
}
126+
127+
triggers = {
128+
dir_sha = local.dir_sha
129+
}
130+
}
131+
132+
module "ecr" {
133+
source = "terraform-aws-modules/ecr/aws"
134+
135+
repository_name = "${random_pet.this.id}-ecr"
136+
repository_force_delete = true
137+
138+
create_lifecycle_policy = false
139+
140+
repository_lambda_read_access_arns = [module.lambda_function_with_docker_build_from_ecr.lambda_function_arn]
141+
}
142+
90143
resource "random_pet" "this" {
91144
length = 2
92145
}

examples/container-image/outputs.tf

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,106 @@
11
# Lambda Function
22
output "lambda_function_arn" {
33
description = "The ARN of the Lambda Function"
4-
value = module.lambda_function_from_container_image.lambda_function_arn
4+
value = module.lambda_function_with_docker_build.lambda_function_arn
55
}
66

77
output "lambda_function_arn_static" {
88
description = "The static ARN of the Lambda Function. Use this to avoid cycle errors between resources (e.g., Step Functions)"
9-
value = module.lambda_function_from_container_image.lambda_function_arn_static
9+
value = module.lambda_function_with_docker_build.lambda_function_arn_static
1010
}
1111

1212
output "lambda_function_invoke_arn" {
1313
description = "The Invoke ARN of the Lambda Function"
14-
value = module.lambda_function_from_container_image.lambda_function_invoke_arn
14+
value = module.lambda_function_with_docker_build.lambda_function_invoke_arn
1515
}
1616

1717
output "lambda_function_name" {
1818
description = "The name of the Lambda Function"
19-
value = module.lambda_function_from_container_image.lambda_function_name
19+
value = module.lambda_function_with_docker_build.lambda_function_name
2020
}
2121

2222
output "lambda_function_qualified_arn" {
2323
description = "The ARN identifying your Lambda Function Version"
24-
value = module.lambda_function_from_container_image.lambda_function_qualified_arn
24+
value = module.lambda_function_with_docker_build.lambda_function_qualified_arn
2525
}
2626

2727
output "lambda_function_version" {
2828
description = "Latest published version of Lambda Function"
29-
value = module.lambda_function_from_container_image.lambda_function_version
29+
value = module.lambda_function_with_docker_build.lambda_function_version
3030
}
3131

3232
output "lambda_function_last_modified" {
3333
description = "The date Lambda Function resource was last modified"
34-
value = module.lambda_function_from_container_image.lambda_function_last_modified
34+
value = module.lambda_function_with_docker_build.lambda_function_last_modified
3535
}
3636

3737
output "lambda_function_kms_key_arn" {
3838
description = "The ARN for the KMS encryption key of Lambda Function"
39-
value = module.lambda_function_from_container_image.lambda_function_kms_key_arn
39+
value = module.lambda_function_with_docker_build.lambda_function_kms_key_arn
4040
}
4141

4242
output "lambda_function_source_code_hash" {
4343
description = "Base64-encoded representation of raw SHA-256 sum of the zip file"
44-
value = module.lambda_function_from_container_image.lambda_function_source_code_hash
44+
value = module.lambda_function_with_docker_build.lambda_function_source_code_hash
4545
}
4646

4747
output "lambda_function_source_code_size" {
4848
description = "The size in bytes of the function .zip file"
49-
value = module.lambda_function_from_container_image.lambda_function_source_code_size
49+
value = module.lambda_function_with_docker_build.lambda_function_source_code_size
5050
}
5151

5252
# Lambda Layer
5353
output "lambda_layer_arn" {
5454
description = "The ARN of the Lambda Layer with version"
55-
value = module.lambda_function_from_container_image.lambda_layer_arn
55+
value = module.lambda_function_with_docker_build.lambda_layer_arn
5656
}
5757

5858
output "lambda_layer_layer_arn" {
5959
description = "The ARN of the Lambda Layer without version"
60-
value = module.lambda_function_from_container_image.lambda_layer_layer_arn
60+
value = module.lambda_function_with_docker_build.lambda_layer_layer_arn
6161
}
6262

6363
output "lambda_layer_created_date" {
6464
description = "The date Lambda Layer resource was created"
65-
value = module.lambda_function_from_container_image.lambda_layer_created_date
65+
value = module.lambda_function_with_docker_build.lambda_layer_created_date
6666
}
6767

6868
output "lambda_layer_source_code_size" {
6969
description = "The size in bytes of the Lambda Layer .zip file"
70-
value = module.lambda_function_from_container_image.lambda_layer_source_code_size
70+
value = module.lambda_function_with_docker_build.lambda_layer_source_code_size
7171
}
7272

7373
output "lambda_layer_version" {
7474
description = "The Lambda Layer version"
75-
value = module.lambda_function_from_container_image.lambda_layer_version
75+
value = module.lambda_function_with_docker_build.lambda_layer_version
7676
}
7777

7878
# IAM Role
7979
output "lambda_role_arn" {
8080
description = "The ARN of the IAM role created for the Lambda Function"
81-
value = module.lambda_function_from_container_image.lambda_role_arn
81+
value = module.lambda_function_with_docker_build.lambda_role_arn
8282
}
8383

8484
output "lambda_role_name" {
8585
description = "The name of the IAM role created for the Lambda Function"
86-
value = module.lambda_function_from_container_image.lambda_role_name
86+
value = module.lambda_function_with_docker_build.lambda_role_name
8787
}
8888

8989
# CloudWatch Log Group
9090
output "lambda_cloudwatch_log_group_arn" {
9191
description = "The ARN of the Cloudwatch Log Group"
92-
value = module.lambda_function_from_container_image.lambda_cloudwatch_log_group_arn
92+
value = module.lambda_function_with_docker_build.lambda_cloudwatch_log_group_arn
9393
}
9494

95-
# Docker Image
95+
# Docker Image by modules/docker-build
9696
output "docker_image_uri" {
9797
description = "The ECR Docker image URI used to deploy Lambda Function"
98-
value = module.docker_image.image_uri
98+
value = module.docker_build.image_uri
9999
}
100100

101101
output "docker_image_id" {
102102
description = "The ID of the Docker image"
103-
value = module.docker_image.image_id
103+
value = module.docker_build.image_id
104104
}
105105

106106
output "docker_image_files_to_hash" {

modules/docker-build/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Terraform module that builds Docker image from `Dockerfile` and pushes it to ECR repository. Lambda can deploy container images from private ECR.
44

5+
If you need to create ECR resources in flexible way, you should use [terraform-aws-ecr module](https://github.com/terraform-aws-modules/terraform-aws-ecr/). See `examples/container-image` for related examples.
6+
57
This Terraform module is the part of [serverless.tf framework](https://github.com/antonbabenko/serverless.tf), which aims to simplify all operations when working with the serverless in Terraform.
68

79
## Usage
@@ -47,7 +49,7 @@ module "docker_image" {
4749

4850
## Examples
4951

50-
* [Container Image](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/container-image) - Creates Docker Image and deploy Lambda Function using it.
52+
* [Container Image](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/container-image) - Creates Docker Image, ECR resository and deploys it Lambda Function.
5153

5254

5355
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

0 commit comments

Comments
 (0)