Suvarna Taware
Technext
Published in
6 min readAug 3, 2019

--

Bitbucket to AWS EC2 Continuous Deployment Pipeline using AWS Code Deploy For PHP Application

CI/CD PHP Application

Pre-requisite:-

  1. Basic Knowledge of Continuous deployment & AWS Services ( EC2, IAM , S3 & Code Deploy)
  2. To get started you will need to get an account at AWS and Bitbucket. Bitbucket is free for private repositories and AWS has a free tier plan. Once you have your accounts created then you are ready to go.

This post shows how to deploy a simple PHP codebase/script on an AWS EC2 Instance and to automate its delivery every time a git push is executed to your bitbucket master repository.

This is how the test.php file for our PHP Application

<?php
echo "Hello World!";
?>

The directory structure looks like:

Process -:>

Step 1: Create an IAM Group, User, Role

Create an IAM Group with the permissions-

  • AmazonS3FullAccess
  • AWSCodeDeployFullAccess

A.Steps to create an IAM group and attach policies (console)

1.Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/.

2.In the navigation pane, click Groups and then click Create New Group.

3.In the Group Name box, type the name of the group (bit_CodeDeployRole)and then click Next Step.

4.In the list of policies, select the check box for Policy -AmazonS3FullAccess,AWSCodeDeployFullAccess. Then click Next Step.

5.Click Create Group.

B.Steps to create an IAM User and attach policies (console)

1.Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/.

2.In the navigation pane, choose Users and then choose Add user.

3.Type the user name (CodeDeployUser) for the new user.
4.Select the type of access as “programmatic access” for this user.

5.On the Set permissions page, select permission as “Add user to group”

5.Under Add user to the group section ,select group created in step 1 i.e “bit_CodeDeployRole”

Save the credentials(Access Key, Secret Access Key), we will need it later.

C.Steps to create an IAM Role and attach policies (console)

Create a Role in IAM as AWSCodeDeployRole and select EC2 Service as our EC2 instance will be using that role later to interact with CodeDeploy and add policies of AmazonS3FullAccessand AWSCodeDeployRole

After creating the role, edit the Trust Relationship to be as follows.
Change the region to the region you are working on, mine is N.Virginia that is us-east-1

{
"Version": "2012–10–17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"ec2.amazonaws.com",
"codedeploy.us-east-1.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}

Step 2: Create an S3 Bucket and an EC2 Instance

A.Steps to create an s3 bucket ( console)

1.Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/.
2.Choose Create bucket
3.In the Bucket name field, type a unique name for your new bucket.I am giving name as bitbucket-codedeploy-deployment
4.For Region, choose US East (N. Virginia) as the region where you want the bucket to reside.

B.Steps to create an EC2 Instance ( console)

Next, create an EC2 Instance .Select OS as per your requirement, I am using an Ubuntu. We need to Add the IAM Role-AWSCodeDeployRole which we already created. Now in order for AWS Code Deploy to recognize our instance it will look for tags, therefore we need to tag it so we’ll tag it say
Name = CodeDeployDirect, open up proper ports you need for your application in security groups like our application will need port 80 open.

Launch the instance and then install the CodeDeploy Agent on your server, depending on your OS the installation process might vary a proper guide can be found here, Follow the instructions according to your OS and make sure the Code Deploy agent is up and running!

wget https://aws-codedeploy-us-west-2.s3.us-west-2.amazonaws.com/latest/install

Step 3: Create a CodeDeploy Application

Go to AWS CodeDeploy Console and click on Create Application

Then Create a deployment Group with the following settings
Name : DG1 (You can choose any)
Service Role : AWSCodeDeployRole
Deployment Type : In Place
Deployment Settings : CodeDeployDefault.OneAtATime
Disable Load Balancing
And Under Environment configuration :

You should see 1 Matching Instance, then press Create Deployment Group

So far we’ve set up -

  1. An IAM user to be used by the Bitbucket Pipeline
  2. An S3 bucket to store application zip file
  3. An EC2 Instance on which our application is to be deployed and we have also installed code deploy agent in order to deploy our application according to the appspec.yml file.
  4. You can see some basic examples of how to configure the appspec.yml here , also you can read more about how to configure bitbucket-pipelines.yml here.

For our application the appspec.yml is as follows:

version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/my-app1

Add the appspec.yml file to your repository on root folder and then add repository variables in Bitbucket, also enable pipelining when prompted.

All that is remaining now is to set up and push the bitbucket-pipelines.ymlto our repository so that everything works.

image: atlassian/default-image:2

pipelines:
default:
- step:
script:
- apt-get update
- apt-get install -y zip
- zip -r application1.zip .
- pipe: atlassian/aws-code-deploy:0.2.5
variables:
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
APPLICATION_NAME: $APPLICATION_NAME
S3_BUCKET: $S3_BUCKET
COMMAND: 'upload'
ZIP_FILE: 'application1.zip'
VERSION_LABEL: 'my-app-1.0.0'
- pipe: atlassian/aws-code-deploy:0.2.5
variables:
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
APPLICATION_NAME: $APPLICATION_NAME
DEPLOYMENT_GROUP: $DEPLOYMENT_GROUP
S3_BUCKET: $S3_BUCKET
COMMAND: 'deploy'
WAIT: 'true'
VERSION_LABEL: 'my-app-1.0.0'
IGNORE_APPLICATION_STOP_FAILURES: 'true'
FILE_EXISTS_BEHAVIOR: 'OVERWRITE'

The above pipe is another script which is maintained by Atlassian Link, you just have to paste the pipe, supply a few key pieces of information, and the rest is done for you.

Now it’s time to commit and push to remote.

After pushing changes to Bitbucket go to the pipeline section of your repository you will be able to see a pipeline triggered with #1 something like

And In few minutes, you’ll see a successful pipeline run.If you check your files on EC2 you’ll see that your changes have now been deployed.

Pipeline Successful

Now you can try changing code in your local repository. once you push changes in master , in minutes ,it will get deployed on remote ec2 instance!

--

--