Skip to content

Commit 4a0b616

Browse files
authored
feat: Add submodule to handle creation of docker images (#162)
1 parent f3c2005 commit 4a0b616

File tree

9 files changed

+187
-47
lines changed

9 files changed

+187
-47
lines changed

examples/container-image/README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,26 @@ Note that this example may create resources which cost money. Run `terraform des
2121
|------|---------|
2222
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
2323
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.19 |
24-
| <a name="requirement_docker"></a> [docker](#requirement\_docker) | >= 2.8.0 |
2524
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2 |
2625

2726
## Providers
2827

2928
| Name | Version |
3029
|------|---------|
31-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.19 |
32-
| <a name="provider_docker"></a> [docker](#provider\_docker) | >= 2.8.0 |
3330
| <a name="provider_random"></a> [random](#provider\_random) | >= 2 |
3431

3532
## Modules
3633

3734
| Name | Source | Version |
3835
|------|--------|---------|
36+
| <a name="module_docker_image"></a> [docker\_image](#module\_docker\_image) | ../../modules/docker-build | |
3937
| <a name="module_lambda_function_from_container_image"></a> [lambda\_function\_from\_container\_image](#module\_lambda\_function\_from\_container\_image) | ../../ | |
4038

4139
## Resources
4240

4341
| Name | Type |
4442
|------|------|
45-
| [aws_ecr_repository.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository) | resource |
46-
| [docker_registry_image.app](https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/registry_image) | resource |
4743
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
48-
| [aws_caller_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
49-
| [aws_ecr_authorization_token.token](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ecr_authorization_token) | data source |
50-
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
5144

5245
## Inputs
5346

@@ -57,6 +50,7 @@ No inputs.
5750

5851
| Name | Description |
5952
|------|-------------|
53+
| <a name="output_docker_image_uri"></a> [docker\_image\_uri](#output\_docker\_image\_uri) | The ECR Docker image URI used to deploy Lambda Function |
6054
| <a name="output_lambda_cloudwatch_log_group_arn"></a> [lambda\_cloudwatch\_log\_group\_arn](#output\_lambda\_cloudwatch\_log\_group\_arn) | The ARN of the Cloudwatch Log Group |
6155
| <a name="output_lambda_function_arn"></a> [lambda\_function\_arn](#output\_lambda\_function\_arn) | The ARN of the Lambda Function |
6256
| <a name="output_lambda_function_invoke_arn"></a> [lambda\_function\_invoke\_arn](#output\_lambda\_function\_invoke\_arn) | The Invoke ARN of the Lambda Function |

examples/container-image/main.tf

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,15 @@ module "lambda_function_from_container_image" {
2424
##################
2525
# Container Image
2626
##################
27-
image_uri = docker_registry_image.app.name
27+
image_uri = module.docker_image.image_uri
2828
package_type = "Image"
2929
}
3030

31-
#################
32-
# ECR Repository
33-
#################
34-
resource "aws_ecr_repository" "this" {
35-
name = random_pet.this.id
36-
}
37-
38-
###############################################
39-
# Create Docker Image and push to ECR registry
40-
###############################################
41-
42-
data "aws_caller_identity" "this" {}
43-
data "aws_region" "current" {}
44-
data "aws_ecr_authorization_token" "token" {}
45-
46-
locals {
47-
ecr_address = format("%v.dkr.ecr.%v.amazonaws.com", data.aws_caller_identity.this.account_id, data.aws_region.current.name)
48-
ecr_image = format("%v/%v:%v", local.ecr_address, aws_ecr_repository.this.id, "1.0")
49-
}
50-
51-
provider "docker" {
52-
registry_auth {
53-
address = local.ecr_address
54-
username = data.aws_ecr_authorization_token.token.user_name
55-
password = data.aws_ecr_authorization_token.token.password
56-
}
57-
}
58-
59-
resource "docker_registry_image" "app" {
60-
name = local.ecr_image
31+
module "docker_image" {
32+
source = "../../modules/docker-build"
6133

62-
build {
63-
context = "context"
64-
}
34+
create_ecr_repo = true
35+
ecr_repo = random_pet.this.id
36+
image_tag = "1.0"
37+
source_path = "context"
6538
}

examples/container-image/outputs.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,9 @@ output "lambda_cloudwatch_log_group_arn" {
8686
description = "The ARN of the Cloudwatch Log Group"
8787
value = module.lambda_function_from_container_image.lambda_cloudwatch_log_group_arn
8888
}
89+
90+
# Docker Image
91+
output "docker_image_uri" {
92+
description = "The ECR Docker image URI used to deploy Lambda Function"
93+
value = module.docker_image.image_uri
94+
}

examples/container-image/versions.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,5 @@ terraform {
44
required_providers {
55
aws = ">= 3.19"
66
random = ">= 2"
7-
8-
docker = {
9-
source = "kreuzwerker/docker"
10-
version = ">= 2.8.0"
11-
}
127
}
138
}

modules/docker-build/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Build Docker Image module
2+
3+
Terraform module that builds Docker image from `Dockerfile` and pushes it to ECR repository. Lambda can deploy container images from private ECR.
4+
5+
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.
6+
7+
## Usage
8+
9+
### Complete example of Lambda Function deployment via AWS CodeDeploy
10+
11+
```hcl
12+
module "lambda_function" {
13+
source = "terraform-aws-modules/lambda/aws"
14+
15+
function_name = "my-lambda1"
16+
create_package = false
17+
18+
image_uri = module.docker_image.image_uri
19+
package_type = "Image"
20+
}
21+
22+
module "docker_image" {
23+
source = "terraform-aws-modules/lambda/aws//modules/docker-build"
24+
25+
create_ecr_repo = true
26+
ecr_repo = "my-cool-ecr-repo"
27+
image_tag = "1.0"
28+
source_path = "context"
29+
}
30+
```
31+
32+
## Examples
33+
34+
* [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.
35+
36+
37+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
38+
## Requirements
39+
40+
| Name | Version |
41+
|------|---------|
42+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
43+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.35 |
44+
| <a name="requirement_docker"></a> [docker](#requirement\_docker) | >= 2.8.0 |
45+
46+
## Providers
47+
48+
| Name | Version |
49+
|------|---------|
50+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.35 |
51+
| <a name="provider_docker"></a> [docker](#provider\_docker) | >= 2.8.0 |
52+
53+
## Modules
54+
55+
No modules.
56+
57+
## Resources
58+
59+
| Name | Type |
60+
|------|------|
61+
| [aws_ecr_repository.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository) | resource |
62+
| [docker_registry_image.this](https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/registry_image) | resource |
63+
| [aws_caller_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
64+
| [aws_ecr_authorization_token.token](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ecr_authorization_token) | data source |
65+
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
66+
67+
## Inputs
68+
69+
| Name | Description | Type | Default | Required |
70+
|------|-------------|------|---------|:--------:|
71+
| <a name="input_create_ecr_repo"></a> [create\_ecr\_repo](#input\_create\_ecr\_repo) | Controls whether ECR repository for Lambda image should be created | `bool` | `false` | no |
72+
| <a name="input_docker_file_path"></a> [docker\_file\_path](#input\_docker\_file\_path) | Path to Dockerfile in source package | `string` | `"Dockerfile"` | no |
73+
| <a name="input_ecr_repo"></a> [ecr\_repo](#input\_ecr\_repo) | Name of ECR repository to use or to create | `string` | `null` | no |
74+
| <a name="input_image_tag"></a> [image\_tag](#input\_image\_tag) | Image tag to use. If not specified current timestamp in format 'YYYYMMDDhhmmss' will be used. This can lead to unnecessary rebuilds. | `string` | `null` | no |
75+
| <a name="input_source_path"></a> [source\_path](#input\_source\_path) | Path to folder containing application code | `string` | `null` | no |
76+
77+
## Outputs
78+
79+
| Name | Description |
80+
|------|-------------|
81+
| <a name="output_image_uri"></a> [image\_uri](#output\_image\_uri) | The ECR image URI for deploying lambda |
82+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
83+
84+
## Authors
85+
86+
Module managed by [Anton Babenko](https://github.com/antonbabenko). Check out [serverless.tf](https://serverless.tf) to learn more about doing serverless with Terraform.
87+
88+
Please reach out to [Betajob](https://www.betajob.com/) if you are looking for commercial support for your Terraform, AWS, or serverless project.
89+
90+
91+
## License
92+
93+
Apache 2 Licensed. See LICENSE for full details.

modules/docker-build/main.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
data "aws_region" "current" {}
2+
3+
data "aws_caller_identity" "this" {}
4+
5+
data "aws_ecr_authorization_token" "token" {}
6+
7+
locals {
8+
ecr_address = format("%v.dkr.ecr.%v.amazonaws.com", data.aws_caller_identity.this.account_id, data.aws_region.current.name)
9+
ecr_repo = var.create_ecr_repo ? aws_ecr_repository.this[0].id : var.ecr_repo
10+
image_tag = coalesce(var.image_tag, formatdate("YYYYMMDDhhmmss", timestamp()))
11+
ecr_image_name = format("%v/%v:%v", local.ecr_address, local.ecr_repo, local.image_tag)
12+
}
13+
14+
provider "docker" {
15+
registry_auth {
16+
address = local.ecr_address
17+
username = data.aws_ecr_authorization_token.token.user_name
18+
password = data.aws_ecr_authorization_token.token.password
19+
}
20+
}
21+
22+
resource "docker_registry_image" "this" {
23+
name = local.ecr_image_name
24+
25+
build {
26+
context = var.source_path
27+
dockerfile = var.docker_file_path
28+
}
29+
}
30+
31+
resource "aws_ecr_repository" "this" {
32+
count = var.create_ecr_repo ? 1 : 0
33+
34+
name = var.ecr_repo
35+
}

modules/docker-build/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "image_uri" {
2+
description = "The ECR image URI for deploying lambda"
3+
value = docker_registry_image.this.name
4+
}

modules/docker-build/variables.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
variable "create_ecr_repo" {
2+
description = "Controls whether ECR repository for Lambda image should be created"
3+
type = bool
4+
default = false
5+
}
6+
7+
variable "ecr_repo" {
8+
description = "Name of ECR repository to use or to create"
9+
type = string
10+
default = null
11+
}
12+
13+
variable "image_tag" {
14+
description = "Image tag to use. If not specified current timestamp in format 'YYYYMMDDhhmmss' will be used. This can lead to unnecessary rebuilds."
15+
type = string
16+
default = null
17+
}
18+
19+
variable "source_path" {
20+
description = "Path to folder containing application code"
21+
type = string
22+
default = null
23+
}
24+
25+
variable "docker_file_path" {
26+
description = "Path to Dockerfile in source package"
27+
type = string
28+
default = "Dockerfile"
29+
}

modules/docker-build/versions.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
terraform {
2+
required_version = ">= 0.13"
3+
4+
required_providers {
5+
aws = ">= 3.35"
6+
docker = {
7+
source = "kreuzwerker/docker"
8+
version = ">= 2.8.0"
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)