ECR Repository with AWS CLI and Terraform

Alex Duncan
2 min readJun 6, 2023

--

AWS’s Elastic Container Registry (ECR) service provides a secure and scalable way to store and manage Docker container images. In this article, we will explore how to create an ECR repository using the AWS CLI and Terraform. The AWS CLI allows us to interact with AWS services via the command line, while Terraform provides a powerful infrastructure as code tool to define and provision resources. Let’s dive in!

IT Training Discount Coupon (https://clarusway.referral-factory.com/sW5XCt/join?j=10826&sfmc_sub=2678253&l=1485_HTML&u=106547&mid=546000854&jb=1)

Step 1: Installing Prerequisites: Before we begin, ensure that you have the AWS CLI and Terraform installed on your local machine. You can download and install them from the official AWS and Terraform websites, respectively. Once installed, configure the AWS CLI with your AWS access key and secret access key using the aws configure command.

Step 2: Creating an ECR Repository with AWS CLI: The AWS CLI provides a straightforward way to create an ECR repository. Follow these steps:

  1. Open a terminal or command prompt.
  2. Run the following command to create the ECR repository:
aws ecr create-repository --repository-name my-ecr-repo

3. The command will return a JSON response containing details about the created repository. Note down the repository URI for future reference.

Step 3: Creating an ECR Repository with Terraform: Terraform allows us to define infrastructure as code using its declarative language. Here’s how you can create an ECR repository using Terraform:

  1. Create a new directory for your Terraform configuration.
  2. Inside the directory, create a new file named main.tf and open it in a text editor.
  3. Add the following code to the main.tf file:
provider "aws" {
region = "us-east-1" # Update with your desired AWS region
}

resource "aws_ecr_repository" "my_ecr_repo" {
name = "my-ecr-repo"
}

4. Save the file and return to the terminal or command prompt

5. Navigate to the directory containing your Terraform configuration file.

6. Run the following commands to initialize Terraform and create the ECR repository:

terraform init
terraform apply

7. Terraform will display an execution plan. Confirm the changes by typing yes.

8. After the command completes, Terraform will create the ECR repository based on your configuration.

In this article, we learned how to create an ECR repository using the AWS CLI and Terraform. The AWS CLI allows for quick and straightforward repository creation, while Terraform provides a more structured and reusable approach to infrastructure provisioning. By leveraging both tools, you can efficiently manage your container images and streamline your development and deployment workflows.

Remember to clean up any resources you created once you’re done experimenting to avoid unnecessary costs. Happy containerizing!

--

--

Alex Duncan

Human, AWS/DevOps Expert, Writer, Reader, Researcher, Traveler