Create ECR repository in AWS using Terraform: Practical Step-by-Step Guide

Step 1: Install Terraform

If you haven’t already, install Terraform on your machine. You can download by referring to Terraform Install

Step 2: Configure AWS Provider

Create a new Terraform configuration file, let’s call it main.tf. In this file, you need to define the AWS provider and specify your AWS credentials. Here’s an example:

provider "aws" {
region = "us-east-1" # Replace with your desired AWS region
access_key = "YOUR_AWS_ACCESS_KEY"
secret_key = "YOUR_AWS_SECRET_KEY"
}


Replace YOUR_AWS_ACCESS_KEY and YOUR_AWS_SECRET_KEY with your actual AWS access key and secret key. Alternatively, you can use environment variables or an AWS credentials file.

Step 3: Create ECR Repository

In the same main.tf file, add a resource block to create an ECR repository:

resource "aws_ecr_repository" "my_ecr_repo" {
name = "my-ecr-repo" # Replace with your desired repository name
image_tag_mutability = "MUTABLE"

image_scanning_configuration {
scan_on_push = true
}
}



This configuration creates an ECR repository named “my-ecr-repo” with image scanning enabled on push. You can customize the repository name and other settings as needed.

Step 4: Initialize Terraform

Open your terminal or command prompt, navigate to the directory containing your main.tf file, and run the following command to initialize Terraform:

terraform init

Step 5: Review the Execution Plan

Before applying the configuration, you can review the execution plan by running:

terraform plan

Step 6: Apply the Configuration

If the execution plan looks good, apply the configuration by running:

terraform apply

This command will prompt you to confirm the changes. Type yes to proceed. Terraform will create the ECR repository according to your configuration.

Step 7: Verify the deployment via the AWS console

Step 8: Delete the deployment.

You can delete the AWS ECR once it’s not required via the following command in the cli:

terraform destory

That’s it! Your Terraform script will now be used to create an ECR Repository. From here on, you will be able to push Docker images to this repository or performing other operations as required.This is a basic example but you can customize this terraform code according to your specific needs. For instance: you can set repository policies and lifecycle polices or even configure other advanced options.

How To Create ECR Repository In AWS Using Terraform ?

In a cloud computing and microservice world, we must concentrate on the involvement of the infrastructure resources properly. With Terraform, an open source IaC (infrastructure as code) tool that is widely used for provisioning and managing cloud resources across many cloud service providers such as AWS (Amazon Web Services), becoming the de-facto standard, it is not surprising that people have started noticing its advantages over other available options. Among the workable services of AWS is Amazon Elastic Container Registry (ECR) which is a well-managed Docker container registry that gives the chance to images to store, manage, and deploy.

Terraform replies to the difficulty of creating and handling ECR repositories by providing a hot approach to IT architecture preservation. In Terraform, you may define your desired state of an ECR repository and other associated resources in a software configuration file. Terraform will consequently handle the creation, updating, and maintenance procedures for your infrastructure.

Similar Reads

What is an ECR repository?

The Amazon ECR is a fully managed Docker container offering provided by Amazon Web Services as it is prevalently called, abbreviated as AWS, which is a cloud computing subsidiary of Amazon. In turn, it becomes the hub where Docker image collections are located and maintained as well as allocated to containerized applications that are crucial in building applications....

What is Terraform?

Terraform is an open-source resource infrastructure (RI) tool manufactured by HashiCorp. It provides the independence of its declaration and configuration to the developers and operations teams to define, provision, and manage cloud infrastructure resources across multiple cloud providers and off-premises environments in a consistent manner....

Create ECR repository in AWS using Terraform: Practical Step-by-Step Guide

Step 1: Install Terraform...

Advantages of using Terraform to create AWS ECR repository:

Using Terraform to create an Amazon Elastic Container Registry (ECR) repository offers several advantages:...

Disadvantages of using Terraform to create AWS ECR repository:

While using Terraform to create an AWS Elastic Container Registry (ECR) repository offers several advantages, there are also some potential disadvantages to consider:...

Conclusion

To conclude, the cloud provider architecture enables the centralized control, management, and automation of infrastructure processes through Terraform to provision and manage the repositories. Implementing Infrastructure as Code (IaC) principles with Terraform would be a beneficial exercise for organizations, ensuring consistency across the infrastructure, easy reproduction, and better version control...

ECR Repository In AWS Using Terraform – FAQ’S

What is the sole advantage of application of Terraform on the construction of an ECR repository?...