Day 45: AWS CD

Day 45: AWS CD

#90daysofdevops

·

4 min read

🚀Introduction

In the previous blog, we explored how to configure CodeBuild and establish a deployment pipeline. Building on that foundation, this blog will focus on deploying applications to EC2 instances using CodeDeploy. We’ll walk through the steps needed to set up CodeDeploy, configure it for your EC2 instances, and ensure your deployments are seamless and efficient.


To set up roles for CodeDeploy and EC2, you’ll need to create two separate IAM roles: one for the EC2 instance and one for CodeDeploy. Here’s a step-by-step guide:

🔸Create two IAM Role

Create IAM Role for EC2

  1. Go to IAM Roles:

    • Open the AWS Management Console.

    • Navigate to IAM > Roles.

  2. Create a New Role:

    • Click on Create role.

    • Choose AWS service as the trusted entity type.

    • Select EC2 as the use case.

    • Click on Next: Permissions.

  3. Attach Permissions:

    • Search for and select the policy named AmazonEC2FullAccess (this policy allows EC2 instances to interact with CodeDeploy).

    • Click Next: Tags (optional) to add tags if needed.

    • Click Next: Review.

  4. Name and Create the Role:

    • Give the role a name, such as ec2deploy

    • Click Create role.

Create IAM Role for CodeDeploy

  1. Go to IAM Roles:

    • Open the AWS Management Console.

    • Navigate to IAM > Roles.

  2. Create a New Role:

    • Click on Create role.

    • Choose AWS service as the trusted entity type.

    • Select CodeDeploy as the use case.

    • Click on Next: Permissions.

  1. Attach Permissions:

    • Search for and select the policy named AWSCodeDeployRole (this policy allows CodeDeploy to interact with EC2 and other necessary services).

    • Click Next: Tags (optional) to add tags if needed.

    • Click Next: Review.

  2. Name and Create the Role:

    • Give the role a name, such as CodeDeployRole.

    • Click Create role.


🔸Launch Instance

Now, launch the instance. While launching the instance, under the Advanced Settings, select the IAM role you created for EC2 from the IAM Instance Profile dropdown

If you still can’t get it follow the steps given below:

  • Go to EC2 > Instances in the AWS Management Console.

  • Select your instance, then click on Actions > Security > Modify IAM Role.

  • Choose the EC2CodeDeployRole role you created earlier and click Update IAM role.


Now run the instance and execute the commands provided below. You can also visit the website mentioned below to understand it better

Install the CodeDeploy agent for ubuntu server

Change bucket-name & region-identifier. To know bucket-name & region-identifier click on resource kit bucket name by region

Follow the given picture to understand

wget https://bucket-name.s3.region-identifier.amazonaws.com/latest/install

My region is us-West (Oregon) so I selected this, you can select according to your region.


🔸Create Application & Deployment group

Select EC2 bcoz we are going to deploy our application on ec2 instance

Create Deployment group

Select role that you have created for codedeploy

In value select ec2 instance that you have created for codedeployment


🔸Create Deployment

You can use my repository

If you don’t know from where to get Commit ID follow the pictures given below

Remember always select latest ID don’t select any Commit ID

After deployment & all logs get succeeded now go towards the pipeline that we created during CodeBuild


🔸Create Pipeline for Deployment

Don’t make new pipeline click on that pipeline that we created during CodeBuild

Click on edit button

Scroll down & come to the botton and add stage

Click on add action group

After adding action group save & come to the instance

Don’t save & release change now, first install docker in ec2 instance & then click on release change otherwise you will get error


🔸Install Docker

Check any docker is running or not by typing sudo docker ps if docker is running then remove it by typing sudo docker rm -f <ContainerID>. And restart docker by typing sudo systemctl restart docker

Click on release change


🚀 Conclusion

In this blog, we explored the process of setting up and configuring AWS CodeDeploy to automate the deployment of applications. We covered the key steps, including creating a CodeDeploy application & configuring deployment groups. By following the steps and examples provided, you should now have a solid understanding of how to use CodeDeploy for automating your deployment workflows.

In the next blog, we'll dive deeper into Cloud Watch


Thanks for reading to the end; I hope you gained some knowledge.❤️🙌

Linkedln

Twitter

Github

Â