Skip to content

Commit

Permalink
fix: Removed docker provider block from docker-build submodule (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonbabenko authored May 23, 2022
1 parent 19b9f11 commit 151a09a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.69.0
rev: v1.71.0
hooks:
- id: terraform_fmt
- id: terraform_validate
Expand Down
7 changes: 6 additions & 1 deletion examples/container-image/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# AWS Lambda launched from Docker Container Image example
# AWS Lambda Function deployed from Docker Container Image example

Configuration in this directory creates AWS Lambda Function deployed with a Container Image.

Expand All @@ -21,12 +21,14 @@ Note that this example may create resources which cost money. Run `terraform des
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.19 |
| <a name="requirement_docker"></a> [docker](#requirement\_docker) | >= 2.12 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.19 |
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |

## Modules
Expand All @@ -41,6 +43,9 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Type |
|------|------|
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
| [aws_caller_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_ecr_authorization_token.token](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ecr_authorization_token) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |

## Inputs

Expand Down
55 changes: 34 additions & 21 deletions examples/container-image/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
data "aws_region" "current" {}

data "aws_caller_identity" "this" {}

data "aws_ecr_authorization_token" "token" {}

provider "aws" {
region = "eu-west-1"

Expand All @@ -9,8 +15,12 @@ provider "aws" {
skip_requesting_account_id = true
}

resource "random_pet" "this" {
length = 2
provider "docker" {
registry_auth {
address = format("%v.dkr.ecr.%v.amazonaws.com", data.aws_caller_identity.this.account_id, data.aws_region.current.name)
username = data.aws_ecr_authorization_token.token.user_name
password = data.aws_ecr_authorization_token.token.password
}
}

module "lambda_function_from_container_image" {
Expand All @@ -33,27 +43,30 @@ module "docker_image" {

create_ecr_repo = true
ecr_repo = random_pet.this.id
image_tag = "1.0"
source_path = "context"
ecr_repo_lifecycle_policy = jsonencode({
"rules" : [
{
"rulePriority" : 1,
"description" : "Keep only the last 2 images",
"selection" : {
"tagStatus" : "any",
"countType" : "imageCountMoreThan",
"countNumber" : 2
},
"action" : {
"type" : "expire"
}
}
]
})

image_tag = "2.0"
source_path = "context"
build_args = {
FOO = "bar"
}
ecr_repo_lifecycle_policy = <<EOF
{
"rules": [
{
"rulePriority": 1,
"description": "Keep only the last 2 images",
"selection": {
"tagStatus": "any",
"countType": "imageCountMoreThan",
"countNumber": 2
},
"action": {
"type": "expire"
}
}
]
}
EOF

resource "random_pet" "this" {
length = 2
}
4 changes: 4 additions & 0 deletions examples/container-image/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ terraform {
source = "hashicorp/aws"
version = ">= 3.19"
}
docker = {
source = "kreuzwerker/docker"
version = ">= 2.12"
}
random = {
source = "hashicorp/random"
version = ">= 2.0"
Expand Down
13 changes: 11 additions & 2 deletions modules/docker-build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@ This Terraform module is the part of [serverless.tf framework](https://github.co

## Usage

### Complete example of Lambda Function deployment via AWS CodeDeploy
### AWS Lambda Function deployed from Docker Container Image

```hcl
data "aws_ecr_authorization_token" "token" {}
provider "docker" {
registry_auth {
address = "835367859852.dkr.ecr.eu-west-1.amazonaws.com"
username = data.aws_ecr_authorization_token.token.user_name
password = data.aws_ecr_authorization_token.token.password
}
}
module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"
Expand Down Expand Up @@ -65,7 +75,6 @@ No modules.
| [aws_ecr_repository.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository) | resource |
| [docker_registry_image.this](https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/registry_image) | resource |
| [aws_caller_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_ecr_authorization_token.token](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ecr_authorization_token) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |

## Inputs
Expand Down
10 changes: 0 additions & 10 deletions modules/docker-build/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@ data "aws_region" "current" {}

data "aws_caller_identity" "this" {}

data "aws_ecr_authorization_token" "token" {}

locals {
ecr_address = coalesce(var.ecr_address, format("%v.dkr.ecr.%v.amazonaws.com", data.aws_caller_identity.this.account_id, data.aws_region.current.name))
ecr_repo = var.create_ecr_repo ? aws_ecr_repository.this[0].id : var.ecr_repo
image_tag = coalesce(var.image_tag, formatdate("YYYYMMDDhhmmss", timestamp()))
ecr_image_name = format("%v/%v:%v", local.ecr_address, local.ecr_repo, local.image_tag)
}

provider "docker" {
registry_auth {
address = local.ecr_address
username = data.aws_ecr_authorization_token.token.user_name
password = data.aws_ecr_authorization_token.token.password
}
}

resource "docker_registry_image" "this" {
name = local.ecr_image_name

Expand Down

0 comments on commit 151a09a

Please sign in to comment.