AWS instance creation with terraform

Cloud provides us the provision to create infrastructure with much ease. You can upgrade or scale up the servers with just few clicks. At one side this functionality of cloud is a big advantage. Whereas on the other side its difficult to manage or track the infrastructure.

Infrastructure as code is new buzz word in IT world. And you can write down infrastructure as code. It provides advantage of creating new resources, manage existing ones and destroy the resources which are not in use. The resources are fully controlled because nothing is manually created. And code can be versioned through GIT. And it helps in getting track changes in the infrastructure.

AWS provides Cloud Formation to write down infrastructure as code. But the advantage of using terraform over Cloud Formation is that terraform is not limited to AWS. Terraform supports multiple providers other than AWS, like Microsoft Azure, Google Cloud, Alibaba Cloud and many more. You can view terraform provider page to see the full list of providers terraform supports.





(adsbygoogle = window.adsbygoogle || []).push({});

To start with Terraform you can download it from its website. I downloaded the Mac version. unpack it and copy it into a folder. It will be a unix executable file. Next step is to login into the AWS management console and create a admin user. If you already created it then go to security credential tab and download the create the access key. I will recommend not to use the super admin user.

Terraform files are saved with .tf extension. I use Visual Studio code to write down the code. You can use any editor or notepad/textpad to write the code. Create a new file to store credentials and region. I name it variable.tf. This will help to separate the common values. To define variables, we have to use the keyword ‘variable’ and then any name. In the variable file I store region, access key and secret key values.

In the main file, start the file with provider as AWS. And then set the access key, secret key and region. As we store these values in a separate file. To access a variable access key following is the syntax ${var.aws_access_key}. Var keyword and then the variable name. Then to create EC2 instance which is a resource, resource keyword is used and then ‘aws_instance’ to create EC2 instance. Below code will create EC2 instance of type T2 micro in the US east 1 region.

To run the terraform open the terminal and goto the directory where you saved these files. Now you can include the terraform unzip file in the same directory or outside the source code folder. I have copied the terraform unzip file outside my source folder. Now to run the terraform enter the first command init which will download all the dependancies. ../terraform init. Because I am inside the source folder and terraform unix file is outside that is why I run the command. Next to apply or implement the code, it requires following code ../terraform apply. This command will apply the infrastructure. Before implementing or creating the infrastructure, terraform will show the report what it’s going to create. This is quite easy to implementing resources. If you are using free tier or want to destroy the resources. It’s quite easy to destroy the instance with terraform. Following command will destroy the resources ../terraform destroy.

You can download the source at https://github.com/waqas80/terraform_intro

Tagged with: , , , , , ,