CloudFormation: Deploy EC2 instance

Fahim Fahad
4 min readApr 28, 2023

CloudFormation is an AWS service that enables developers to create AWS resources easily, allowing us to reuse the scripts to create resources.

I am learning CloudFormation, and this is my first article.

In this article, I will create an EC2 instance. with two security groups so I can ssh to the instance, and web apps can connect using port 80. I will use user data to install Java 17. This is a very basic setup. Lets begin 🙂

Prerequisite

Before I start writing CF script I need to prepare aws to make it work. AWS is all about proper access and configuration. For my task I will need to prepare 3 things.

  1. Create an IAM role to execute CF scripts. It is preferable not to use the root user.
  2. Create a role for CloudFormation with administrator access.
  3. Configure a key pair for EC2 to connect to an EC2 instance.
Role for CloudFormation

CloudFormation Script

AWSTemplateFormatVersion: '2010-09-09'
Description: >-
CloudFormation template to create EC2 instance with security group

Resources:
MyInstance:
Type…

--

--