Table of contents
Introduction:)
๐ Welcome to Day 52 of #90DaysOfDevOps! ๐
Today, we're diving deep into AWS CodeDeploy, a robust deployment service provided by Amazon Web Services. It streamlines the process of deploying applications and code to various compute services, including EC2 instances, AWS Lambda functions, and on-premises servers.
What is CodeDeploy?
AWS CodeDeploy is a fully managed deployment service provided by Amazon Web Services. It automates the process of deploying applications or code to various compute services, including Amazon EC2 instances, AWS Lambda functions, and on-premises servers.
It can also deploy a wide range of content and applications, including:
Code
Serverless AWS Lambda functions
Web and configuration files
Executables
AWS CodeDeploy makes it easy to put your latest software into action on your chosen servers. It comes with handy features like different ways to deploy and the ability to go back to an earlier version if needed.
Tasks:)
Task 1: Understanding Appspec.yaml and Deploying to EC2
Read about Appspec.yaml file for CodeDeploy.
The
appspec.yaml
file is a crucial configuration file used by AWS CodeDeploy. It defines the deployment process and instructs CodeDeploy on how to manage the deployment on your target environment. You can find detailed information on creating aappspec.yaml
file in the official AWS documentation here.Here is a basic example of an
appspec.yaml
file:version: 0.0 os: linux files: - source: / 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
Below are key components commonly found in an
appspec.yaml
file:| Component | Description | | --- | --- | | Version | Specifies the version of the deployment specification (e.g., 0.0). | | OS | Defines the operating system used by the deployment target (e.g., Linux). | | Resources | Allows you to manage AWS resources like Auto Scaling groups or EC2 instances. | | Hooks | Defines lifecycle event hooks for different deployment phases (e.g., BeforeInstall, AfterInstall, ApplicationStop, etc.). Each hook specifies the location of a script file to be executed during that phase. | | Files | Specifies which files should be copied from the source location to the destination location on the deployment target. |
Deploy index.html file on EC2 machine using nginx
To deploy an
index.html
file on an EC2 instance using Nginx and AWS CodeDeploy, follow these steps:Creating a CodeDeploy Application:
Navigate to the AWS Management Console and access CodeDeploy.
Click on "Applications," then select "Create application."
Choose the compute platform as "EC2/on-premises" and click "Create application."
Your application is now successfully created.
Setting Up a Service Role:
Create a role named 'code-deploy-service-role' in AWS Identity and Access Management (IAM) with the necessary permissions. This role facilitates communication between CodeDeploy and other AWS services.
Configuring an EC2 Instance:
- Launch an Ubuntu EC2 instance on AWS, where you'll deploy the index.html file.
Creating a Deployment Group:
After creating a CodeDeploy application, proceed to form a deployment group. This group defines the EC2 instances for deployment.
Add instance names to the deployment group.
Click on "Create deployment group."
Installing the CodeDeploy Agent:
- To enable code deployment on your EC2 instance, you must install the CodeDeploy agent. Execute the provided commands.
# This installs the CodeDeploy agent and its prerequisites on Ubuntu
#!/bin/bash
sudo apt-get update
sudo apt-get install ruby
sudo apt-get install wget
wget https://aws-codedeploy-ap-south-1.s3.ap-south-1.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent status
sudo service codedeploy-agent start
sudo service codedeploy-agent status
Creating an index.html File:
Create the index.html file that you intend to deploy on your EC2 instance.
Task-02: Adding appspec.yaml to CodeCommit Repository
Adding an
appspec.yaml
file to CodeCommit RepositoryCreate the appspec.yaml File:
yamlCopy codeversion: 0.0 os: linux files: - source: / destination: /var/www/html hooks: BeforeInstall: - location: install_nginx.sh timeout: 300 runas: root AfterInstall: - location: start_nginx.sh timeout: 300 runas: root
Also add the
install_
nginx.sh
, andstart_
nginx.sh
files as needed abovePush Files to CodeCommit Repository:
Commit the changes.
git add appspec.yaml install_nginx.sh start_nginx.sh git commit -m "Added appspec.yaml and scripts" git push origin <branch-name>
Replace
<branch-name>
with the actual branch name.
Verify Changes in CodeCommit:
Go to your CodeCommit repository in the AWS Management Console and verify that the files have been successfully pushed.
Rebuilding the Project and Preparing for Deployment
Edit the artifacts in your CodeBuild project and rebuild it.
Create an S3 bucket for code deployment, or use an existing one.
Add the artifacts to the S3 bucket and take note of the object URL.
build the project it will automatically added the artifacts
Deploying Your Application
Open AWS CodeDeploy:
- Go to AWS CodeDeploy in the AWS Management Console.
Navigate to Deployment Groups:
In the CodeDeploy dashboard, click on "Applications" in the left sidebar.
Select your application.
Select Your Deployment Group:
- Choose the deployment group you created earlier.
Create a Deployment:
Click on "Create deployment."
Specify Revision:
In the "Revision location" section, paste the object URL from your S3 bucket where your artifacts are stored.
Deployment Configuration:
- Choose the deployment configuration based on your requirements.
Deployment Group Settings:
- Review and adjust deployment settings as needed.
Deploy:
- Click on "Create deployment" to initiate the deployment process.
Monitor Deployment:
You can monitor the progress of the deployment in the CodeDeploy console.
Verify Deployment:
- Once the deployment is complete, verify that your application is running as expected.
With these steps, you have successfully deployed your application using AWS CodeDeploy. Ensure to monitor the deployment progress to address any issues that may arise.
Thank you for diving into this blog with me! I trust you found the information both helpful and enlightening. To stay updated on the latest in DevOps ๐, make sure to follow me. Remember, staying informed means staying ahead in the dynamic world of DevOps!
Feel free to connect with me on:
For more updates and engaging discussions on DevOps, let's connect! ๐ #DevOpsCommunity