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 :
- The code is pushed from local repo/machine to AWS Code Commit
- The Code Pipeline is triggered, artifacts are stored in S3 bucket
- The code is deployed on development – “dev” instance
- Manual Approval from approving authority through email (AWS SNS)
- 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.
- 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 :
- AWS CloudFormation
- AWS Code Commit
- AWS Code Build
- S3 bucket
- AWS Code Deploy
- AWS SNS
- AWS EC2
- 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”
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.