Everything As A Code: Cloud Formation (ECS Fargate) Template Through Jenkins
This tutorial demonstrates how, with just the Jenkins Master and codbase, you can create a pipeline code for a customized Dockerfile.
Join the DZone community and get the full member experience.Join For Free
This article is an example of “Everything as a code” to create an ECS Fargate Stack in AWS using the Cloud Formation template through Jenkins.
Only Jenkins Master and codebase is required to check this flow. Check out the Github URL mentioned below to refer to the code.
You may also enjoy: The Best of Both Worlds With AWS Fargate
There is some Jenkins master node configuration required to trigger the pipeline.
Configure the Docker Installation in Jenkins > Manage Jenkins > Global Tool Configuration > Docker.
Need to store the AWS Credentials on Jenkins > Credentials > System > Global Credentials > Add Credentials
Then it will look like this under Jenkins > Credentials:
Be Familiar with Files and Folders
In the code repository, there are some files and folder as below.
- Templates folder — contains the YAML file which has Cloud Formation code to create the ECS Fargate.
- Parameters folder — contains the JSON file and it contains all the parameters which are required Cloud Formation YAML
- Dockerfile — A customized Dockerfile is created which has capabilities of AWS CLI and Cloud Formation Linting; no need to install these on Jenkins Master specifically.
- Jenkinsfile — The Jenkins Pipeline which will trigger all the stages like “Poll SCM,” “Validating CF template and JSON,” and “ Cloud Formation Stack creation.”
There are so many approaches to achieve the same task. The first few of the CLI (AWSCLI and CFN-LINT) need to be installed on Jenkins Master or many Jenkins Plugins are available to trigger the ECS Fargate parameters.
Here, an approach is taken where code will take off everything and Jenkins Pipeline is written completely based on code.
A modular approach is taken for Cloud Formation code where templates and parameters are kept in the separate directory, so it will be easy to main the CF code better.
A customized Dockerfile will take care of the AWS CLI and CFN-LINT; no need to install these on Jenkins Master.
Jenkinsfile has 3 stages:
- Fetching code from the remote Git repository.
- Validating the CF template and JSON file before creating the ECS Fargate stack. The CF template will be validated using CFN-LINT running inside the container. However, the Python “json.tool” module is used to validate the JSON file. If there is a problem with the CF template and JSON file validation, then this stage will be failed.
- Here, ECS Fargate stack creation will be triggered, the ID will be successful, then it will return a stack ID.
Dockerfile code snapshot:
Jenkinsfile code snapshot:
Create a Pipeline Job in Jenkins and execute the job. It will create the ECS Fargate stack in AWS.
However, this customized Dockerfile is not only for creating ECS Fargate stack, but even other commands can also be triggered which are working fine with the AWSCLI.
It will show all the EC2 instances in the AWS account and the output will be displayed in tabular format.
Region name for AWS also can be passed using an argument like
“Learning never exhausts the mind.” ― Leonardo da Vinci
Opinions expressed by DZone contributors are their own.