Skip to content

Commit

Permalink
chore: Added example for container-image with ECR module (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonbabenko authored Apr 5, 2024
1 parent c42e758 commit 54b8256
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 30 deletions.
9 changes: 6 additions & 3 deletions examples/container-image/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AWS Lambda Function deployed from Docker Container Image example

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

## Usage

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

| Name | Source | Version |
|------|--------|---------|
| <a name="module_docker_image"></a> [docker\_image](#module\_docker\_image) | ../../modules/docker-build | n/a |
| <a name="module_lambda_function_from_container_image"></a> [lambda\_function\_from\_container\_image](#module\_lambda\_function\_from\_container\_image) | ../../ | n/a |
| <a name="module_docker_build"></a> [docker\_build](#module\_docker\_build) | ../../modules/docker-build | n/a |
| <a name="module_docker_build_from_ecr"></a> [docker\_build\_from\_ecr](#module\_docker\_build\_from\_ecr) | ../../modules/docker-build | n/a |
| <a name="module_ecr"></a> [ecr](#module\_ecr) | terraform-aws-modules/ecr/aws | n/a |
| <a name="module_lambda_function_with_docker_build"></a> [lambda\_function\_with\_docker\_build](#module\_lambda\_function\_with\_docker\_build) | ../../ | n/a |
| <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 |

## Resources

Expand Down
63 changes: 58 additions & 5 deletions examples/container-image/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ provider "docker" {
}
}

module "lambda_function_from_container_image" {
module "lambda_function_with_docker_build" {
source = "../../"

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

create_package = false

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

image_uri = module.docker_image.image_uri
image_uri = module.docker_build.image_uri
}

module "docker_image" {
module "lambda_function_with_docker_build_from_ecr" {
source = "../../"

function_name = "${random_pet.this.id}-lambda-with-docker-build-from-ecr"
description = "My awesome lambda function with container image by modules/docker-build and ECR repository created by terraform-aws-ecr module"

create_package = false

##################
# Container Image
##################
package_type = "Image"
architectures = ["arm64"] # ["x86_64"]

image_uri = module.docker_build_from_ecr.image_uri
}

module "docker_build" {
source = "../../modules/docker-build"

create_ecr_repo = true
Expand Down Expand Up @@ -87,6 +104,42 @@ module "docker_image" {
}
}

############################################
# Docker Image and ECR by terraform-aws-ecr
############################################

module "docker_build_from_ecr" {
source = "../../modules/docker-build"

ecr_repo = module.ecr.repository_name

use_image_tag = false # If false, sha of the image will be used

# use_image_tag = true
# image_tag = "2.0"

source_path = local.source_path
platform = "linux/amd64"
build_args = {
FOO = "bar"
}

triggers = {
dir_sha = local.dir_sha
}
}

module "ecr" {
source = "terraform-aws-modules/ecr/aws"

repository_name = "${random_pet.this.id}-ecr"
repository_force_delete = true

create_lifecycle_policy = false

repository_lambda_read_access_arns = [module.lambda_function_with_docker_build_from_ecr.lambda_function_arn]
}

resource "random_pet" "this" {
length = 2
}
42 changes: 21 additions & 21 deletions examples/container-image/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,106 +1,106 @@
# Lambda Function
output "lambda_function_arn" {
description = "The ARN of the Lambda Function"
value = module.lambda_function_from_container_image.lambda_function_arn
value = module.lambda_function_with_docker_build.lambda_function_arn
}

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

output "lambda_function_invoke_arn" {
description = "The Invoke ARN of the Lambda Function"
value = module.lambda_function_from_container_image.lambda_function_invoke_arn
value = module.lambda_function_with_docker_build.lambda_function_invoke_arn
}

output "lambda_function_name" {
description = "The name of the Lambda Function"
value = module.lambda_function_from_container_image.lambda_function_name
value = module.lambda_function_with_docker_build.lambda_function_name
}

output "lambda_function_qualified_arn" {
description = "The ARN identifying your Lambda Function Version"
value = module.lambda_function_from_container_image.lambda_function_qualified_arn
value = module.lambda_function_with_docker_build.lambda_function_qualified_arn
}

output "lambda_function_version" {
description = "Latest published version of Lambda Function"
value = module.lambda_function_from_container_image.lambda_function_version
value = module.lambda_function_with_docker_build.lambda_function_version
}

output "lambda_function_last_modified" {
description = "The date Lambda Function resource was last modified"
value = module.lambda_function_from_container_image.lambda_function_last_modified
value = module.lambda_function_with_docker_build.lambda_function_last_modified
}

output "lambda_function_kms_key_arn" {
description = "The ARN for the KMS encryption key of Lambda Function"
value = module.lambda_function_from_container_image.lambda_function_kms_key_arn
value = module.lambda_function_with_docker_build.lambda_function_kms_key_arn
}

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

output "lambda_function_source_code_size" {
description = "The size in bytes of the function .zip file"
value = module.lambda_function_from_container_image.lambda_function_source_code_size
value = module.lambda_function_with_docker_build.lambda_function_source_code_size
}

# Lambda Layer
output "lambda_layer_arn" {
description = "The ARN of the Lambda Layer with version"
value = module.lambda_function_from_container_image.lambda_layer_arn
value = module.lambda_function_with_docker_build.lambda_layer_arn
}

output "lambda_layer_layer_arn" {
description = "The ARN of the Lambda Layer without version"
value = module.lambda_function_from_container_image.lambda_layer_layer_arn
value = module.lambda_function_with_docker_build.lambda_layer_layer_arn
}

output "lambda_layer_created_date" {
description = "The date Lambda Layer resource was created"
value = module.lambda_function_from_container_image.lambda_layer_created_date
value = module.lambda_function_with_docker_build.lambda_layer_created_date
}

output "lambda_layer_source_code_size" {
description = "The size in bytes of the Lambda Layer .zip file"
value = module.lambda_function_from_container_image.lambda_layer_source_code_size
value = module.lambda_function_with_docker_build.lambda_layer_source_code_size
}

output "lambda_layer_version" {
description = "The Lambda Layer version"
value = module.lambda_function_from_container_image.lambda_layer_version
value = module.lambda_function_with_docker_build.lambda_layer_version
}

# IAM Role
output "lambda_role_arn" {
description = "The ARN of the IAM role created for the Lambda Function"
value = module.lambda_function_from_container_image.lambda_role_arn
value = module.lambda_function_with_docker_build.lambda_role_arn
}

output "lambda_role_name" {
description = "The name of the IAM role created for the Lambda Function"
value = module.lambda_function_from_container_image.lambda_role_name
value = module.lambda_function_with_docker_build.lambda_role_name
}

# CloudWatch Log Group
output "lambda_cloudwatch_log_group_arn" {
description = "The ARN of the Cloudwatch Log Group"
value = module.lambda_function_from_container_image.lambda_cloudwatch_log_group_arn
value = module.lambda_function_with_docker_build.lambda_cloudwatch_log_group_arn
}

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

output "docker_image_id" {
description = "The ID of the Docker image"
value = module.docker_image.image_id
value = module.docker_build.image_id
}

output "docker_image_files_to_hash" {
Expand Down
4 changes: 3 additions & 1 deletion modules/docker-build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

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.

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.

## Usage
Expand Down Expand Up @@ -47,7 +49,7 @@ module "docker_image" {

## Examples

* [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.
* [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.


<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Expand Down

0 comments on commit 54b8256

Please sign in to comment.