Task-01 :
Read about Appspec.yaml file for CodeDeploy.
Deploy index.html file on EC2 machine using nginx
you have to setup a CodeDeploy agent in order to deploy code on EC2
Step 1: Read about
Appspec.yaml
file for CodeDeployBefore we jump into deployment, it's essential to understand the
appspec.yaml
file, which plays a crucial role in defining how your application should be deployed.This file is used by CodeDeploy to determine what actions to take during the deployment process.
It specifies hooks, permissions, and other details required for a successful deployment.
Here's a simple 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/install_nginx.sh timeout: 300 runas: root ApplicationStart: - location: scripts/stop_nginx.sh timeout: 300 runas: root ApplicationStop: - location: scripts/start_nginx.sh timeout: 300 runas: root
Let's break down its sections:
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
, andApplicationStart
). 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.
Step 2: Deploy index.html
file on EC2 using Nginx
Creating a CodeDeploy Application
Go to the AWS Management Console and navigate to CodeDeploy.
Click on "Applications" and then select "Create application."
Choose the compute platform as "EC2/on-premises" and click "Create application.
Your application is now successfully created.
Creating a Service Role
To enable communication between CodeDeploy and other AWS services, you need to create a service role.
Go to AWS Identity and Access Management (IAM) and create a role named 'code-deploy-service-role' with the required permissions.
Setting Up an EC2 Instance
Before deploying your application, create an EC2 instance where you want to deploy the index.html file.
Launch an Ubuntu EC2 instance on AWS.
Creating a Deployment Group
Once you've created a CodeDeploy application, it's time to create a deployment group. This group defines the EC2 instances where your application will be deployed.
Add instance names to the deployment group.
Click on "Create deployment group."
Installing the CodeDeploy Agent
To deploy code on your EC2 instance, you must install the CodeDeploy agent.
Install the CodeDeploy agent on your Ubuntu EC2 instance by running the provided commands & Check whether the Code agent is running.
# 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
Before moving forward, create the index.html
file that you want to deploy on your EC2 instance.
Task-02 :
Add appspec.yaml file to CodeCommit Repository and complete the deployment process.
Adding an appspec.yaml
file to CodeCommit Repository
Create an appspec.yaml file that specifies how CodeDeploy should handle your application.
Push all the files, including the
appspec.yaml
file, to your CodeCommit repository.
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.
Deploying Your Application
In AWS CodeDeploy, go to "Applications," "Deployment Groups," and select your deployment group.
Click on "Create deployment," and paste the object URL in the Revision location.
Now we can see the deployment is successful and you can check whether the file is running or not by going
intoartifact.zip
>index.html and click on index.html file to have output.