Automating Deployments with AWS CodeDeploy: A Step-by-Step Guide Day - 52

ยท

2 min read

In the realm of DevOps, setting up a robust CI/CD pipeline is crucial for seamless application deployment. In this blog post, we'll explore how to leverage AWS services like CodeDeploy, CodeCommit, CodeBuild, and S3 to automate the deployment process on an EC2 instance with Nginx.

Task-01: Understanding Appspec.yaml for CodeDeploy

What is appspec.yaml?

The appspec.yaml file acts as the deployment orchestrator for AWS CodeDeploy. It defines how the deployment should be executed, including pre and post-deployment scripts.

Example appspec.yaml for deploying an HTML file with Nginx on EC2:

version: 0.0
os: linux
files:
  - source: /
    destination: /usr/share/nginx/html
hooks:
  BeforeInstall:
    - location: scripts/before-install.sh
      timeout: 300
      runas: root
  AfterInstall:
    - location: scripts/after-install.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/application-start.sh
      timeout: 300
      runas: root

This YAML configuration specifies the source and destination of files, along with hooks to run scripts before and after installation.

Deployment Steps:

  1. Create an EC2 instance: Set up an EC2 instance with Nginx installed.

  2. Install CodeDeploy Agent: Facilitate communication between the instance and CodeDeploy by installing the CodeDeploy agent.

  3. Create appspec.yaml: Define the deployment steps in the appspec.yaml file.

  4. Prepare Deployment Package: Package your HTML files, appspec.yaml, and scripts into a zip file.

  5. Upload to S3: Store the deployment package in an S3 bucket.

  6. Create Deployment in CodeDeploy Console: Set up a deployment in the CodeDeploy console, referencing the S3 bucket and deployment package.

  7. Deploy to EC2: CodeDeploy will automate the deployment to the specified EC2 instance.

Task-02: Adding appspec.yaml to CodeCommit Repository

Steps:

  1. Create CodeCommit Repository: Establish a CodeCommit repository to store your application code and appspec.yaml file.

  2. Add appspec.yaml to Repository: Push the appspec.yaml file and your application code to the CodeCommit repository.

  3. Configure CodePipeline: Set up a CodePipeline in AWS CodePipeline service to automate the deployment process.

  4. Define Source Stage: Utilize CodeCommit as the source provider for your CodePipeline.

  5. Define Build Stage: Integrate CodeBuild to build your application code and create the deployment package.

  6. Define Deploy Stage: Integrate CodeDeploy as the deployment provider in the CodePipeline.

  7. Configure CodeDeploy in CodePipeline: Set up the CodeDeploy stage in CodePipeline to use the CodeDeploy application and deployment group.

  8. Trigger Pipeline: Manually trigger the CodePipeline or set it to be triggered automatically upon code changes in the CodeCommit repository.

  9. Monitor Deployment: Keep an eye on the CodePipeline and CodeDeploy console for the deployment status and any potential issues.

By following these tasks, you can establish a seamless CI/CD pipeline using AWS services, ensuring efficient and automated application deployments.

Happy coding and deploying! ๐Ÿš€

ย