Last Updated on January 31, 2020
Hello again. I hope you guys are doing awesome, and I am back again with a new blog – how to create AWS application stack using Terraform.
It is obvious that the mind gets easily bored doing the same thing repeatedly all over and over. Being an AWS cloud solution architect, I have to manage the resources, even sometimes I have to provision server resources twice or thrice a day.
The provision of resources seems so easy, but let’s assume you have to create VPC on AWS that contains subnets, both public and private, public for hosting frontend i.e., Web Server and Private for Database.
IPs – I am working with VPC so it will contain both public and private IPs, and of course using CIDR block. Apart from that, you might need Route Table; here, I am using public and private subnet as each subnet required one Route Table. Still, Internet Gateway (IG) is missing, which precisely takes care of communication between instances and the Internet.
For security reasons, our Private Subnet (Database) is only through using Public subnet, let’s understand this through a Diagram.
Lots of configuration are there to host a single secure web application. Here we need a viable solution. From now onwards, if you need the same Stack and resources, then you won’t be required to spend the same amount of time in configuring VPC.
We can achieve this using Terraform as IaaC, yes Terraform is a tool used for Infrastructure automation; it is an approach for provisioning and managing server resources using configuration file or script.
What is infrastructure as a code?
Infrastructure as a Code also known as -IaaC, is the management of infrastructure networks, load balancers, virtual machines, and connection topology in a vivid model, applying the same version of DevOps on AWS team that is used for source code.
Let’s get back on the track and understand how to write Terraform scripting and how to configure the AWS account.
Our Terraform script is going to use AccessKey and SecretKey for the provision and manage AWS resources.
Go to IAM users, security credential and save the ACCESS_KEY and the SECRET_ACCESS_KEY.
Setup Provider for Terraform.
In terraform script, you will have to mention provider, in the below example, AWS is the provider. Provider.tf file looks something like;
Note: We are getting this var.variable_name value from variable.tf file.
Terraform scripting for VPC
In the above vpc.tf file I have created VPC, now it’s time for security groups to create a Terraform file.
Terraforming security groups are pretty easy. You only have to define ingress and egress rules.
Now it’s time for variable.tf
To create the AWS resource using Terraform script, please run the below commands.
terraform init terraform apply
Please make sure you provide the correct region and AMI else you will get errors. In the above scenario, I have discussed about the VPC; you can customize based on your application stack.
I hope you have completely got the idea of how Terraform resource allocation and mapping is working. But, if you want to gain more knowledge on this, then I would like to request you to visit this link where all the AWS services and it’s mapping technology have mentioned and explained in detail.