- Views: 1
- Report Article
- Articles
- Business & Careers
- Outsourcing
How to deploy container image from ECR to EC2 instances using AWS code-deploy agent
Posted: Nov 06, 2023
Scenario:
In this blog, we are going to learn how to deploy a container image from AWS ECR to an EC2 instance using AWS code-deploy. Prerequisite:
Aws account with access.
Must have an ec2 instance with docker and code-deploy agent installed.
Follow https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install.html
You must already have a code code-commit Repo with your codes.
Steps Involved:
- Create the files in the repo which is used by the code-deploy agent to deploy the image.
- Create an ECR Repo in order to push the docker image.
- Creating a build project which fetches artifacts from the code-commit repo and builds the docker image and pushes to ECR.
- Create a code-deploy application and deployment group with consist of an ec2 instance.
- Automate the process using a code pipeline.
Here we are assuming your repo already has a Dockerfile which is tested and working fine to build the image.
The appspec.yaml file in AWS CodeDeploy is used to define the deployment configuration for an application. It specifies details such as the location of the application files, the scripts to run during the deployment process, and the environment variables to set. The file is used by the CodeDeploy service to manage and automate the deployment of the application to the specified instances.
This script is used to deploy a Docker container to a host. The script performs the following actions:
- It defines a variable container_name with the desired name for the container.
- It logs in to the AWS Elastic Container Registry (ECR) using the aws ecr get-login-password command, which retrieves an authentication token for the registry. The docker login command is then used to log in to the registry using the retrieved token.
- The script pulls the latest version of an image from the specified ECR repository.
- It changes the image tag to the desired container name and tag version.
- The script stops and removes the currently running container with the same name defined in the variable.
- Finally, it creates and starts a new Docker container using the new image, mapping port 80 of the host to port 80 of the container and naming it with the defined container_name variable.
This script can be used as a part of the deploy command in the appspec.yaml file in AWS CodeDeploy.
Step-2: Create an ECR repo.To create an Amazon Elastic Container Registry (ECR) repository and push a Docker image to it, you can follow these steps:
- Open the Amazon ECR console in the AWS Management Console
- Choose the region where you want to create the repository.
- Choose to Create a repository.
- Enter a repository name and choose to Create repository.
Creating a building project which fetches artifacts from the code-commit repo and builds the docker image and pushes to ECR.
- Click on the "Create project" button.
- Enter a name for the project and select the source repository that contains the code you want to build.
- Choose the environment for your You can either use one of the pre-configured environments or create a custom environment.
- Define the build specification for your project.
- Set up the Artifacts that will be produced by the Artifacts can be stored in Amazon S3 or AWS CodeArtifact.
- Configure the build settings, including the build timeout, AWS Identity and Access Management (IAM) roles, and any additional environment variables.
- Review the build project settings and click the "Create project" button to create the project.
- For more information click here: How to deploy container image from ECR to EC2 instances using AWS code-deploy agent
Rate this Article
Leave a Comment