We are creating an EC2 Instance with Ruby installation Using AWS CloudFormation. Using this we can set up web-application architecture in the AWS cloud. you can add more steps according to requirements like( git install, clone project from git, run deployment script, Nginx setup, docker install, and many more…)
Using this we can save time and money. Easy to setup new server.
What is AWS Cloudformation?
AWS CloudFormation is a service that gives developers and businesses an easy way to create a collection of related AWS and third-party resources, and provision and manages them in an orderly and predictable fashion. We will be using Mappings, Resources, and Outputs for this project.
Here are the core components that we are going to create:
Web Server Security Group
EC2 instance with Ubuntu 20.04 image and 8Gb Ebs Volume and Ruby installation.
For creating VPC and Subnet please check the below article, in this article we will be creating an EC2 instance with Ruby Installation.
Create a VPC with private and public subnets using CloudFormation
The Mappings section is basically a lookup table using key: value relationships. In this template, the Amazon Machine Image is mapped to its respective Region. I am using only one region mapped for this project, so the template is restricted to this one region and would fail if launched outside of these regions.
Use the optional Parameters section to customize your templates. Parameters enable you to input custom values to your template each time you create or update a stack.
Change your key name.( First_EC2 => Your_key_name )
The Resources section includes all the AWS resources that you want to create in the stack.
- For WebserverSecurityGroup, the template is creating a security group with an inbound rule allowing all traffic on HTTP.
- For EC2Instance, the template is creating a single EC2 Instance with the following properties:
ImageID: You could directly put in the AMI-Id for this property, but since we want the ability to use the template in one region we use the FindInMap function to reference our Mappings.
UserData: This section is used to install Ruby using CloudFormation::Init
Use the AWS::CloudFormation::Init type to include metadata on an Amazon EC2 instance
EC Instance Template
The Outputs section prints information to the Outputs tab in CloudFormation after the stack is created and can also be used to import information into other stacks.
Complete CloudFormation Template:
RVM and Ruby Default installation output.
Note, With the
DeletionPolicythe attribute you can preserve, and in some cases, backup a resource when its stack is deleted. You specify a
DeletionPolicyattribute for each resource that you want to control. If a resource has no
DeletionPolicyattribute, Amazon CloudFormation deletes the resource by default. If you Want to keep your all resource then add
DeletionPolicy: Retainin each resource
For more details: AWS doc
Using this template you can create VPC with the public, and private subnets, Web Server Security Group and launch EC2 instance with Ruby installation, using AWS CloudFormation. It builds a private networking environment in which you can securely run AWS resources, along with related networking resources.
If this guide has been helpful to you and your team please share it with others!