What is AWS CodeDeploy?
AWS CodeDeploy is a deployment service offered by Amazon Web Services (AWS) that streamlines and automates the deployment of applications to various target environments, including Amazon EC2 instances, on-premises servers, serverless Lambda functions, and Amazon ECS services. CodeDeploy can handle applications stored in Amazon S3 buckets, GitHub repositories, or Bitbucket repositories. One of its standout features is the ability to deploy serverless Lambda functions seamlessly without requiring code modifications.
Task of the Days:-
In this task, weāll focus on deploying an index.html
file onto an Amazon EC2 instance using AWS CodeDeploy. To achieve this, we'll need to set up an EC2 instance, configure Nginx as our web server, and establish the CodeDeploy agent on the EC2 instance. Let's dive into each step:
Step 1: Set Up an EC2 Instance
Launch an EC2 Instance: Log in to your AWS Management Console, navigate to the EC2 service, and launch a new EC2 instance. Choose an Amazon Machine Image (AMI) that suits your applicationās requirements.
Configure Security Groups: Ensure that your EC2 instanceās security group allows HTTP traffic (port 80) so that your web server can be accessed from the internet.
Connect to the EC2 Instance: Once your EC2 instance is running, connect to it using SSH. Youāll need to use the key pair associated with the instance for authentication.
Step 2: Install and Configure Nginx
- Update the Package Lists: Run the following command to update the package lists on your EC2 instance:
sudo apt-get update
2.Install Nginx: Install Nginx with the following command:
sudo apt-get install nginx
3.Start Nginx: Start the Nginx service:
sudo systemctl start nginx
4.Enable Nginx: Ensure that Nginx starts automatically upon system boot:
sudo systemctl enable nginx
Step 3: Set Up AWS CodeDeploy Agent
- Install the CodeDeploy Agent: SSH into your EC2 instance and install the AWS CodeDeploy agent. The installation process may vary depending on your instanceās operating system. For example, on Ubuntu, you can use the following commands:
sudo apt-get update
sudo apt-get install codedeploy-agent
2.Start and Enable the Agent: Start and enable the CodeDeploy agent:
sudo service codedeploy-agent start
sudo systemctl enable codedeploy-agent
Step 4: Prepare Your Deployment Package
1.Create a Deployment Package: Package your index.html
file along with the appspec.yaml
file that specifies how the deployment should occur. The appspec.yaml
file should define things like file locations, permissions, and scripts to run during deployment.
Hereās a simple example of an appspec.yaml
file:
version: 0.0
os: linux
files:
- source: /
destination: /var/www/html
2.Upload to S3: Upload your deployment package, including the index.html
and appspec.yaml
files, to an S3 bucket. Note down the S3 bucket URL.
Step 5: Deploy using AWS CodeDeploy
Create an AWS CodeDeploy Application and Deployment Group: In the AWS CodeDeploy console, create a new application and deployment group. Specify your EC2 instance as the target for the deployment group.
Create a Deployment: Create a deployment and specify the S3 location of your deployment package.
Deploy: Trigger the deployment. AWS CodeDeploy will use the
appspec.yaml
file to deploy theindex.html
file to the specified location on your EC2 instance.
Task-02: Adding appspec.yaml to CodeCommit Repository
In this task, weāll add the appspec.yaml
file to a CodeCommit repository and complete the deployment process.
Step 1: Push the appspec.yaml
File to CodeCommit
Create a CodeCommit Repository: If you havenāt already, create a CodeCommit repository for your project.
Push the
appspec.yaml
File: Add theappspec.yaml
file to your local project directory and push it to the CodeCommit repository using Git:
git add appspec.yaml
git commit -m "Add appspec.yaml for AWS CodeDeploy"
git push origin master
Step 2: Set Up AWS CodePipeline
Create an AWS CodePipeline: In the AWS CodePipeline console, create a new pipeline. Configure it to monitor your CodeCommit repository as the source.
Add a Deployment Stage: Add a deployment stage to your pipeline, specifying AWS CodeDeploy as the deployment action.
Configure Deployment: Configure the deployment action to use the deployment group you created earlier in AWS CodeDeploy. Specify the S3 location of your deployment package.
Save and Release: Save the pipeline configuration and trigger a release. AWS CodePipeline will pull the
appspec.yaml
andindex.html
files from your CodeCommit repository and deploy them to your EC2 instance using AWS CodeDeploy.
By completing these tasks, youāve successfully added the appspec.yaml
file to your CodeCommit repository and integrated it into your CI/CD pipeline, ensuring seamless and automated deployments of your index.html
file on your EC2 instance. This represents a significant step forward in building a robust CI/CD pipeline on AWS.