Skip to main content

Continuous Deployment of React App to AWS EC2 with Bitbucket-Pipelines


Each time you push the code in the application repository bit bucket is an automated process for deploying applications on AWS EC2 instances. This can be achieved by combining several useful tools such as AWS CodeDeploy and Bitbucket Pipelines.


AWS Console

Login in AWS console with registered email i.e. with the root. 

IAM Group

First create IAM group with AmazonS3FullAccess and AWSCodeDeployFullAccess permissions.
Step1: In AWS console, from services search IAM and goto IAM console
Step2: Goto Groups by clicking Groups in left navigation panel.
Step3: Click on Create New Group,
Step4: Enter Group Name (I have entered TestDeployGroup), click on Next Step.

Step5: Allow AmazonS3FullAccess and AWSCodeDeployFullAccess permissions by selecting them and click on Next Step.

Step6: Now review tab will open. Here are you can review the group information and now click on Create Group.

IAM User

Now create IAM user that Bitbucket can use to upload artifacts to S3 and inform CodeDeploy that a new revision is ready to be deployed.
Step1: Goto Users from left navigation panel and click on Add User.
Step2: Enter User name (I have enter TestDeployUser) and select Programmatic Access from AWS access type and click on Next: Permission.

Step3: Now select IAM group that you have created earlier (I have selected TestDeployGroup) and click on Next: Tags.
Step4: For now no tags require and click on Next: Review.
Step5: Now review section will open. You can review the user’s information, if information is correct click on Create User. 
Step6: You will get success screen where can see the user’s Access Key ID and secret access key. Make note this information. It will be required later.

IAM Role

Now create IAM role that can be associated to EC2 instances and interact with CodeDeploy.

Step1: Goto Roles from left navigation panel and click on Create Role.
Step2: Select AWS Services (EC2) since EC2 is the service that will interact with CodeDeploy and click on Next: Permission
Step3: In attach permissions policies search and select AWSCodeDeployRole and AmazonS3FullAccess. Now click on Next: Tags.
Step4: No tags require and click on Next: Review.
Step5:  Now review section will open. Enter Role Name (I have entered TestDeployRole) and review the role’s information then click on Create Role.

Step6: Goto newly created role, open Trust realtionships tab, Edit trust relationship and services should be “codedeploy.us-west-2.amazonaws.com”, “ec2.amazonaws.com” and  “codedeploy.amazonaws.com” and update Trust Policy.


S3

Now need to create bucket to store the revisions of your application. A revision just an archive of whatever is required to run your code and the AppSpec file.

Step1: Goto S3 console and click on Create bucket.
Step2: Leave default setting for Configure options, Set permissions and Review and click on Create bucket.

EC2

EC2 instance(s) need to be launched that CodeDeploy can use as deployment targets. 

Create an EC2 Instance The OS can be as per your requirement. We will be using Ubuntu Server 18.04 LTS with t2.micro instance type. 

Configuration details are all default except for IAM role, which will be the role created earlier. 


Deploy to recognize our instance it will look for tags, therefore we need to tag it so we’ll tag it say  Name = CodeDeploy. 


The security group should allow SSH and any ports you need in order to access your application.

CodeDeploy Applicatiom

The CodeDeploy agent needs to be installed on the instance. This agent is how CodeDeploy actually makes changes on the EC2 instance.

The key points with configuring EC2 is that an IAM role must be attached that allows EC2 to communicate with CodeDeploy, the CodeDeploy agent needs to be running on the instance(s), and the instance(s) needs to be tagged so that CodeDeploy can identify the instance(s) participating in deployments.

Go to AWS CodeDeploy Console and click on Create Application.


Now create deployment group with following configuration.
Enter Deployment Group Name : CodeDeployApplicationGroup
Service Role : AWSCodeDeployRole (IAM role that was created earlier)
Deployment Type : In Place
Environment Configuration : Amazon EC2 Instance with tag (Tag that was created earlier)
Deployment Settings : CodeDeployDefault.AllAtOnce
Now create deployment group.



Bitbucket

Now need to tell Bitbucket about all of this information. Set the following Environment Variables for the repository.
Pipeline

All things done and the only thing left is to build the pipeline to trigger CodeDeploy. This happens in three steps:


Deploy the revision to S3
Tell CodeDeploy that a new revision is ready
Wait for CodeDeploy to perform the deployment
The root of your project directory will look like this
bitbucket-pipelines.yml
The first step is to write the bitbucket-pipelines.yml file. There’s nothing to do but create the revision to send to S3.

pipelines:
branches:
   staging:
     - step:
         script:
           - apt-get install -y zip
           - zip -r application.zip .
           - pipe: atlassian/aws-code-deploy:0.2.7
             variables:
               AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
               AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
               AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
               APPLICATION_NAME: $APPLICATION_NAME
               S3_BUCKET: $S3_BUCKET
               COMMAND: 'upload'
               ZIP_FILE: 'application.zip'
               VERSION_LABEL: 'react-app-1.0.0'
           - pipe: atlassian/aws-code-deploy:0.2.7
             variables:
               AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
               AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
               AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
               APPLICATION_NAME: $APPLICATION_NAME
               DEPLOYMENT_GROUP: $DEPLOYMENT_GROUP
               S3_BUCKET: $S3_BUCKET
               WAIT: 'true'
               COMMAND: 'deploy'
               VERSION_LABEL: 'application-1.0.0'
               IGNORE_APPLICATION_STOP_FAILURES: 'true'
               FILE_EXISTS_BEHAVIOR: 'OVERWRITE'

 
We have created a zip archive. This archive needs to contain all of the code and libraries required to run your application because this is the archive that is ultimately deployed.

Application Specification
appspec.yml is the CodeDeploy configuration. There are several lifecycle event hooks that can be configured here, but we will only worry about three. BeforeInstall, AfterInstall and ApplicationStart.

appspec.yml 

Version: 0.0.1
os: linux
files:
  - source: /
    destination: /var/www/html
hooks:
  BeforeInstall:
    - location: scripts/before_install.sh
      timeout: 300
      runas: ubuntu
  AfterInstall:
    - location: scripts/after_install.sh
      timeout: 300
      runas: ubuntu
  ApplicationStart:
    - location: scripts/application_start.sh
      timeout: 300
      runas: ubuntu

 
before_install.sh

sudo apt-get update
sudo apt-get install nodejs -y
sudo apt-get install npm  -y
sudo npm install forever -g
 

after_install.sh

cd /var/www/ci-cd-react
sudo npm install

 
application_start.sh

sudo rm -rf /var/www/ci-cd-react/build
sudo npm run build
forever stopall
forever start -c "node -r esm" Server.js


 
Run the Pipeline
At this point we are ready to push everything to Bitbucket and start the pipeline.

After pushing changes to Bitbucket go to the pipeline section of your repository you will be able to see a pipeline triggered.

Comments

  1. I enjoy reading your blog! Thank you for writing such an informative post.
    Reference: https://webdesignvalley.com/

    ReplyDelete

Post a Comment

Popular posts from this blog

How to Speedup your WordPress site with Amazon CloudFront.

Introduction A WordPress performance is quite excellent . The number of WordPress plugins to handle performance is such evidence. But the easiest way to improve your user experience is to accelerate the entire WordPress website using CloudFront. This will help you not only improve site response time reduces the necessary infrastructure, reducing the load on the Web server, so you can reduce the total cost of the infrastructure works WordPress. CloudFront is actually a site can greatly help your site to respond to unexpected load when gained popularity. Today this post is to clarify the method of providing a reasonable standard configuration on the WordPress website or blog. How does CloudFront help? Amazon CloudFront is to improve the user's experience accessing the Web site in several ways: 1.  Anycast DNS is to ensure that customers are routed to the nearest edge location. 2.  The cached content is available to users at the edge position (i

Linux System : Free Employee Monitoring with Automatic Screenshots

Introduction :   A utomatic screen-shots  of Linux system directly import  on your web server with Linux samba server service and scrot command. 1)  Setup web panel UI on your web server :   Get web panel PHP/HTML code from Github URL: https://github.com/raj412/Employee-Monitoring-for-Linux-System It’s work in Linux server LAMP environment(no need database for this configuration ) Defult login username password is admin/1234. You can change username/passwrod from login.php file in line number #6 Login Page : Dashboard : Screenshot Page : 2)  Samba server configuration on web server : I.  Install Samba on your server where you setup Linux screenshot log system web panel. sudo apt-get update sudo apt-get install samba II.  Set a password for your user in Samba sudo smbpasswd -a <user_name> III.  Share gallery-images folder from you web panel. This is my web panel gallery-images p

How to control high traffic load on Apache servers : optimization performance of APACHE2 & PHP-FPM

Everyone handle high traffic loads  on Apache server. During down-time they forget to check server memory . Apache not used lot of memory  still server not responding. All time we restart Apache service and all things will start working good. I also faced same issue numerous time and all-time  used same solution : Restart Apache. After lots of research and reading found one solution. In this blog you see step-by-step guide to apache2 performance settings. System environment: Intel(R) Xeon(R) CPU 3.10GHz, 4 cores | 8GB RAM Ubuntu 16.04 Apache2 version using mpm_event PHP FPM (5.6,7.1) First,  Calculate process size : Download : python script file “ps_mem.py” from Github Open this and ps_mem.py file upload on you server :  https://github.com/raj412/ps_mem chmod a+x ps_mem.py sudo python ps_mem.py Output like this:  See here : 12 Apache processes, consuming a total of  35.7MiB, so each Apache process is using roughly 3MiB of RAM. The 42 php-fpm process u