Deploy CloudFormation stack using Jenkins

In this post, we are going to deploy Cloud Formation Stack using Jenkins FreeStyle project.

Cloud Formation Stack we are going to use is available here: EC2Stack

Download the stack file and save it in your s3 bucket as we are using the s3 bucket URL as a template-body in cloud formation cli.  

Follow the below steps:

Step1: Go to Jenkins Dashboard and click on “New Item”, New screen will appear on that enter the item name say “CFNStackDeploy” and select Freestyle Project and then click on “OK”

Step2: Now under the “General” section select the check box “This project is parameterized” and then add “String Parameter”. Now provide Name, Default Value, And Description, here we have given Name as “StackName”, Value as “ExampleStack” and Description as “Provide Stack Name”

This parameter we are going to invoke in the shell while executing the stack command under the build section.

Step3: Now under the “Build Environment” section, select the check box “Use secret text(s) or file(s)” and add “secret text”. Here provide the Variable names and select Credentials Values as shown in the image. 

Click on this link to know how to save AWS credentials in Jenkins.

Step4: Now under the “Build” section add a build step to “Execute Shell” and copy the below command as given. And Then Click on “Apply” and “Save”

#aws cloudformation create-stack --stack-name ${StackName} --template-body https://<bucket-name> --parameters ParameterKey=MyKeyName,ParameterValue=Mumbai ParameterKey=SecurityGroupIds,ParameterValue=sg-xxxxxxxxxx ParameterKey=MyAvailabilityZone,ParameterValue=ap-south-1a ParameterKey=MySubnetId,ParameterValue=subnet-xxxxxxxxxx --region ap-south-1 

Modify the above command as per your configuration. Change s3 bucket URL, change Keyname, security group id, availability zone, subnet id, region to deploy.

Step5: Now go to the project and click on “Build with Parameter”, provide stack name “Anything”, then click on build.

Go to the Console Output to see the build status.

Also, verify CloudFormation dashboard whether the stack is deployed successfully. And verify the instance launched.

This is how we have configured our Freestyle Jenkins Job using Parameters, Secret text credentials, Shell command.

No comments:

Post a Comment