Introduction

AWS CodeDeploy is a fully managed deployment service that automates software deployments to a variety of compute services such as Amazon EC2, AWS Fargate, AWS Lambda, and on-premises servers. CodeDeploy makes it easier to rapidly release new features, helps avoid downtime during application deployment, and handles the complexity of updating your applications.

Key Concepts

  1. Deployment Types:

    • In-place Deployment: Updates the application on the same set of instances.
    • Blue/Green Deployment: Deploys the new version on a separate set of instances and then switches traffic to the new instances.
  2. Deployment Configurations:

    • OneAtATime: Deploys the application to one instance at a time.
    • HalfAtATime: Deploys the application to half of the instances at a time.
    • AllAtOnce: Deploys the application to all instances simultaneously.
  3. Application Specification File (AppSpec File):

    • Defines the deployment actions.
    • Contains hooks for lifecycle events.
  4. Lifecycle Events:

    • BeforeInstall: Tasks to be performed before the application revision is installed.
    • AfterInstall: Tasks to be performed after the application revision is installed.
    • ApplicationStart: Tasks to be performed after the application revision is started.
    • ValidateService: Tasks to be performed to validate the service is running correctly.

Setting Up AWS CodeDeploy

Step 1: Create an Application

  1. Open the AWS Management Console.
  2. Navigate to the CodeDeploy service.
  3. Click on "Create application".
  4. Enter a name for your application.
  5. Choose the compute platform (EC2/On-premises, AWS Lambda, or Amazon ECS).
  6. Click "Create application".

Step 2: Create a Deployment Group

  1. In the CodeDeploy console, select your application.
  2. Click on "Create deployment group".
  3. Enter a name for the deployment group.
  4. Choose a service role that grants CodeDeploy permissions to access your resources.
  5. Select the deployment type (In-place or Blue/Green).
  6. Configure the deployment settings (e.g., load balancer, deployment configuration).
  7. Click "Create deployment group".

Step 3: Create an AppSpec File

The AppSpec file is a YAML or JSON file that defines the deployment actions. Below is an example of a YAML AppSpec file for an EC2/On-premises deployment:

version: 0.0
os: linux
files:
  - source: /src
    destination: /var/www/html
hooks:
  BeforeInstall:
    - location: scripts/install_dependencies.sh
      timeout: 300
      runas: root
  AfterInstall:
    - location: scripts/start_server.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/verify_service.sh
      timeout: 300
      runas: root

Step 4: Create a Deployment

  1. In the CodeDeploy console, select your application.
  2. Click on "Create deployment".
  3. Choose the deployment group.
  4. Specify the revision location (e.g., S3 bucket, GitHub repository).
  5. Click "Create deployment".

Practical Example

Example: Deploying a Simple Web Application

  1. Create an Application:

    • Name: MyWebApp
    • Compute Platform: EC2/On-premises
  2. Create a Deployment Group:

    • Name: MyWebAppDeploymentGroup
    • Service Role: CodeDeployServiceRole
    • Deployment Type: In-place
    • Deployment Configuration: CodeDeployDefault.OneAtATime
  3. AppSpec File:

    version: 0.0
    os: linux
    files:
      - source: /src
        destination: /var/www/html
    hooks:
      BeforeInstall:
        - location: scripts/install_dependencies.sh
          timeout: 300
          runas: root
      AfterInstall:
        - location: scripts/start_server.sh
          timeout: 300
          runas: root
      ApplicationStart:
        - location: scripts/verify_service.sh
          timeout: 300
          runas: root
    
  4. Create a Deployment:

    • Application Name: MyWebApp
    • Deployment Group: MyWebAppDeploymentGroup
    • Revision Location: S3://mybucket/mywebapp.zip

Scripts

  • install_dependencies.sh:

    #!/bin/bash
    yum install -y httpd
    
  • start_server.sh:

    #!/bin/bash
    service httpd start
    
  • verify_service.sh:

    #!/bin/bash
    curl -I http://localhost
    

Exercises

Exercise 1: Create a CodeDeploy Application

  1. Create a new CodeDeploy application named MyFirstApp.
  2. Choose the compute platform as EC2/On-premises.

Exercise 2: Create a Deployment Group

  1. Create a deployment group named MyFirstAppDeploymentGroup for the MyFirstApp application.
  2. Use the CodeDeployDefault.OneAtATime deployment configuration.

Exercise 3: Write an AppSpec File

  1. Write an AppSpec file that copies files from /src to /var/www/html.
  2. Include hooks for BeforeInstall, AfterInstall, and ApplicationStart.

Exercise 4: Create a Deployment

  1. Create a deployment for the MyFirstApp application using the MyFirstAppDeploymentGroup.
  2. Use an S3 bucket to store the application revision.

Solutions

Solution to Exercise 1

  1. Open the AWS Management Console.
  2. Navigate to the CodeDeploy service.
  3. Click on "Create application".
  4. Enter MyFirstApp as the application name.
  5. Choose EC2/On-premises as the compute platform.
  6. Click "Create application".

Solution to Exercise 2

  1. In the CodeDeploy console, select MyFirstApp.
  2. Click on "Create deployment group".
  3. Enter MyFirstAppDeploymentGroup as the deployment group name.
  4. Choose a service role.
  5. Select In-place as the deployment type.
  6. Choose CodeDeployDefault.OneAtATime as the deployment configuration.
  7. Click "Create deployment group".

Solution to Exercise 3

version: 0.0
os: linux
files:
  - source: /src
    destination: /var/www/html
hooks:
  BeforeInstall:
    - location: scripts/install_dependencies.sh
      timeout: 300
      runas: root
  AfterInstall:
    - location: scripts/start_server.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/verify_service.sh
      timeout: 300
      runas: root

Solution to Exercise 4

  1. In the CodeDeploy console, select MyFirstApp.
  2. Click on "Create deployment".
  3. Choose MyFirstAppDeploymentGroup as the deployment group.
  4. Specify the revision location (e.g., S3://mybucket/myfirstapp.zip).
  5. Click "Create deployment".

Conclusion

In this section, you learned about AWS CodeDeploy, its key concepts, and how to set up and use it for deploying applications. You also practiced creating a CodeDeploy application, deployment group, and deployment, and writing an AppSpec file. This knowledge will help you automate and manage your application deployments efficiently. In the next module, we will explore AWS CodePipeline, which integrates with CodeDeploy to provide a complete CI/CD solution.

© Copyright 2024. All rights reserved