Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Terraform - Create an Elastic Container Registry

Below is some sample code you can use to deploy an elastic container registry (ECR) to push/pull your private Docker images.

# Allow the user to specify the region to deploy to.
variable "aws_region" {
    type = string
    description = "The region to deploy to. E.g. eu-west-2 for London."
    default = "eu-west-2"
}


# Allow the user to specify the name of the registry
variable "registry_name" {
    type = string
    description = "The name to give the elastic container registry."
}


provider "aws" {
    region = var.aws_region
}


# Create an ECR
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository
resource "aws_ecr_repository" "my_ecr_repository" {
    name = var.registry_name
}


# Output the URL of the ECR we created so we know where to push to.
output "ecr_url" {
    value = aws_ecr_repository.my_ecr_repository.repository_url
}
Last updated: 2nd February 2024
First published: 22nd June 2021