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 -
- Create an AWS Keypair. -> This is to login via SSH to the created EC2 instance
- Configure AWS CLI
- Configure EC2 using CloudFormation template
- 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.
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’.
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
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.
- InstanceNode
- 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.
So here also, we have 2 resources to create a RDS.
- DatabaseInstance
- 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.
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,
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.
Now, login to the EC2 instance and check for RDS created
Hope, this will clear how to create EC2 Instance with RDS connected.