diff --git a/README.md b/README.md index 1ac9665..6e790bf 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,37 @@ # terraform-aws-ecr-docker-image -Builds a Docker image for use on AWS ECR + +Builds & pushes a Docker image to an AWS ECR repository. + +The image can then be used in an ECS or Fargate task. + +- Builds from a Dockerfile in the source path +- Pushes to an AWS ECR repository +- Can customize the push and hash scripts +- Cleans up old images from the repository + +## Requirements + +- Docker +- md5sum (e.g. from `brew install md5sha1sum`) + +## Usage + +See [examples](examples). + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| hash\_script | Path to script to generate hash of source contents | string | `""` | no | +| image\_name | Name of Docker image | string | n/a | yes | +| push\_script | Path to script to build and push Docker image | string | `""` | no | +| source\_path | Path to Docker image source | string | n/a | yes | +| tag | Tag to use for deployed Docker image | string | `"latest"` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| hash | Docker image source hash | +| repository\_url | ECR repository URL of Docker image | +| tag | Docker image tag | diff --git a/examples/python-hello-world/main.tf b/examples/python-hello-world/main.tf new file mode 100644 index 0000000..76c4598 --- /dev/null +++ b/examples/python-hello-world/main.tf @@ -0,0 +1,15 @@ +terraform { + backend "local" { + path = "terraform.tfstate" + } +} + +provider "aws" { + region = "us-west-1" +} + +module "python-hello-world" { + source = "../../" + image_name = "python-hello-world" + source_path = "${path.module}/src" +} diff --git a/examples/python-hello-world/src/Dockerfile b/examples/python-hello-world/src/Dockerfile new file mode 100644 index 0000000..65f5b17 --- /dev/null +++ b/examples/python-hello-world/src/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.7-alpine + +RUN mkdir /src +ADD main.py /src/main.py + +WORKDIR /src + +ENTRYPOINT ["python", "main.py"] diff --git a/examples/python-hello-world/src/main.py b/examples/python-hello-world/src/main.py new file mode 100644 index 0000000..bace31f --- /dev/null +++ b/examples/python-hello-world/src/main.py @@ -0,0 +1,10 @@ +import logging + + +# Setup logging in order for CloudWatch Logs to work properly +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger() + + +if __name__ == "__main__": + logger.info("Hello world") diff --git a/hash.sh b/hash.sh new file mode 100755 index 0000000..8cfd7f8 --- /dev/null +++ b/hash.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# +# Calculates hash of Docker image source contents +# +# Invoked by the terraform-aws-ecr-docker-image Terraform module. +# +# Usage: +# +# $ ./hash.sh . +# + +set -e + +source_path=${1:-.} + +# Hash all source files of the Docker image +# Exclude Python cache files, dot files +file_hashes="$( + cd "$source_path" \ + && find -s . -type f -not -name '*.pyc' -not -path './.**' -exec md5sum {} \; +)" + +hash="$(echo "$file_hashes" | md5sum | cut -d' ' -f1)" + +echo '{ "hash": "'"$hash"'" }' diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..e34e3c3 --- /dev/null +++ b/main.tf @@ -0,0 +1,39 @@ +resource "aws_ecr_repository" "repo" { + name = "${var.image_name}" +} + +resource "aws_ecr_lifecycle_policy" "repo-policy" { + repository = "${aws_ecr_repository.repo.name}" + + policy = <