Testing CloudFormation Templates
By: Suleman Hasib
Tags: Jenkins, AWS, Cloudformation, Automation Testing, Pipeline

Setup AWS CLI Auth from Jenkins User

To get taskcat working we need to authenticate AWS CLI from within Jenkins user there are two ways to do this. 1) aws configure. 2) assign a role to AWS ec2 instance with appropriate permissions. For this tutorial, I am going to use aws configure.

su — jenkins

aws configure

aws utility will ask you for the region, access_key, secret_key, and output format provide this information according to your user/setup.

Setting up taskcat and Jenkins Pipeline

My Project structure

It is something like where we have multiple stacks in one project/git repo. Each stack has its own directory and all files related to one stack reside in their respective stack directory.

stack1

|

- cloudformationtemplate.yml

|

- .Jenkinsfile

|

- .taskcat.yml

stack2

|

- stack2_cloudformationtemplate.yml

As we already installed taskcat version 0.9.23 we can proceed with taskcat configuration and Jenkins descriptive file.

.taskcat.yml

This file can be created in several locations but I created this at the stack root/directory.

general: parameters: UsersCount: 1 KeyName: yourkey Timezone: Asia/Karachi UserEmailList: suleman.hasib@gmail.com



CiTest: 'true' project: name: test-ci regions: - eu-west-1 shorten_stack_name: true tests: default: template: ./cloudformationtemplate.yml

Let me explain the above code snippet.

The cloud formation template I am using requires some parameters to be explicitly defined the general section provides just those. There are several parameters which in my use-case can be set to default within the cloud formation template. This reduces the effort on .taskcat.yml.

CiTest is a parameter which describes either we want to execute tests inside UserData.

The second section is project this section has a name which can be whatever you like. And regions should be the ones where you want to deploy/test your cloud formation templates. I named it test-ci, and I wanted to deploy/test only in the eu-west-1 region, i.e. Ireland.

I also set the option to shorten stack names to true because some resources in cloud formation take stack name as part of their naming convention and if the stack name is lengthy cloud formation template deployment fails.

And the last section in the above yml is tests where we define different tests and their respective configurations. I am only defining a simple test by name default and it's using a template file located in the project root ./cloudformationtemplate.yml

I have fairly simple .taskcat.yml but there are huge configurations available. For a complete example with comments check here.

Setup Descriptive Pipeline Jenkinsfile

pipeline { agent any options { ansiColor('xterm') } stages { stage('Test') { when { changeset "stack1/*"} steps { sh ''' cd TestSuite taskcat test run || true aws s3 sync taskcat_outputs s3://taskcatresults/${JOB_NAME}-${BUILD_ID} --acl public-read echo https://taskcatresults.s3-eu-west-1.amazonaws.com/${JOB_NAME}-${BUILD_ID}/index.html ''' } } } }

Handling Linting Errors

If you’re getting linting errors and you know that this is an error that you can ignore. Add MetaData to each resource in the cloud formation template where you want to ignore the error.