Create Elastic Container Registry (ECR) In AWS With Terraform

In this post, we will create an Elastic Container Registry (ECR) in AWS with Terraform.

ECR

Elastic Container Registry (ECR) is an AWS implementation of a private container registry inside an AWS subscription. ECR is fully private, and no one can access it with a username and password.

Terraform

The Terraform ECR provider allows us to create an ECR without using the AWS management console.

Configuration

The following Terraform configuration will create an ECR with the name ntweeklyecr with a tag (env = prod). I’m also creating the registry in the us-west-2 region.

terraform {

  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "3.47.0"
    }
  }
}

provider "aws" {
  region = "us-west-2"
}

resource "aws_ecr_repository" "ecr" {
  name                 = "ntweeklyecr"
  image_tag_mutability = "MUTABLE"

  image_scanning_configuration {
    scan_on_push = true
 }
  
  tags = {
   Env = "prod"
 }

}

To run the code, use the following Terraform commands.

terraform init
terraform plan
terraform apply

For more AWS posts, visit the AWS category page. To set up Terraform on your machine, visit the following post on our sister blog.


Posted

in

, ,

by