Everything As A Code: Cloud Formation (ECS Fargate) Template Through Jenkins – DZone DevOps | xxxEverything As A Code: Cloud Formation (ECS Fargate) Template Through Jenkins – DZone DevOps – xxx
菜单

Everything As A Code: Cloud Formation (ECS Fargate) Template Through Jenkins – DZone DevOps

一月 31, 2020 - MorningStar

Over a million developers have joined DZone.

{{announcement.body}}

{{announcement.title}}

Let’s be friends:

1024)” dc-slot=”ads.sl1.slot(articles[0], 0)” tags=”ads.sl1.tags(articles[0], 0)” size=”ads.sl1.size(articles[0], 0)” style=”border:0px;”>
1 && !articles[0].partner.isSponsoringArticle && (width > 1024)” dc-slot=”ads.sb2.slot(articles[0], 0)” tags=”ads.sb2.tags(articles[0], 0)” size=”ads.sb2.size(articles[0], 0)”>

Everything As A Code: Cloud Formation (ECS Fargate) Template Through Jenkins

DZone ‘s Guide to

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.

Everything As A Code: Cloud Formation (ECS Fargate) Template Through Jenkins - DZone DevOps by

CORE ·

Mar. 11, 20 · DevOps Zone ·

Free Resource

Join the DZone community and get the full member experience.

Join For Free

Introduction

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

Jenkins Configuration

There is some Jenkins master node configuration required to trigger the pipeline. 

Docker Installation

Configure the Docker Installation in Jenkins > Manage Jenkins > Global Tool Configuration > Docker.

Everything As A Code: Cloud Formation (ECS Fargate) Template Through Jenkins - DZone DevOps

Fig 1: Docker Installation configuration in Jenkins Master

AWS Credentials

Need to store the AWS Credentials on Jenkins > Credentials > System > Global Credentials > Add Credentials

Everything As A Code: Cloud Formation (ECS Fargate) Template Through Jenkins - DZone DevOps

Fig 2: AWS Credentials configuration in Jenkins

Then it will look like this under Jenkins > Credentials:

Everything As A Code: Cloud Formation (ECS Fargate) Template Through Jenkins - DZone DevOps

Be Familiar with Files and Folders 

In the code repository, there are some files and folder as below.

Everything As A Code: Cloud Formation (ECS Fargate) Template Through Jenkins - DZone DevOps

  1. Templates folder — contains the YAML file which has Cloud Formation code to create the ECS Fargate.
  2. Parameters folder — contains the JSON file and it contains all the parameters which are required Cloud Formation YAML
  3. 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.
  4. Jenkinsfile — The  Jenkins Pipeline which will trigger all the stages like “Poll SCM,” “Validating CF template and JSON,” and “ Cloud Formation Stack creation.”

Approach 

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: 

  1. Fetching code from the remote Git repository.
  2. 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.
  3. Here, ECS Fargate stack creation will be triggered, the ID will be successful, then it will return a stack ID.

Dockerfile code snapshot:

Dockerfile

 

xxxxxxxxxx
1

15

 

1

FROM alpine:3.8

2

# Default evironment variables.

3

ENV AWS_DEFAULT_REGION us-east-1

4

ENV AWS_ACCESS_KEY_ID aws_key_id

5

ENV AWS_SECRET_ACCESS_KEY aws_Secret_key

6

COPY templates/New_ECS_Fargate_Para.yml .

7

COPY parameters/parameters.json .

8

RUN apk add -q --update /

9

    && apk add -q /

10

FROM alpine:3.8

0

11

FROM alpine:3.8

1

12

FROM alpine:3.8

2

13

FROM alpine:3.8

3

14

FROM alpine:3.8

4

15

FROM alpine:3.8

5

Jenkinsfile code snapshot:

Groovy

 

FROM alpine:3.8

6

1

32

 

1

FROM alpine:3.8

7

2

FROM alpine:3.8

8

3

FROM alpine:3.8

9

4

# Default evironment variables.

0

5

# Default evironment variables.

1

6

# Default evironment variables.

2

7

# Default evironment variables.

3

8

# Default evironment variables.

4

9

# Default evironment variables.

5

10

# Default evironment variables.

6

11

# Default evironment variables.

7

12

# Default evironment variables.

8

13

# Default evironment variables.

9

14

ENV AWS_DEFAULT_REGION us-east-1

0

15

ENV AWS_DEFAULT_REGION us-east-1

1

16

ENV AWS_DEFAULT_REGION us-east-1

2

17

ENV AWS_DEFAULT_REGION us-east-1

3

18

ENV AWS_DEFAULT_REGION us-east-1

4

19

ENV AWS_DEFAULT_REGION us-east-1

5

20

ENV AWS_DEFAULT_REGION us-east-1

6

21

ENV AWS_DEFAULT_REGION us-east-1

7

22

ENV AWS_DEFAULT_REGION us-east-1

8

23

ENV AWS_DEFAULT_REGION us-east-1

9

24

ENV AWS_ACCESS_KEY_ID aws_key_id

0

25

ENV AWS_ACCESS_KEY_ID aws_key_id

1

26

ENV AWS_ACCESS_KEY_ID aws_key_id

2

27

ENV AWS_ACCESS_KEY_ID aws_key_id

3

28

ENV AWS_ACCESS_KEY_ID aws_key_id

4

29

ENV AWS_ACCESS_KEY_ID aws_key_id

5

30

ENV AWS_ACCESS_KEY_ID aws_key_id

6

31

ENV AWS_ACCESS_KEY_ID aws_key_id

7

32

ENV AWS_ACCESS_KEY_ID aws_key_id

8

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. 

For example:

Shell

 

ENV AWS_ACCESS_KEY_ID aws_key_id

9

1

 

1

ENV AWS_SECRET_ACCESS_KEY aws_Secret_key

0

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  -e “region-name”.

Find the code repository in Github here CF_ECS_FARGATE_Jenkins, and check out my DZone profile here.

Keep learning!!!

“Learning never exhausts the mind.” ― Leonardo da Vinci

Further Reading

Using CloudFormation to Set Up Scalable Apps

A Model-Based Approach to CD: From ”Everything as Code" to "Everything is a Model"

Topics:
devops ,dockerfile ,jenkins ,cloudformation ,aws ,aws ecs ,cloud ,everything as code

Opinions expressed by DZone contributors are their own.

DevOps Partner Resources

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}

· {{ parent.articleDate | date:’MMM. dd, yyyy’ }} {{ parent.linkDate | date:’MMM. dd, yyyy’ }}


Notice: Undefined variable: canUpdate in /var/www/html/wordpress/wp-content/plugins/wp-autopost-pro/wp-autopost-function.php on line 51