Launch an EC2 Instance with Ruby installation Using AWS CloudFormation
Tags: ruby, aws, cloudformation, productivity
Published: 2022-09-09 01:54:52


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](https://dev.to/kanani_nirav/create-a-vpc-with-private-and-public-subnets-using-cloudformation-2m6a)**

**Mappings:**

The [Mappings](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html) 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.
{% gist https://gist.github.com/kananinirav/c0dcaf0d9aca33a01d4fbdbfeaf96812.js %}
**Parameters:**

Use the optional [Parameters](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html) section to customize your templates. Parameters enable you to input custom values to your template each time you create or update a stack.
{% gist https://gist.github.com/kananinirav/645601ed0bb20bdc1149cd6d662756a4.js %}
Change your key name.( First_EC2 => Your_key_name )

**Resources:**

The [Resources](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html) section includes all the AWS resources that you want to create in the stack.

 1. For *WebserverSecurityGroup*, the template is creating a security group with an inbound rule allowing all traffic on HTTP.
{% gist https://gist.github.com/kananinirav/1503ca635210a0f1635c7d3dc3877367.js %}
2. 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](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html) to reference our Mappings.

* UserData: This section is used to install Ruby using [CloudFormation::Init](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html)
>  **AWS::CloudFormation::Init**
Use the AWS::CloudFormation::Init type to include metadata on an Amazon EC2 instance

EC Instance Template
{% gist https://gist.github.com/kananinirav/c373fd517f5d045921c2a5e0b1a9786b.js %}
**Outputs:**

The [Outputs](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html) 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.
{% gist https://gist.github.com/kananinirav/a18a9f13ad3daf0e1aba825e5d972181.js %}
**Complete CloudFormation Template:**
{% gist https://gist.github.com/kananinirav/12cee3f8fb3367b438349ece948ee835.js %}
**Final Output**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fa78syhqm84lp4r5a8mg.png)

RVM and Ruby Default installation output.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qydc9gji14bekv5xueqr.png)
>  Note, With the `DeletionPolicy` the attribute you can preserve, and in some cases, backup a resource when its stack is deleted. You specify a `DeletionPolicy` attribute for each resource that you want to control. If a resource has no `DeletionPolicy` attribute, Amazon CloudFormation deletes the resource by default. If you Want to keep your all resource then add `DeletionPolicy: Retain` in each resource
For more details: [AWS doc](https://docs.amazonaws.cn/en_us/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html)

**Conclusion**

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!