Learn how to seamlessly push your Docker images to Amazon Elastic Container Registry (ECR) using the power and automation of Terraform.
This guide provides a step-by-step approach to building and deploying Docker images to Amazon Elastic Container Registry (ECR) using Terraform. We will cover creating an ECR repository, configuring AWS credentials, building and tagging Docker images, pushing images to ECR, and referencing them in your Terraform code.
Define ECR repository:
resource "aws_ecr_repository" "example" {
name = "my-repo"
}
Configure AWS credentials: Ensure your Terraform environment has access to your AWS account.
Build the Docker image: Use the Docker CLI or a tool like Docker Compose.
docker build -t my-image:latest .
Authenticate to ECR: Retrieve an authentication token and log in to your ECR registry using the AWS CLI.
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account_id>.dkr.ecr.us-east-1.amazonaws.com
Tag the image: Tag your Docker image with the ECR repository URI.
docker tag my-image:latest <account_id>.dkr.ecr.us-east-1.amazonaws.com/my-repo:latest
Push the image: Push the tagged image to your ECR repository.
docker push <account_id>.dkr.ecr.us-east-1.amazonaws.com/my-repo:latest
Reference the image in your Terraform code: Use the image
argument in your ECS task definition or other relevant resources.
resource "aws_ecs_task_definition" "example" {
# ...
container_definitions = <<EOF
[
{
"name": "my-container",
"image": "${aws_ecr_repository.example.repository_url}:latest"
}
]
EOF
}
This code provides a complete example of deploying a Docker image to Amazon ECR and using it in an ECS task definition. It includes Terraform code to define an ECR repository and an ECS task definition that references the Docker image. A Dockerfile builds a simple web server using Nginx, and a bash script builds, tags, and pushes the Docker image to the ECR repository. The example demonstrates a basic setup that can be expanded with features like automated deployments, environment variables, service discovery, and load balancing.
This example demonstrates the entire process, from defining an ECR repository to referencing the pushed image in an ECS task definition.
1. Terraform Code (main.tf):
# Configure the AWS Provider
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = "us-east-1" # Replace with your desired region
}
# Define ECR Repository
resource "aws_ecr_repository" "example" {
name = "my-repo"
}
# Define ECS Task Definition
resource "aws_ecs_task_definition" "example" {
family = "my-app"
container_definitions = <<EOF
[
{
"name": "my-container",
"image": "${aws_ecr_repository.example.repository_url}:latest",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80
}
],
"essential": true,
"memory": 512
}
]
EOF
requires_compatibilities = ["FARGATE"]
cpu = "256"
memory = "512"
network_mode = "awsvpc"
}
2. Dockerfile (Dockerfile):
FROM nginx:latest
# Add your application code here
COPY . /usr/share/nginx/html
3. Build and Push Script (build-and-push.sh):
#!/bin/bash
# Replace with your AWS account ID and region
AWS_ACCOUNT_ID=<your_account_id>
AWS_REGION=us-east-1
# Build the Docker image
docker build -t my-image:latest .
# Authenticate to ECR
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
# Tag the image
docker tag my-image:latest $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/my-repo:latest
# Push the image
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/my-repo:latest
echo "Image pushed successfully!"
Steps:
main.tf
, Dockerfile
, and build-and-push.sh
files.<your_account_id>
and us-east-1
with your actual AWS account ID and desired region.chmod +x build-and-push.sh
and then ./build-and-push.sh
.terraform init
, terraform plan
, and terraform apply
to deploy your infrastructure.This example showcases a basic setup. You can expand it further by adding features like:
1. ECR Repository:
aws_ecr_repository
resource in Terraform simplifies the creation process.name
attribute, you can customize repositories with policies for access control, lifecycle rules for image management, and image scanning settings for security.2. AWS Credentials:
3. Building the Docker Image:
docker build
command uses your Dockerfile
as a blueprint to create the image.-t
flag assigns a tag (name:version) to your image for easy identification.Dockerfile
includes all necessary dependencies, configurations, and application code for your containerized application.4. Authenticating to ECR:
5. Tagging the Image:
latest
, v1.0
, etc.).6. Pushing the Image:
docker push
command uploads your tagged image to the specified ECR repository.7. Referencing the Image in Terraform:
repository_url
attribute from the aws_ecr_repository
resource to construct the complete image URI.Additional Tips:
This guide outlines the process of pushing a Docker image to an Amazon Elastic Container Registry (ECR) repository using Terraform and the Docker CLI.
Steps:
aws_ecr_repository
resource).docker build
.aws ecr get-login-password
and docker login
to authenticate with your ECR registry.docker tag
.docker push
.repository_url
attribute from the aws_ecr_repository
resource to reference the image within your Terraform code (e.g., in an ECS task definition).Key Points:
repository_url
attribute dynamically references the ECR repository within your Terraform code.This approach streamlines the deployment of Docker images to ECR, enabling efficient containerized application deployments.
By following these steps, you can efficiently build, deploy, and manage Dockerized applications on AWS, leveraging the power of infrastructure as code with Terraform and the convenience of ECR as a private container registry. This approach enhances development workflows, promotes automation, and ensures secure and scalable container deployments within your AWS environment.