Continuous Delivery Pipeline Using Terraform, Jenkins, Github and Deploy On AWS
Continuous Delivery Pipeline Using Terraform, Jenkins, Github and Deploy On AWS
terraform_codedeploy
Steps to set up :
1. Install Terraform
2. Set up AWS CLI (Optional)
Why Optional :
This project have 'main.tf' file in 'dev' folder which consist the provider as "aws" and
region as "us-east-1" by default. As for orchestration on aws through Terraform program
either you need to set up the AWS User (Admin console user) using "provider" block,
where you will have to define your AWS Key and AWS Secret Key for that user, but that
is not safe.
The better option is to set up AWS CLI and define your user's Key and Secret key inside
your terminal itself using AWS CLI which is more secure option than defining them
openly in main file. So that it will stay encapsulated.
Develop Jenkins Pipeline & Configure Jenkins for AWS Code Deploy :
First and foremost, you may require (if the job is not already configured) to create a Jenkins
freestyle project/job to configure with application code base integration (Git) and post build
action configuration.
This will create job successfully and it will open the job configuration page consisting of
different stages e.g. General, SCM — Source Code Management, Build Triggers, Build and Post
Build Actions. Below video will guide you how to execute this all steps.
SCM :
First step here is SCM — Source Code Management, you can add the source code Github SSH
URL into the SCM => Git
Build Triggers :
Continuing to next step which is Jenkins Build Triggers. Basically, build trigger is the main part
as far as the pipeline is concern why, because it listens to different events and based on that
Jenkins triggers the Build phase. Although, as an admin you will have to configure the Build
triggers for your build and there are various options to configure in that. In our case, we will use
Webhook to trigger build from Git Source commit. Below is the high level steps how you can
configure Webhook in your Git Repo.
1. GitHub hook trigger for GITScm polling (Option) — only if you want jenkins to keep polling
on Git server for any commit change
Build :
Now as part of the next step, select Build phase and select Execute shell script. As we are
building and deploying a NodeJs application we will add a phase where the project dependencies
should get installed and unit test cases also should automatically get executed. As a result, we
will be adding 2 commands in the added shell script section as mentioned below.
1. npm install
2. ./script/test
Add AWS CodeDeploy phase from the dropdown menu and if you are not able to find, the
plugin you have installed in Step 4 is not installed properly. Once you will add that phase in to
post build actions that will consist of a few fields which will help Jenkins to link with AWS EC2
instances. Below are the information you can add in that.
1. AWS CodeDeploy Application Name (which is your application name which you have created
on AWS EC2)
2. AWS ClodeDeploy Deployment Group (which is the Deployment Group within that
application)
3. AWS CodeDeploy Deployment Config (which you can find in the Deployment Group detail
page on AWS)
4. Region
5. S3 Bucket name
Now the main part is authenticate this hand shaking as Jenkins will try to connect with AWS
EC2 instances. Using AWS Access/Secret keys, to see how follow below steps
Now, to create our project in Jenkins we need to configure the required Jenkins plugin.
1. Sign in to Jenkins with the user name and password that you created earlier and click on
Manage Jenkins then Manage Plugins.
2. From the Available tab search for and select the below plugins then choose Install
without restart:
.
AWS CodeDeploy
AWS CodeBuild
Http Request
File Operations
.
3. Select the Restart Jenkins when installation is complete and no jobs are running.
Jenkins will take couple of minutes to download the plugins along with their
dependencies then will restart.
5. Enter a name for the project (for example, CodeDeployApp), and choose OK.
.
6. On the project configuration page, under Source Code Management, choose Git. For
Repository URL, enter the URL of your GitHub repository.
.
.
7. For Build Triggers, select the Poll SCM check box. In the Schedule, for testing enter
H/2 * * * *. This entry tells Jenkins to poll GitHub every two minutes for updates.
.
.
8. Under Build Environment, select the Delete workspace before build starts check box.
Each Jenkins project has a dedicated workspace directory. This option allows you to wipe
out your workspace directory with each new Jenkins build, to keep it clean.
.
9. Under Build Actions, add a Build Step, and AWS CodeBuild. On the AWS
Configurations, choose Manually specify access and secret keys and provide the keys.
.
.
10. From the CloudFormation stack Outputs tab, copy the AWS CodeBuild project name
(myProjectName) and paste it in the Project Name field. Also, set the Region that you
are using and choose Use Jenkins source.
It is a best practice is to store AWS credentials for CodeBuild in the native Jenkins
credential store. For more information, see the Jenkins AWS CodeBuild Plugin wiki.
.
11. To make sure that all files cloned from the GitHub repository are deleted choose Add
build step and select File Operation plugin, then click Add and select File Delete.
Under File Delete operation in the Include File Pattern, type an asterisk.
.
.
c. Copy the S3 bucket name from the CloudFormation stack Outputs tab and paste
it after (http://s3-eu-central-1.amazonaws.com/) along with the name of the zip
file codebuild-artifact.zip as the value for HTTP Plugin URL.
Example: (http://s3-eu-central-1.amazonaws.com/mybucketname/codebuild-
artifact.zip)
13. Under HTTP Request, choose Advanced and leave the default values for
Authorization, Headers, and Body. Under Response, for Output response to file, enter
the codebuild-artifact.zip file name.
.
14. Add the two build steps for the File Operations plugin, in the following order:
a. Unzip action: This build step unzips the codebuild-artifact.zip file and places the
contents in the root workspace directory.
b. File Delete action: This build step deletes the codebuild-artifact.zip file, leaving
only the source bundle contents for deployment.
.
15. On the Post-build Actions, choose Add post-build actions and select the Deploy an
application to AWS CodeDeploy check box.
16. Enter the following values from the Outputs tab of your CloudFormation stack and leave
the other settings at their default (blank):
d. For AWS Region, choose the Region where you created the CodeDeploy
environment.
Choose Deploy Revision. This option registers the newly created revision to your
CodeDeploy application and gets it ready for deployment.
Select the Wait for deployment to finish? check box. This option allows you to
view the CodeDeploy deployments logs and events on your Jenkins server
console output.
.
.
Now that you have created a project, you are ready to test deployment.