AWS CodeDeploy Tutorial

A jurney to the AWS CI/CD (Part - 3)

AWS CodeDeploy Tutorial

Day 52 of #90DaysOfDevOps

Task-01 :

  • Read about Appspec.yaml file for CodeDeploy.

  • Deploy index.html file on EC2 machine using nginx

  • you have to set up a CodeDeploy agent to deploy code on EC2

Task-02 :

  • Add appspec.yaml file to CodeCommit Repository and complete the deployment process.

What is CodeDeploy?

  • AWS CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances, on-premises instances, serverless Lambda functions, or Amazon ECS services.

What is the AppSpec file for CodeDeploy?

  • The application specification file (AppSpec file) is a YAML-formatted or JSON-formatted file used by CodeDeploy to manage a deployment.

  • The AppSpec file for an EC2/On-Premises deployment must be named appspec.yml, unless you are performing a local deployment.

Create CodeDeploy application.

  • Go to the dashboard of CodeDeploy from AWS and create one.

    Enter a name, and select compute platform as EC2. In the tags enter the name as KEY and the name of the ec2 instance as value.

  • Create a deployment group, but before that create a role that permits the codeDeploy to access S3, EC2, CodeBuild, and CodeDeploy.

  • A deployment group is a set of EC2 instances where you want to deploy your application.

Create Service Role

  • Create a service role for a deployment group for which we are going to create next. This role will provide access to EC2, S3, and CodeDeploy.

  • Go to IAM > Roles > Create.

  • Click on Next and enter a name. Then create.

  • Here, go to the service role that we have created and attach policies.

  • Attach the following policies:

Policy name

Type

Attached as

AmazonEC2FullAccess

AWS managed

Permissions policy

AmazonEC2RoleforAWSCodeDeploy

AWS managed

Permissions policy

AmazonEC2RoleforAWSCodeDeployLimited

AWS managed

Permissions policy

AmazonS3FullAccess

AWS managed

Permissions policy

AWSCodeDeployFullAccess

AWS managed

Permissions policy

AWSCodeDeployRole

AWS managed

Permissions policy

Launch an EC2 instance

  • Again, an EC2 instance will be required to launch the deployment group, hence we need to create an instance for deployment.

  • Do the settings just as done above and launch the instance.

Go back to CodeDeploye

  • Create a deployment group to specify where the application is going to deploy.

  • Enter the group name and service role ARN which we just created.

  • Select Deployment type as In-place.

  • For the environment configuration select EC2 and select the EC2 instance.

  • Next, for agent configuration with AWS Systems Manager click on "Never" as of now.

  • Click on Create.

Set up EC2 instance

  • Go to the instance and connect to it to set up a code deploy agent.

  • After connecting to the instance, create a script file with ".sh" extension and paste the below code and run with bash command. This will install and setup the CodeDeploy agent.

      #!/bin/bash 
      # This installs the CodeDeploy agent and its prerequisites on Ubuntu 22.04.  
      sudo apt-get update 
      sudo apt-get install ruby-full ruby-webrick wget -y 
      cd /tmp 
      wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/releases/codedeploy-agent_1.3.2-1902_all.deb 
      mkdir codedeploy-agent_1.3.2-1902_ubuntu22 
      dpkg-deb -R codedeploy-agent_1.3.2-1902_all.deb codedeploy-agent_1.3.2-1902_ubuntu22 
      sed 's/Depends:.*/Depends:ruby3.0/' -i ./codedeploy-agent_1.3.2-1902_ubuntu22/DEBIAN/control 
      dpkg-deb -b codedeploy-agent_1.3.2-1902_ubuntu22/ 
      sudo dpkg -i codedeploy-agent_1.3.2-1902_ubuntu22.deb 
      systemctl list-units --type=service | grep codedeploy 
      sudo service codedeploy-agent status
    

    Wait for a while.

  • After completion, you can check the service running.

Create a Service role for EC2

  • Create and attach a role to the instance that can permit to access S3, CodeDeploy, and EC2.

Attach AmazonEC2FullAccess, AmazonS3FullAccess, AWSCodeDeployFullAccess

Create and attach to the EC2 instance.

Create and upload an appspec file

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html
hooks:
  AfterInstall:
    - location: scripts/install_nginx.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/run_nginx.sh
      timeout: 300
      runas: root

Make changes in buildspec file specifically in artifacts section.

Push the changes to the repository.

Code Build Artifacts

  • Go to Code Build and edit the artifacts and make it into a zip file for compression.

    • Update the artifact and Start the build.

Create Deployment

  • From the deployment group, create a deployment.

  • Copy the S3 URL to paste in deployment.

  • Create deployment.

  • Succeeded.

  • Hit the IP address of the instance in the browser to check webpage is opening or not.

    • DONE! Next, all the processes will be on the ci/cd pipeline.

Thank you.