0% found this document useful (0 votes)
340 views14 pages

Continuous Delivery Pipeline Using Terraform, Jenkins, Github and Deploy On AWS

This document provides steps to set up a continuous delivery pipeline using Terraform, Jenkins, GitHub, and AWS CodeDeploy. The steps include: 1. Installing Terraform and optionally setting up AWS CLI 2. Cloning the GitHub repository containing Terraform configuration files 3. Running Terraform commands to provision the AWS CodeDeploy infrastructure 4. Configuring a Jenkins job to build and deploy code upon GitHub commits using the AWS CodeDeploy plugin 5. Configuring the job to unzip artifacts, delete files, and trigger CodeDeploy revisions upon build completion

Uploaded by

office mails
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
340 views14 pages

Continuous Delivery Pipeline Using Terraform, Jenkins, Github and Deploy On AWS

This document provides steps to set up a continuous delivery pipeline using Terraform, Jenkins, GitHub, and AWS CodeDeploy. The steps include: 1. Installing Terraform and optionally setting up AWS CLI 2. Cloning the GitHub repository containing Terraform configuration files 3. Running Terraform commands to provision the AWS CodeDeploy infrastructure 4. Configuring a Jenkins job to build and deploy code upon GitHub commits using the AWS CodeDeploy plugin 5. Configuring the job to unzip artifacts, delete files, and trigger CodeDeploy revisions upon build completion

Uploaded by

office mails
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Continuous delivery pipeline using

Terraform,Jenkins, Github and deploy on


AWS
Pre-requisites :
1. Jenkins must be installed on your local machine or remote machine (VM)
2. You must have basic knowledge about how to use Jenkins and its different plugins
3. A GitLab/Github account
4. A gmail account which will be utilised for post build notification confirmation to the team about build
status

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.

3. Clone the repository


4. Open terminal to the clonned repository path
5. Open dev folder
6. cd tf_aws_codedeploy/dev
7. Initialize Terraform
8. terraform init
9. Check the plan insights what are the resources to be orchestrated
10. terraform plan
11. Execute the plan
12. terraform apply
This process may take afew minutes and once the process will be completed, your AWS
CodeDeploy orchestration will be up and running and it will be ready for any application
deployment.

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.

Below are the steps :

1. GoTo Jenkins dashboard

2. Select New Item

3. Select Freestyle Project

4. Name the Project

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. GoTo your GitLab/Github Repo Settings

2. Select Webhook in case of Github

3. Select Integrations in case of GitLab


4. Enter URL
http://IP_WHERE_JENKINS_SETUP:PORT/project/FREESTYLE_PROJECT_NAME/

5. Save the configuration

Furthermore, GoTo jenkins Build Triggers and select below options.

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

Post Build Actions :

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

 Using AWS Access/Secret Keys :


Create a project and configure the CodeDeploy Jenkins plugin

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.

4. Login then choose New Item, Freestyle project.

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.
.
.

12. Under Build, configure the following:

a. Choose Add a Build step.

b. Choose HTTP Request.

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)

d. For Ignore SSL errors?, choose Yes.


.
.

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):

a. For AWS CodeDeploy Application Name, enter the value of


CodeDeployApplicationName.

b. For AWS CodeDeploy Deployment Group, enter the value of


CodeDeployDeploymentGroup.

c. For AWS CodeDeploy Deployment Config, enter


CodeDeployDefault.OneAtATime.

d. For AWS Region, choose the Region where you created the CodeDeploy
environment.

e. For S3 Bucket, enter the value of S3BucketName.


The CodeDeploy plugin uses the Include Files option to filter the files based on
specific file names existing in your current Jenkins deployment workspace
directory. The plugin zips specified files into one file. It then sends them to the
location specified in the S3 Bucket parameter for CodeDeploy to download and
use in the new deployment.
.
As shown below, in the optional Include Files field, I used (**) so all files in the
workspace directory get zipped.
.

 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.

You might also like