Day 51: Your CI/CD pipeline on AWS - Part 3 ๐Ÿš€ โ˜

Day 51: Your CI/CD pipeline on AWS - Part 3 ๐Ÿš€ โ˜

ยท

6 min read

What is CodeDeploy ?

AWS CodeDeploy is a service that automates code deployments to any instance, including Amazon EC2 instances and instances running on-premises. It makes the process of releasing new features, updates, and bug fixes smoother and more efficient.

Imagine your software application as a rocket ๐Ÿš€. With traditional deployment methods, you manually load the rocket with each update, ensuring everything is in place before launch. It's time-consuming and prone to errors.

Now, picture AWS CodeDeploy as your mission control center ๐Ÿ›ฐ๏ธ. You upload your code to CodeDeploy, and it takes care of the deployment process, launching your updates automatically to the right destinations.

Here's how it works:

  1. Upload Your Code: You prepare your application code along with deployment configurations and upload it to AWS CodeDeploy.

  2. Define Deployment Settings: You specify the deployment configuration, such as deployment type, deployment group, and any custom settings.

  3. Target Deployment Environment: You choose the target environment where you want to deploy your code, whether it's Amazon EC2 instances, on-premises servers, or even AWS Lambda functions.

  4. Deployment Strategy: CodeDeploy allows you to define deployment strategies, such as rolling deployments, blue/green deployments, or canary deployments. These strategies help minimize downtime and risk during deployments.

  5. Deployment Execution: CodeDeploy automatically deploys your application to the specified instances according to your defined deployment strategy. It monitors the deployment process and ensures that each step completes successfully.

  6. Validation and Rollback: After deployment, CodeDeploy can validate the deployment results based on predefined criteria. If any issues arise, it can automatically roll back to a previous version, ensuring your application remains stable.

Task-01 :

  • Read about Appspec.yaml file for CodeDeploy.

    The appspec.yaml file is a crucial component in AWS CodeDeploy deployments. It's a YAML-formatted file that provides CodeDeploy with instructions on how to deploy your application to the target instances. Here's an overview of what you can typically include in an appspec.yaml file:

    1. Application Structure:

    • Define directories/files in your application package and where they should be deployed on the target instances.

2. Hooks:

  • Hooks define actions to be taken at various stages of the deployment process. Common hooks include:

    • BeforeInstall: Actions to be performed before installation.

    • AfterInstall: Actions to be performed after installation.

    • ApplicationStart: Actions to be performed after the application is deployed and started.

    • ValidateService: Actions to validate the deployment.

3. Permissions:

  • Specify permissions for files and directories on the target instances, ensuring proper access controls.

4. Lifecycle Events:

  • Define scripts or commands to run during deployment lifecycle events. For example:

    • Installing dependencies.

    • Starting or stopping services.

    • Configuring environment variables.

Example appspec.yaml File:

    yamlCopy codeversion: 0.0
    os: linux
    files:
      - source: /
        destination: /var/www/myapp
    hooks:
      BeforeInstall:
        - location: scripts/install_dependencies.sh
          timeout: 300
          runas: root
      AfterInstall:
        - location: scripts/configure_app.sh
          timeout: 300
          runas: root
      ApplicationStart:
        - location: scripts/start_app.sh
          timeout: 300
          runas: root
    permissions:
      - object: /var/www/myapp
        pattern: "**"
        owner: ec2-user
        group: apache
        mode: 755

In this example:

  • The files section specifies that all files in the root directory of the application package should be deployed to /var/www/myapp on the target instances.

  • Various hooks are defined, including BeforeInstall, AfterInstall, and ApplicationStart, each with corresponding scripts to execute.

  • permissions ensure that proper permissions are set for the deployed files and directories.

The appspec.yaml file provides flexibility and control over the deployment process, allowing you to

  • Deploy index.html file on EC2 machine using nginx

To deploy an index.html file on an EC2 instance using Nginx, you'll need to follow these general steps:

  1. Set Up an EC2 Instance:

    • Launch an EC2 instance using the AWS Management Console or AWS CLI.

    • Choose an appropriate Amazon Machine Image (AMI) with your desired operating system (e.g., Amazon Linux, Ubuntu, etc.).

    • Ensure that the security group associated with the EC2 instance allows inbound traffic on port 80 (HTTP) for Nginx.

  2. Install Nginx:

    • Connect to your EC2 instance using SSH.

    • Update the package repository: sudo apt update (for Ubuntu) or sudo yum update (for Amazon Linux).

    • Install Nginx: sudo apt install nginx (for Ubuntu) or sudo yum install nginx (for Amazon Linux).

    • Start the Nginx service: sudo service nginx start or sudo systemctl start nginx.

    • Verify that Nginx is running: sudo service nginx status or sudo systemctl status nginx.

  3. Deploy index.html:

    • Upload your index.html file to the appropriate directory on the EC2 instance. By default, the Nginx web server serves files from the /var/www/html directory.

    • You can use SCP or SFTP to transfer the file from your local machine to the EC2 instance:

        javascriptCopy codescp -i <path_to_private_key> /path/to/index.html ec2-user@<instance_public_ip>:/var/www/html/
      
  4. Access the Website:

    • Open a web browser and enter the public IP address or domain name of your EC2 instance. You should see your index.html file served by Nginx.
  5. Optional: Customize Nginx Configuration:

    • You can customize the Nginx configuration file located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default to configure additional settings like virtual hosts, SSL certificates, etc.
  6. Optional: Set Up Domain Name (if applicable):

    • If you have a domain name, you can point it to the public IP address of your EC2 instance using DNS configuration.

That's it! Your index.html file should now be deployed and accessible via Nginx on your EC2 instance. Remember to consider security best practices, such as configuring SSL/TLS certificates and setting appropriate permissions for your files.

  • you have to setup a CodeDeploy agent in order to deploy code on EC2

You're absolutely correct! I apologize for the oversight. To deploy code using AWS CodeDeploy on an EC2 instance, you need to set up the CodeDeploy agent on the instance. Here's how you can do it:

  1. Launch an EC2 Instance:

    • Follow the steps to launch an EC2 instance as mentioned earlier.
  2. Install and Configure the CodeDeploy Agent:

    • Connect to your EC2 instance using SSH.

    • Install the CodeDeploy agent using the appropriate commands for your operating system. For example:

      • For Amazon Linux:

          bashCopy codesudo yum update -y
          sudo yum install -y ruby
          sudo yum install -y wget
          cd /home/ec2-user
          wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
          chmod +x ./install
          sudo ./install auto
        
      • For Ubuntu:

          bashCopy codesudo apt-get update
          sudo apt-get install -y ruby
          sudo apt-get install -y wget
          cd /home/ubuntu
          wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
          chmod +x ./install
          sudo ./install auto
        
    • This script downloads and installs the CodeDeploy agent, and starts the service automatically.

  3. Verify CodeDeploy Agent Installation:

    • You can verify that the CodeDeploy agent is running using the following command:

        luaCopy codesudo service codedeploy-agent status
      
  4. Create an Application and Deployment Group in AWS CodeDeploy:

    • Go to the AWS Management Console and navigate to the CodeDeploy service.

    • Create a new application and specify the deployment platform as EC2/On-Premises.

    • Create a deployment group and select the EC2 instances where you want to deploy your application.

  5. Deploy Your Code:

    • Upload your application files (including the appspec.yaml file) to an S3 bucket.

    • Create a deployment in AWS Code Deploy, specifying the application, deployment group, and the location of your application files in the S3 bucket.

    • AWS Code Deploy will automatically trigger the deployment process on the specified EC2 instances.

ย