Setup Your EC2 Instance And RDS Using CloudFormation

I have explained a lot about the Cloud Formation and use of Template Resources in my previous article. Let’s start a simple CloudFormation to set up EC2 instance and RDS.

What we are going to cover -

  1. Create an AWS Keypair. -> This is to login via SSH to the created EC2 instance
  2. Configure AWS CLI
  3. Configure EC2 using CloudFormation template
  4. Configure RDS using CloudFormation template

Creation of AWS KeyPair

TO create an AWS Keypair, log into AWS Web and select on Services -> EC2 under a region.

Setup Your EC2 Instance And RDS Using CloudFormation 

Here, I have chosen Oregon (us-west-2) region and made a note of the region.

Now, select the key pair under "Network and Security" which is listed on the left side panel and click on ‘Create a Key Pair’, provide a valid keypair name, and download it. Here, I have provided my Key Pair name as ‘CloudFormation’.

Setup Your EC2 Instance And RDS Using CloudFormation 
Setup Your EC2 Instance And RDS Using CloudFormation 

Configure AWS CLI

I have written in my previous article on how to configure a CLI in AWS. Kindly help yourself to have look on the same.

Configure EC2 using CloudFormation template

So, to Configure an EC2 instance, create a YAML file with the extension of. yml. So here, I have created it with a name as stack.yml

Setup Your EC2 Instance And RDS Using CloudFormation 

Mentioned above is the sample code to create a template for EC2 instance.

What I have done above? 

I have created a template with 2 resources. 
  1. InstanceNode
  2. InstanceNodeSG

InstanceNode

This node defines the type of instance along with the Keyname (created in step1), Image ID (The Image ID is different in each region. So, check the ID before we embed into the .yml file) and Instance Type.

InstanceNodeSG

This is the Security Group for the InstanceNode which we have created. So, we will define the enabling of the port number which is used to communicate with each other.

So we have specified the SSH and HTTP port here.

Update RDS using CloudFormation template

So here we will write to describe a RDS template.

Setup Your EC2 Instance And RDS Using CloudFormation 

So here also, we have 2 resources to create a RDS.

  1. DatabaseInstance
  2. DatabaseSG

DatabaseInstance

This will define the type of RDS engine which we going to configure, Username and password to connect to the Database, DB Name

DatabaseSG

This is the Security Group for DatabaseInstance. Here we will be describing about the communication needs to configure.

So once done, we will execute the command in CLI and Instance creation and RDS Creation.

To Execute the Template, login to command prompt and enter the below command,

C:\Users\shanmugapriyan.m\Desktop>aws cloudformation create-stack --stack-name EC2RDS --template-body file://stack.yml --region us-west-2

EC2RDS –> Stack name I have created.

region us-west-2-> Specified in which region it should be created.

Setup Your EC2 Instance And RDS Using CloudFormation 

Once the above command is success, we could able to see the stack is being created and the total number of resources are generated from our yaml file. So, we have totally 4 resources created and below are the same,

Setup Your EC2 Instance And RDS Using CloudFormation 

Once the RDS and EC2 instance are created, the status will be showing ad CREATE_COMPLETE where the instance is ready to login and do some next set of actions.

Since, we have RDS created, it will take max of 5 min to get all done.

Setup Your EC2 Instance And RDS Using CloudFormation 

Now, login to the EC2 instance and check for RDS created

Setup Your EC2 Instance And RDS Using CloudFormation 
 
Setup Your EC2 Instance And RDS Using CloudFormation 

Hope, this will clear how to create EC2 Instance with RDS connected.


Similar Articles