In the cloud native era, infrastructure as a code (IaaC) is a critical part of ensuring consistency and reusability. Most public providers have a version of IaaC they offer; for AWS, it is CloudFormation.
With CloudFormation, you can go from creating resources from the console to automating complex architecture on demand. It uses a template which is a declarative way of defining your resources as a YAML or JSON file.
In this blog post, we’ll look at two CloudFormation templates to create Windows and Linux EC2 instances and add EBS volume and later assigning an Elastic IP to the instance.
Creating EC2 instance
First, we need to create a YAML/JSON file to spin-up instance. (here we are using YAML file)
Now, from AWS console create a S3 bucket to create an object and upload this YAML file there and then copy the URL of your object.
Navigate to the Cloudformation section on your AWS console.
Click on Create stack
On the Prepare template section, select Template is ready, then choose Amazon s3 URL in the Specify Template section. Paste the URL of your s3 object that contains YAML file, then Click the Next button.
You will be taken to the next section. Here we define the stack name of our cloud template. I named it demo-stack-ec2. When you are done naming the stack, click the next button to continue.
You will be taken to the Configure Stack Options page. For Tags, permissions and advanced options, leave everything blank, scroll all the way down and click on the next button.
You will be taken to the review page. Leave everything blank and click the Create Stack button at the bottom right of the page.
Cloudformation will now begin implementing the resources described in the YAML file
Wait for 2–3 minutes for the process to complete.
CloudFormation has now successfully created our EC2 instance. Let’s navigate to the EC2 section to see the provisioned instance.
As you can see your ec2 instance is now created.
You can use any template of your own or from the AWS documentation available. But here I am using this below template. It is a simple YAML file that would first create an instance name ‘demo2-EC2’ and later add an EBS volume of size 50GB to this instance.
We have given this EC2 instance a larger hard disk through the BlockDeviceMapping section
Amazon EC2 supports two types of block devices:
· Instance store volumes (virtual devices whose underlying hardware is physically attached to the host computer for the instance)
· EBS volumes (remote storage devices)
When you attach a volume to your instance, you include a device name for the volume. For more information, see Device naming on Linux instances.
Now, for creating stack follow the above-mentioned steps.
As you can see here that the S3 object that contains the YAML file, its URL has been copied and pasted here.
We will now assign a name to the stack.
On the next page, Configure Stack Options you can leave everything as is and scroll all the way down and click on the next button.
Review your stack and click Create.
The stack create process will be initiated.
And after few minutes you can see that the CREATE_PROCESS is completed.
To verify your instance goto EC2 Dashboard. There you can see that the instance with name ‘demo2-ec2’ is created. Select that instance and in the details section below, click on storage option. You can see that EBS of volume size 50GB has been added.
Next stop, ASSIGNING ELASTIC IP
For this part we will be using the following template. As you can see, I have only added the ElasticIP resource in the previous template and have changed the name of the instance for clear understanding.
Follow the aforementioned steps for creating the stack.
In the events section you can see that all the process are completed. The instance has been created and elastic IP has also been assigned to the instance.
To verify, move to EC2 dashboard.
Select the instance named, ‘demo3-ec2’ and in the details down below you can see that the elastic IP has been assigned to the instance.
AWS does not support Elastic IP address with IPv6. The limit of Elastic IPv4 address per account is limited to 5 per region.
Congratulation!! If you were able to follow all these steps then you have created an EC2 instance along with EBS and provided an elastic IP to it