Continuous Integration and Delivery pipeline using Bitbucket, AWS CodeBuild, AWS CodePipline and AWS CodeDeploy - Part 2

In the previous post we looked at how to configure AWS Codebuild for CI/CD pipeline.
In this post we will look at configuring AWS CodeDeploy.
First Let's see what is CodeDeploy.
According to Amazon Web Services website, 
AWS CodeDeploy is a fully managed deployment service that automates software deployments to compute services such as Amazon EC2, AWS Lambda, and your on-premises servers.
So now let's see how we can set up AWS CodeDeploy.
Once you login to your AWS account, go to AWS CodeDeploy and click on "Create Application".
You need to first provide Application name (such as "MyFirstDeployment") and choose your compute platform (EC2, AWS Lambda, Amazon ECS). Here we will choose EC2 Instance as our comput platform.



Then click "Create Application".

Once the application is created you must create a deployment group. You can have multiple deployment group per application. Deployment group allows to have different deployment settings per different environment such as Production, Staging,Development or different deployment settings per different types of application in save environments such as Frontend Web application vs backend microservices.
Click  on the "Create Deployment Group" button.
Here first provide the deployment group name and choose an existing service role which has required deployment permissions (such as EC2 access permission, cloudwatch log creation permission, S3 permission if you are going to download artifacts from S3).



Now, in the "Deployment Type" section choose how you want to deployment to happen. "In-place" means every time you deploy the application, previous version of the application will be removed and new one will be deployed. This means that your application will not be available during the duration of the deployment.
In the "Blue/Green" deployment, each revision of the application is deployed to a different instance(existing or new), which then brought online after the deployment. Existing instance keeps running during the course of deployment and taken offline after the deployment. This way your application is always available even during the deployment.



Here we will stick to In-Place deployment for now.
After this you need to provide the Tags to select the environment in which you want to deploy.



Next you need to provide Deployment Settings and if you use Load Balancer.



We can skip the Trigger and Alarm section and create the deployment group.

In next post we will look at how to setup the AWS Codepipeline and connect the AWS CodeBuild, which we setup previously and AWS CodeDeploy which we setup in this post.






Read Part 3: Configuring AWS CodePipeline

Enjoy!!

Continuous Integration and Delivery pipeline using Bitbucket, AWS CodeBuild, AWS CodePipline and AWS CodeDeploy - Part 1

In this post I am going to show you how to develop a continuous integration and continuous delivery pipeline using AWS CodeBuild, AWS CodePipline and AWS CodeDeploy. I will use bitbucket as our source repository. But any other repository such as Github or Gitlab also can be used. Ofcourse, certain steps may defer as AWS Code Pipeline can pull the code directly from Github, but not from bitbucket.

So here are the steps:

1. Configure AWS CodeBuild to build the code by directly pulling from BitBucket and upload the build artifacts in S3.
2. Configure AWS CodeDeploy to pull the build package from S3(configured in above step) and deploy the application.
3. Configure AWS CodePipeline to get the build artifacts from S3 and deploy them using the AWS CodeDeploy application configured in step 2.


This will be three part series, and in this part-1 we will see how to configure AWS CodeBuild.

Part 2: Configuring AWS Code Deploy
Part 3: Configuring AWS CodePipeLine

Step 1: Configure AWS CodeBuild

Login to your AWS console and go to AWS CodeBuild.
Most of the steps to create a CodeBuild project are self explainatory. So here I will mention here the critical steps that you may want to get right.
Under the Source section, choose BitBucket and select "Repository In My BitBucket Account.".
Now choose your "BitBucket Repository" in which you have your code to build.
Now under the "Primary source webhook Events", check the box named "Rebuild every time a code change is pushed to this repository". Additional options will be available where you can configure the events on which code will be built.
For example, you can select whether the code should be built on every push, or on every pull request created or every pull request updated.
As depicted in below screenshot, I have configured my build project to build on everey push on the dev branch, but not to build when any tag is created or updated.



 Now it is time to configure the Environment under which the code is built. Here you have to select the whether you want to use AWS provided Image ("AWS Managed docker images") or custom image ("custom docker image"). 
For most of the common programming language runtimes and environments(such as dotnet, php, nodejs, java, golang) AWS provides, so choose "Managed Image", and then choose the operating system. Here, I have selected Ubuntu, Standard and aws/codebuild/standard:2.0 as Operating System, Runtime and Image respectively.



Now, as with any AWS service, you have to select a service role so that CodeBuild can build you project and upload the artifacts to S3 or use any other AWS services required.




Under the "Additional Configuration" section you can select timeout and specify if your build requires certificates, connection to VPC and compute requirements. Also, you can specify the Environment Variables here. Environment variables are helpful if you want to include custom build step depending on the environment or naming the build artifact based on the environment.




Now, comes the most important step, which is BuildSpec. You can write the commands to build your project using the BuildSpec file. In this section you can choose if build commands are included in a file (name buildspec.yml) in your project, or you can specify the build commands directly in the editor provided by AWS console.




Below is sample of the buildspec.yml, which includes commands to build nodejs project and package it as zip file.


As you can see above, in the first few lines, I provide the runtime environment(here, nodejs, version 10), in the "runtime-version".

In the build section, I have provided commands to build the project. For this node project, after running npm install and npm run build, I am copying the "node_module" folder to the "dist" folder, which contains all other project files except the node_modules folder.
In the artifacts section, I have specified the name of the zip file which contains all the file I want to include in the package. The artifacts section also allows me to specify the files I want to include or exclude in the package. Here I am specifying all the content of the dist folder, which is the output of the build commands mentioned above. I have included appspec.yml and deployment scripts which will be usefull for deploying the application. We will them later in the section about AWS CodeDeploy.

Next comes the Artifacts section, where you can provide the details about the S3 location where your artifacts will be saved.




As shown above, you have to select the S3 bucket(which should be pre-existing) where you want to save the artifacts and the name of the artifact zip file. You will notice that the artifact zip file name matches with the name I specified in the artifact section of the buildspec.yml file.

The Path option is the folder name in the S3 bucket where your artifact will be saved. So in this case, the artifact will be save as "my-codebuild-artifact-1/dev/my-api-dev.zip".

Once this configuration is done, you can click the "Create Build Project" button and your CodeBuild project will be created. Now start the build and once it is finished, you will see your artifact is saved in the above mentioned path.


Enjoy!!