top of page

Tutorial Blog to configure CodeDeploy to deploy an application on Amazon EC2 instance

Learning Objectives:  

  • Learn to create configure CodeDeploy.

  • Learn to create deploy WordPress on an Amazon EC2 instance.

Prerequisite:

Step 1: Create a role.

In AWS console, go to IAM, go to Roles, and choose Create role.


ree

Under Select type of trusted entity, select AWS service. Under Choose a use case, select EC2. Under Select your use case, choose EC2. Choose Next: Permissions.


ree

Search for and select the policy named AmazonEC2RoleforAWSCodeDeploy.

 

ree

Choose Next: Review. Enter a name for the role (for example, EC2InstanceRoleForCodeDeploy).

 

ree

Choose Create role.

 

ree

Choose Create role.


ree

Under Select trusted entity, choose AWS service. Under Use case, choose CodeDeploy. Choose CodeDeploy from the options listed. Choose Next.

 

ree

The AWSCodeDeployRole managed policy is already attached to the role.

Choose Next.


ree

Enter a name for the role (for example, CodeDeployServiceRole), and then choose Create role.

 

ree

Choose Create role.

 

ree

Step 2:  Create a EC2 instance.

Open the Amazon EC2 console at new tab.

Go to instances and choose Launch instances.

Under Name and tags, give name as my-wordpress.

 

ree

In Application and OS Images. Here search/select Amazon Linux 2 AMI 

 

ree

 

Choose an Instance Type over here.

Select General purpose type t2.micro Instance Type.

select existing key pair.

Choose an existing key pair option from the drop down.


ree

In Network settings section, click edit to change the following.

a) In the Subnet field select on the drop-down list and select any one of the Subnets.

b) In the Auto-assign Public IP field select on the drop-down list and select Enable option.

 

ree

Configure Security Group

a) Create a new security group and name it as WEBSERVER_SG

b) In the description enter the following text: Security Groups for webservers

c) Keep the default SSH rule and add a new rule. From the drop-down list select HTTP Rule.

d) Note - Warning: Rules with source of 0.0.0.0/0 allow all IP addresses to access your instance. We recommend setting security group rules to allow access from known IP addresses only. Ignore this for this Tutorial.

e) While using this feature for production make sure the known IP address is entered.

 

ree

 

In Advanced details, under IAM instance profile, attach EC2 instance role that you created previously (AmazonEC2RoleforCodeDeploy).

 

ree

Keep rest as default and click on Launch instance.


Step 3: Now, go to CloudShell, which is at the bottom left corner of your console (or to the right of your search bar as well).


ree

Once you click on the CloudShell icon, a terminal interface will appear as below.


ree

Upload the master.zip file in CloudShell.

To upload the file, click on action button on top right corner of CloudShell and click on Upload file.

 

ree

Select the file and click on open.

 

ree

You can see your file is uploaded.


ree

 

Call the unzipmkdircp, and rm commands to:

Unpack the master .zip file into the /tmp/WordPress_Temp directory (folder).

Copy its unzipped contents to the /tmp/WordPress destination folder.

Delete the temporary /tmp/WordPress_Temp folder and master file.

Run the commands one at a time:


unzip master -d /tmp/WordPress_Temp


ree

mkdir -p /tmp/WordPress


cp -paf /tmp/WordPress_Temp/WordPress-master/* /tmp/WordPress


rm -rf /tmp/WordPress_Temp



ree

Create a scripts directory in your copy of the WordPress source code:

mkdir -p /tmp/WordPress/scripts


ls /tmp/WordPress


ree

Create an install_dependencies.sh file in /tmp/WordPress/scripts.

 

nano /tmp/WordPress/scripts/install_dependencies.sh


ree

Add the following lines to the file. This install_dependencies.sh script installs Apache, MySQL, and PHP.

 

#!/bin/bash

sudo amazon-linux-extras install php7.4

sudo yum install -y httpd mariadb-server php

 

Note: Save the code by pressing CTRL+O and hit enter.

To exit from the file press CTRL+X.


ree

Create a start_server.sh file in /tmp/WordPress/scripts.

 

nano /tmp/WordPress/scripts/start_server.sh


ree

Add the following lines to the file. This start_server.sh script starts Apache and MySQL.

 

#!/bin/bash

sudo systemctl start mariadb.service

sudo systemctl start httpd.service

sudo systemctl start php-fpm.service


ree

Create a stop_server.sh file in /tmp/WordPress/scripts.

 

nano /tmp/WordPress/scripts/stop_server.sh


ree

Add the following lines to the file. This stop_server.sh script stops Apache and MySQL.


#!/bin/bash

isExistApp=pgrep httpd

if [[ -n $isExistApp ]]; then

systemctl stop httpd.service

fi

isExistApp=pgrep mysqld

if [[ -n $isExistApp ]]; then

systemctl stop mariadb.service

fi

isExistApp=pgrep php-fpm

if [[ -n $isExistApp ]]; then

systemctl stop php-fpm.service


fi


ree

Create a create_test_db.sh file in /tmp/WordPress/scripts.

 

nano /tmp/WordPress/scripts/create_test_db.sh

 

ree

Add the following lines to the file. This create_test_db.sh script uses MySQL to create a test database for WordPress to use.

 

#!/bin/bash

mysql -uroot <<CREATE_TEST_DB

CREATE DATABASE IF NOT EXISTS test;

CREATE_TEST_DB


ree

Finally, create a change_permissions.sh script in /tmp/WordPress/scripts

 

nano /tmp/WordPress/scripts/change_permissions.sh


ree

This is used to change the folder permissions in Apache.


#!/bin/bash

sudo chmod -R 777 /var/www/html/WordPress


ree

Give all the script’s executable permissions. On the command line, type:


chmod +x /tmp/WordPress/scripts/*


ree

Create a file named appspec.yml.

 

nano /tmp/WordPress/appspec.yml


ree

 

Add the following lines to the file:


version: 0.0

os: linux

files:

  - source: /

    destination: /var/www/html/WordPress

hooks:

  BeforeInstall:

    - location: scripts/install_dependencies.sh

      timeout: 300

      runas: root

  AfterInstall:

    - location: scripts/change_permissions.sh

      timeout: 300

      runas: root

  ApplicationStart:

    - location: scripts/start_server.sh

    - location: scripts/create_test_db.sh

      timeout: 300

      runas: root

  ApplicationStop:

    - location: scripts/stop_server.sh

      timeout: 300

      runas: root

 

ree

Step 4:

Call the mb command to create an Amazon S3 bucket named codedeploydemobucket-my-wordpress-bucket:

 

aws s3 mb s3://codedeploydemobucket-my-wordpress-bucket --region ap-south-1

 

ree

On the development machine, switch to the folder where the files are stored:

 

cd /tmp/WordPress


Call the create-application command to register a new application named WordPress_App:


aws deploy create-application --application-name WordPress_App

 

ree

Call the CodeDeploy push command to bundle the files together, upload the revisions to Amazon S3, and register information with CodeDeploy about the uploaded revision, all in one action.

 

aws deploy push \

  --application-name WordPress_App \

  --s3-location s3://codedeploydemobucket-my-wordpress-bucket/WordPressApp.zip \

  --ignore-hidden-files

 

ree

aws s3 ls s3://codedeploydemobucket-my-wordpress-bucket

 

ree

Step 5: Open EC2 instances and click on Connect.

 

ree

ree

ree

sudo yum update -y


ree

sudo yum install ruby -y


ree


ree

chmod +x ./install


ree

sudo ./install auto


ree

sudo systemctl status codedeploy-agent


ree

Step 6: 

Now go to IAM service and go to the roles and click on CodeDroloyServiceRole, copy the ARN


ree

Now that you have the service role ARN, call the create-deployment-group command to create a deployment group named WordPress_DepGroup, associated with the application named WordPress_App, using the Amazon EC2 tag named CodeDeployDemo, and deployment configuration named CodeDeployDefault.OneAtATime

 

Note: Go back to CloudShell

 

Note: Replace by arn:aws:iam::account-no:role/CodeDeployServiceRole

 your CodeDeployServiceRole ARN.

 

aws deploy create-deployment-group \

  --application-name WordPress_App \

  --deployment-group-name WordPress_DepGroup \

  --deployment-config-name CodeDeployDefault.OneAtATime \

  --ec2-tag-filters Key=Name,Value=my-wordpress,Type=KEY_AND_VALUE \

  --service-role-arn arn:aws:iam::12345678958:role/CodeDeployServiceRole


ree

Now call the create-deployment command to create a deployment associated with the application named WordPress_App, the deployment configuration named CodeDeployDefault.OneAtATime, and the deployment group named WordPress_DepGroup, using the application revision named WordPressApp.zip in the bucket named codedeploydemobucket-my-wordpress-bucket:

 

aws deploy create-deployment \

  --application-name WordPress_App \

  --deployment-config-name CodeDeployDefault.OneAtATime \

  --deployment-group-name WordPress_DepGroup \

 --s3-location bucket=codedeploydemobucket-my-wordpress-bucket,bundleType=zip,key=WordPressApp.zip


ree

Get the deployment's ID by calling the list-deployments command against the application named WordPress_App and the deployment group named WordPress_DepGroup:

 

aws deploy list-deployments --application-name WordPress_App --deployment-group-name WordPress_DepGroup --query 'deployments' --output text


ree

Go to CodeDeploy there you can see your App is Deployed.


ree

Step 7: Login to WordPress

Go to EC2 and copy the Public IPv4 DNS.


ree

Now paste URL/DNS in new browser along with /WordPress in the end.


ree

 

This window will appear. Click on Let’s go!


ree

Database: Name: test

Username: root

Password: keep blank

Rest is default and click on Submit.


ree

Click on Run the installation.

Fill the following details accordingly. And click on install WordPress.

ree

You will get the following window. Now click on Log In.


ree

Now insert the details and login.


ree

 

We have successfully Installed and login to WordPress. Now in the top left corner click on WordPress logo.


ree

It will redirect you to this page.


ree

Step 8: Update the application.

To update the application, Run the following command in CloudShell to open WordPress file.

nano /tmp/WordPress/wp-admin/about.php

Once the file is open find the word WordPress and add the following context before word WordPress.

Hi this is My update to the WordPress.


ree

ree

Note: Save the code by pressing CTRL+O and hit enter.

To exit from the file press CTRL+X.


Step 9: To update and redeploy the application, we need to zip and push revised code to S3 bucket.

Run the following command in CloudShell.

aws deploy push \

  --application-name WordPress_App \

  --s3-location s3://codedeploydemobucket-my-wordpress-bucket/WordPressApp.zip \

  --ignore-hidden-files


ree

ree

Run the following command in to redeploy the application.

aws deploy create-deployment --application-name WordPress_App --deployment-config-name CodeDeployDefault.OneAtATime --deployment-group-name WordPress_DepGroup --s3-location bucket=codedeploydemobucket-my-wordpress-bucket,bundleType=zip,key=WordPressApp.zip


ree

Now go back to your browser and refresh the page to check the update.

Here you can see your page is updated.


ree

If you no longer need then you can delete the EC2 Instance, S3 bucket and application from CodeDeploy.



Was this document helpful? How can we make this document better. Please provide your insights. You can download PDF version for reference. 

 




For your aws certification needs or for aws learning contact us.

 

 

3 commentaires


Gokul M
Gokul M
11 avr. 2024

It is easy to learn to configure codeDeploy to deploy an application with the help of this blog.

J'aime

Jothipriya
Jothipriya
11 avr. 2024

Learning how to configure CodeDeploy to deploy an application with the help of this blog

J'aime

Gokulnath
Gokulnath
11 avr. 2024

Learnt to configure CodeDeploy to deploy an application on Amazon EC2 instance

J'aime
bottom of page