Automated Continuous Deployment with AWS Code Deploy and Code Pipeline

Mary Wangoi
10 min readJun 18, 2024

--

Introduction

In this guide, we’ll dive into setting up continuous deployment (CD) using AWS CodeDeploy, a powerful managed service for automating application deployments.

Before implementing this project, it’s crucial to complete the continuous integration (CI) phase using AWS services like CodeCommit, CodeBuild, and CodePipeline.

I’ve already covered this essential step in a previous Medium article, which you can find here.

Let’s streamline our deployment process with AWS CodeDeploy and ensure a smooth transition from CI to CD.

Feel free to connect with me on LinkedIn to discuss this post, or ask any questions.

Tools and Technologies Used

  • AWS CodePipeline: Continuous integration and continuous delivery service that orchestrates your release process.
  • AWS CodeDeploy: Managed deployment service that automates software deployments to a variety of computing services such as Amazon EC2 instances, AWS Lambda functions, or on-premises servers.
  • Amazon EC2: Scalable virtual servers in the cloud used as deployment targets by AWS CodeDeploy.

These tools enable efficient deployment automation and scalability, ensuring reliable and consistent application releases without the need to manage underlying infrastructure.

Prerequisites

Detailed steps for setting up each prerequisite will be provided in the documentation.

  1. AWS Account:

You need an AWS account to access and use AWS services, including AWS CodeDeploy.

2. GitHub Repository:

Have a GitHub repository where your application source code is stored. This repository will trigger automated deployments in AWS CodeDeploy upon code commits.

3. CodeDeploy IAM Role:

Create an IAM role with permissions for AWS CodeDeploy to access your deployment resources.

4. Deployment Target:

Prepare your deployment target, which could be:

  • Amazon EC2 instances: Ensure your instances are configured with the necessary IAM roles and permissions. Steps for setting this up will be covered.
  • AWS Fargate: Set up your Fargate profiles if deploying to serverless containers.

5. Deployment Package:

Prepare your application code packaged for deployment, typically stored in Amazon S3 or another accessible repository.

6. Deployment Configuration:

Define your deployment configuration in AWS CodeDeploy, including deployment groups and deployment settings.

Setting Up Git Repository

  • Fork the GitHub Repository: Fork the GitHub repository I’ve prepared for this demo. You can find it here.

Create an EC2 Instance

Note that this step can also be automated using Terraform, but for the sake of simplicity, we will do it manually here.

Here is a detailed step-by-step guide to creating an EC2 instance:

  1. Log into AWS Management Console:

2. Open the EC2 Dashboard:

  • In the AWS Management Console, search for “EC2” in the search bar.
  • Select “EC2” from the list of services.

3. Launch Instance:

  • Click the “Launch Instance” button on the EC2 Dashboard.

4. Add Tags:

  • (Optional) Add tags to your instance for easier identification.

5. Choose an Amazon Machine Image (AMI):

  • Select an appropriate AMI for your needs. For this example, you can choose “Ubuntu”.

6. Choose an Instance Type:

  • Select an instance type. For most use cases, the “t2.micro” instance type is sufficient and is free tier eligible.

7. Select a Key Pair:

  • Select an existing key pair or create a new key pair to securely access your instance.
  • Download the key pair file (.pem) and keep it safe. You cannot download it again after it is created.

8. Configure Instance Details:

  • Optionally, configure instance details such as network settings, subnets, IAM role, etc. You can leave these settings as default for now.

9. Configure Security Group:

  • Create a new security group or select an existing one.
  • Ensure that you have rules allowing SSH access (port 22) from your IP address.

10. Add Storage:

  • The default storage settings should be sufficient.
  • You can adjust the size if needed.

11. Review and Launch:

  • Review your instance configuration.
  • Click “Launch” to proceed.

Access Your Instance

Once the instance is launched, it will take a few minutes to initialize.

You can then connect to your instance using SSH with the downloaded .pem file.

We are using Mobaxterm to SSH to the EC2 Instance ( For Windows machines).

  • Get MobaXterm and install it.
  • Open MobaXterm from your desktop or the Start menu.
  • Click the “Session” button, then select “SSH”.
  • Enter the IP address or hostname of the server and specify username.
  • Click on “Advanced SSH settings” tick the “Use private key” box and select the private key pair you used in your instance.
  • Click “OK” to connect.

Installing AWS CodeDeploy Agent

1: Install the AWS CodeDeploy Agent:

The AWS CodeDeploy agent is required on your EC2 instance to facilitate communication with the AWS CodeDeploy service.

This agent manages deployments and ensures that your application updates are orchestrated correctly according to your deployment configurations.

For detailed installation instructions, refer to the AWS documentation here.

Ensure you select the documentation relevant to the operating system used on your EC2 instance.

2: Verify the Installation:

After installing the AWS CodeDeploy agent, verify its installation by running the following command on your EC2 instance:

sudo service codedeploy-agent status

Install Docker

Ensure Docker is installed to use Docker commands later in the project.

sudo apt install docker -y

Creating IAM Role and Granting Permissions

An IAM role in AWS defines the permissions and access controls for services like AWS CodeDeploy.

It enables secure management and deployment of applications by granting necessary permissions to EC2 instances, ensuring seamless interaction with AWS services.

1; Create an IAM Role:

  • Navigate to the AWS Management Console and go to the IAM service.
  • Click on “Roles” in the left-hand menu and then “Create role”.
  • Choose the service that will use this role (EC2 in this case) and click “Next: Permissions”.
  • In the “Filter policies” search box, type “AWSCodeDeployRole” and select the policy named AWSCodeDeployRole (for AWS managed policies).
  • Alternatively, create a custom policy allowing the necessary permissions for AWS CodeDeploy. Click “Next”.
  • Optionally, add tags to your role for better organization and click “Next: Review”.
  • Provide a name and description for the role and click “Create role”.

2; Attach IAM Role to EC2 Instance:

  • Once the role is created, navigate to the EC2 service in the AWS Management Console.
  • Select your EC2 instance, click “Actions”, navigate to “Security”, and then “Modify IAM Role”.
  • Choose the IAM role created in the previous step and click “Update IAM Role”.

Go to your terminal and restart your code deploy agent service.

sudo service codedeploy-agent restart

Then run the status again to verify it has started.

sudo service codedeploy-agent status

3: Grant Additional Permissions

  • Navigate to the above created IAM Role.
  • Click on the “Permissions” tab, then click “Add permissions” and then “Attach Policies”.
  • In the search box, type “AmazonEC2FullAccess” and select the policy.
  • Review the policy and click “Add permissions” to attach it to the role.
  • Verify the role’s permissions include both AWSCodeDeployRole and AmazonEC2FullAccess.

Configure AWS Code Deploy Application

Configure AWS CodeDeploy to orchestrate and automate application deployments across your AWS infrastructure.

Follow these steps to set up deployment groups, define deployment settings, and integrate with other AWS services seamlessly.

  • Go to the AWS Management Console and select the AWS CodeDeploy service.
  • Click on “Create Application” button.
  • Provide a name for your application.
  • Select the compute platform where your application will be deployed (EC2/On-premises or AWS Lambda).
  • Click on “Create application” to finalize and create the application.
  • Once created, you will see the new application listed in your CodeDeploy dashboard.

Create Deployment Group in AWS CodeDeploy

Set up a deployment group in AWS CodeDeploy to define where and how your application will be deployed across your AWS infrastructure.

Follow these steps to configure deployment settings, choose deployment targets, and finalize the setup for seamless application deployments.

  • Navigate to AWS CodeDeploy Console
  • In the CodeDeploy dashboard, click on the name of the application you previously created.
  • Click on the “Create deployment group” button.
  • Provide a name for your deployment group.
  • Configure deployment settings like service role.
  • Choose the deployment type (In-place or Blue/green).
  • Select the environment configuration (EC2/On-premises or AWS Lambda).
  • Mention the key and value of this. (It will be matched with an Instance)
  • You can configure advanced settings such as triggers, alarms, and notifications.
  • Click on “Create deployment group” to finalize and create the deployment group.
  • Once created, the new deployment group will be listed under your application in the CodeDeploy dashboard.

Create a Deployment in AWS CodeDeploy

  • Navigate to AWS CodeDeploy Console
  • In the CodeDeploy dashboard, click on the name of the application you previously created.
  • Click on “Create deployment”.
  • Select the deployment group you created earlier.
  • Choose the source of your application code. Options include:

S3: Specify the S3 bucket name and object key.

GitHub: Provide the repository name and commit ID.

Bitbucket: Provide the repository name and commit ID.

Local file: Upload a local file as the revision.

  • Optionally, provide a description for the deployment.
  • Configure additional options (optional).
  • Click on “Create deployment” to start the deployment process.
  • Monitor the status of your deployment in the CodeDeploy dashboard to ensure it completes successfully.

Integrate AWS CodeDeploy with AWS CodePipeline

Integrating AWS CodeDeploy with AWS CodePipeline automates your deployment process, enabling continuous delivery (CD).

This integration ensures that every change in your source code repository triggers a new deployment, streamlining your workflow and improving deployment efficiency.

Here are the steps to set up this integration:

  • Log in to the AWS Management Console.
  • Go to the AWS CodePipeline service.
  • Find and select the pipeline we set up in the previous project while working on the CI part of this project.
  • Find the steps of creating the pipeline here.
  • Click on “Edit” in the top-right corner of the pipeline details page.
  • Click on “Add stage” at the appropriate point in your pipeline where you want the deployment to occur (typically after the build stage).
  • Name the stage (e.g., “Deploy”) then click “Add Stage”.
  • Provide a name for this action (e.g., “DeployAction”).
  • Select “AWS CodeDeploy”.
  • Select the region where your CodeDeploy application is located.
  • Select the output artifact from the previous build stage.
  • Choose the CodeDeploy application you created.
  • Choose the CodeDeploy deployment group you created.
  • Click on “Done”.
  • Review the changes to ensure that the new deploy stage and action are correctly configured.
  • Click on “Save pipeline changes”.
  • Trigger the pipeline manually by clicking “Release change” or wait for an automated trigger from a new code commit.
  • Monitor the progress of the pipeline in the CodePipeline console. Each stage (Source, Build, Deploy) will show its status.

Clean Up Resources

After completing the deployment, it’s important to clean up resources to avoid unnecessary charges. Ensure you:

  1. Terminate EC2 Instances: Stop and terminate the EC2 instances used for deployment.
  2. Delete the CodeDeploy Application: Remove the CodeDeploy application and deployment groups.
  3. Remove IAM Roles and Policies: Delete any IAM roles and policies created specifically for this project.
  4. Clean Up the CodePipeline: If no longer needed, delete the CodePipeline to prevent further triggers and resource usage.

By cleaning up, you maintain a tidy AWS environment and avoid incurring unexpected costs.

Conclusion

In this project, we extended our existing CI pipeline by integrating AWS CodeDeploy to automate the deployment process.

By following the outlined steps, we successfully set up a seamless CI/CD pipeline using AWS services.

This setup ensures efficient, automated deployments, enhancing our workflow and reducing manual intervention.

Feel free to refer to this guide for setting up similar projects, and explore the benefits of continuous delivery using AWS CodeDeploy.

For further information on the CI part of this project, you can refer here.

Connect with me

For more insights and future projects, feel free to connect with me on LinkedIn , GitHub and Medium.

--

--

Mary Wangoi

AWS, Kubernetes, Docker, Jenkins, Terraform, Ansible, Prometheus, Python, Git, ArgoCD, Linux