AWS Infrastructure as Code - Cloudformation

 AWS Infrastructure as Code - Cloudformation


In AWS cloudformation we have to know 3 important concept:

  • Stack
  • Template
  • ChangeSet
Let's give simple definition to these but it will be clear with hands on example.

Template - is a yaml or json file we create. This file serves a purpose of containing the infrastructure of code (also known as Infrastructure as Code - IaC). This means this file provides the directions to AWS what to create and how to connect building blocks.

Stack - on the other hand is collection of AWS resources, you can use a single unit.

ChangeSet - allows you to see changes before applying into stack.

Let's create a simple stack using template :

The template below will create simple webpage using user-data and EC2, will allow connections for ports 22,80 

Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
AvailabilityZone: eu-north-1a
ImageId: ami-05edb7c94b324f73c
InstanceType: t3.micro
SecurityGroups:
- !Ref MySecurityGroup
UserData:
Fn::Base64: |
#!/bin/bash
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
cd /var/www/html
echo "This EC2 instance was launched by AWS CloudFormation!" > index.html
MySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SSH on Port 22 and Web on Port 80
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 22
IpProtocol: tcp
ToPort: 22
- CidrIp: 0.0.0.0/0
FromPort: 80
IpProtocol: tcp
ToPort: 80





  1. Let's first open Cloudformation







  2. Create stack 





  3. In the code block above please check these 2 parameters, AMI can be taken from EC2 instance initialization page as well as instance type.









  4. After successful creation, you will be able to see your html page in public IP address assigned to that instance you just created through Cloudformation.


  5. You can also keep track of process itself .





Comments

Popular posts from this blog

S3 - Create a static website

AWS - Databases DynamoDB, RDS

S3