AWS Code Pipeline with CloudFormation template (Part 1)

  • Published
  • Posted in AWS
  • Updated
  • 3 mins read
  • Tagged as

AWS CloudFormation is a service that allows developers to deploy AWS resources in an orderly and predictable manner. To deploy the resources, they need to be specified in a document called “template”. The resources can be written in JSON or YAML format.

CloudFormation makes it easy for developers to specify, organize, deploy resources on AWS environment along with their specifications.

By using CloudFormation templates, you can define the template on its creation and also update the same stack whenever needed without any difficulty.

In this blog, we will be deploying two CloudFormation templates. One will be launching EC2 instances and other will be to deploy a Code Pipeline. We will be trying to deploy changes made from Code Commit to EC2 instances. The pipeline will include Source, Build, Approval of changes and then Deploy them on instances.

The high level overview is as follows :

  1. The code is pushed from local repo/machine to AWS Code Commit
  2. The Code Pipeline is triggered, artifacts are stored in S3 bucket
  3. The code is deployed on development – “dev” instance
  4. Manual Approval from approving authority through email (AWS SNS)
  5. The code is deployed on production – “prod” instance

In this part of blog, we will be launching 2 EC2 instances with certain specifications. The second blog will have Code Pipeline with all it’s Stages.

Let’s Start.

Pre-requisites :

  • Key-Pair : You need to create key-pair from AWS console. So you will be able to download the .pem file to connect the instance from console.

I have create 2 key-pairs in us-east-2 region: cfn-key-1.pem and cfn-key-2.pem.

  • Create a S3 bucket
  • Create a Code Commit repository as update it with your code.

Services used :

  1. AWS CloudFormation
  2. AWS Code Commit
  3. AWS Code Build
  4. S3 bucket
  5. AWS Code Deploy
  6. AWS SNS
  7. AWS EC2
  8. AWS Code Pipeline
  • Create key-pairs for EC2 instance

We need to create 2 key pairs for two instances. The key-pairs can be downloaded only while its creation from Console. We need key-pairs to connect to instances.

  • Create a S3 Bucket

We will be needing S3 bucket to store the Artifacts.

  • Create Code Commit repository.

The source for the Pipeline will be Code Commit. We need to create it and host the code.

The code contains a sample welcome page in index.html.

  • EC2-Stack template

Now we will launch a CloudFormation template to deploy 2 EC2 instances, named as “dev” and “prod”

To access the files, click here (link behind “here” :

The EC2-stack template is named : ec2

And Pipeline stack template is named : CF

Next navigate to CloudFormation console and upload the stack template.

Upload the template.

Select “cfn-key-1” from drop-down and proceed. Rest settings keep default and proceed to next page.

NOTE : Please note the key-pairs must be created in the same region where you want to launch the template.

Wait for the resources to get created. Once created, you will get message as follows :

We can check the same in Instances console.

IAM Role Assigned to Instance specified in template.

The instances are launched with ruby, wget, nginx, codedeploy-agent installed in it. The required cmds are specified in “User Data” when deploying instances through template.

Security Group of Instances

In next part, we will launch the template to deploy AWS Code Pipeline with CloudFormation template.


Addend Analytics

Addend Analytics is a Microsoft Gold Partner based in Mumbai, India, and a branch office in the U.S.

Addend has successfully implemented 100+ Microsoft Power BI and Business Central projects for 100+ clients across sectors like Financial Services, Banking, Insurance, Retail, Sales, Manufacturing, Real estate, Logistics, and Healthcare in countries like the US, Europe, Switzerland, and Australia.

Get a free consultation now by emailing us or contacting us.