Skip to content

andreswebs/terraform-aws-ecr-image

Repository files navigation

terraform-aws-ecr-image

Creates an ECR repository and pushes a container image using local bash scripts.

Pre-requisites

The following programs must be available on the PATH to be able to run the local scripts:

  • bash
  • docker
  • aws
  • find
  • sort
  • md5sum

Usage

Example:

module "ecr_image" {
  source            = "github.com/andreswebs/terraform-aws-ecr-image"
  ecr_namespace     = "yourorg"
  image_suffix      = "yourimage"
  image_source_path = "./your-image-code"
}

Scripts

The push.bash script uses docker by default to build and push the container image.

It is possible to use a different command, by setting the DOCKER_CMD environment variable before running the terraform commands.

Examples:

export DOCKER_CMD='podman'
terraform apply
export DOCKER_CMD=`lima nerdctl`
terraform apply

Inputs

Name Description Type Default Required
ecr_namespace (Optional) A namespace prefixed to the ECR repository name, e.g. 'my-namespace' in my-namespace/my-repo string null no
hash_script (Optional) Path to a custom script to generate a hash of source contents string "" no
image_default_tag Default tag to use for the container image string "latest" no
image_force_delete (Optional) Force delete image? bool false no
image_source_path Path to the image source code string n/a yes
image_suffix Suffix used to name the container image, e.g. 'my-repo' in my-namespace/my-repo string n/a yes
image_tag_mutability Image tag immutability. Must be one of MUTABLE or IMMUTABLE string "MUTABLE" no
lifecycle_policy Repository lifecycle policy. A default will be used if not provided string null no
push_script (Optional) Path to a custom script to build and push the container image string "" no
scan_on_push Scan image on push? bool true no

Modules

No modules.

Outputs

Name Description
ecr_repository The AWS ECR Repository resource
image_hash Hash of the image source code, applied as a tag to the container image
image_uri Image URI
repository_url ECR repository URL

Providers

Name Version
aws ~> 4.50
external ~> 2.3
null ~> 3.2

Requirements

Name Version
aws ~> 4.50
external ~> 2.3
null ~> 3.2

Resources

Name Type
aws_ecr_lifecycle_policy.this resource
aws_ecr_repository.this resource
null_resource.push resource
external_external.hash data source

Authors

Andre Silva - @andreswebs

License

This project is licensed under the Unlicense.

Acknowledgements

The code for pushing images to ECR (under scripts/) was based on:

https://github.com/mathspace/terraform-aws-ecr-docker-image