Amazon EC2 Deployment: Complete CI/CD Pipeline using Bitbucket, CodePipeline, and AWS CodeDeploy.
In this post, we will cover another stack of CI/CD Pipeline Technology using Bitbucket, s3, Codepipeline, and CodeDeploy for Amazon EC2 Deployment.
Let’s jump in!
1. Create an EC2 with Amazon EC2 Code Deploy Role:
For this, first, you need to login into your AWS console and go to EC2 section, from there you can click on Launch instance and proceed with it.
After clicking on Launch instance, choose the configuration you want to use as per your needs. I’ll use Ubuntu machine for this blog.
Also, you need to choose the IAM instance profile AmazonEC2CodeDeploy default role in the Advance details section.
2. Install Code Deploy Agent and NodeJS on EC2 Instance:
run those commands on the EC2 terminal.
sudo apt update
sudo apt install -y ruby
sudo apt install -y wget
wget https://aws-codedeploy-ap-southeast-1.s3.ap-southeast-1.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent start
3. Create an IAM user with AWSCodeDeployFullAccess and AmazonS3 access:
For this, move over to the IAM console and create a new user with the following permissions.
4. Setup AWS S3
To save CodeDeploy Revisions we need storage, so you need to create a bucket in S3 storage. Recommended convention of S3 storage bucket name is <application_name>-codedeploy-deployment.
5. Create codedeploy.
Go to AWS Management Console and log in using our AWS Account. Proceed to CodeDeploy in the console and click on Applications. Then, Click on Create application.
We will need to put your application name and select EC2/On-premises as our compute platform.
Create the Deployment Group
Create the deployment group in the application.
In the service role, select the service role we created earlier.
Next, choose Amazon EC2 instances in the environment configuration and put our EC2 instance tag.
Since we’ve already installed CodeDeployAgent, we can choose Never in Agent configuration section. After that choose AllAtOnce in Deployment settings section which will make sure the code will be deployed on all the EC2 instances which verifies the Key-Value pair. After that just click on Create deployment group and you’re good to go.
6. Create a Codepipeline.
In this section, we will configure AWS CodePipeline to connect to our Bitbucket repository and monitor a specific branch for changes. This will trigger the CI/CD pipeline whenever changes are pushed to that branch. Here’s how you can set this up:
Provide the Pipeline naming details and let the service role to be default.
Provide the Code repository details. In our case, we have our code in bitbucket repository. Choose AWS CodePipeline to automatically execute the pipeline in case of any changes in the code automatically.
Now add the deploy stage. Select the provider and name of the already created application and deployment group.
The pipeline will now fetch the code from the CodeCommit repository.
- Then, it will build the code and deploy to the server as we can see in the below screenshot.
7. Repository setting and make the connection on ec2.
Go to your EC2 terminal, create your directory like,
cd home/ubuntu
mkdir my_app
cd my_app
after that should need to clone your repository code from bitbucket and configure the SSH settings.
git clone git@bitbucket.org:username/repo.git
Before pushing your code from your local machine on your desire branch you should add appspec.yml file in root directory
The file app/appspec.yml contains the information about where the code will be deployed and what kind of actions will be executed while deploying on the EC2,
Since This is just for demo, there’s nothing much from App side but if you want to use your own app for deployment via CICD, you
need to move the codebase into my_app/ directory and modify the scripts which will be used to start and stop as per your application needs which are located at
app/scripts/start.sh
app/scripts/stop.sh
If there is any feedback is appreciated, it will be great for me to improve and work further on this pipeline.
Thanks!