Infrastructure Automation with Terraform and AWS

Arshad Zackeriya
4 min readMay 27, 2018

--

I always had this thought what if i can automate the infrastructure? as a DevOps engineer always trying to make all the process automate. Always and i love to work with AWS as my primary cloud service provider using AWS with terraform. For Continuous Integration i cant always log in to the AWS console and provisioning the servers, other AWS services. Then i found this awesome tool “Terraform” i must say this, that i am in love with this tool. Still there are not much of documentations and examples how to use this tool. I would like to share my experience with you all.

PS: If you are going to try my IAM user credentials, don't waste your time trying it, since i have already revoked the access 😛

What is Terraform?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. Configuration files describe to Terraform the components needed to run a single application or your entire data-center.

What we need to execute the first script?

AWS account, and IAM user with EC2 and VPC full access.

A linux host machine with AWS cli configured.

Terraform installed : How to install Terraform

Creating an IAM user and assigning Policy

In the AWS console go to the IAM section and add a new user as below

Next assign the both 2 policies as below.

Complete the setup, then you will get the Access ID Key and the Secret Access Key as below.

Make sure to copy those keys in a safe place hence we need that to the next step.

Add the IAM user to the Linux host

Please type the below command in your terminal.

since i am using multiple aws accounts i am using profiles to manage the credentials. I am going to create my EC2 instance in North Virginia hence the Default region is : us-east-1.

Clone My Sample project from the git.

You can clone the code from my below repository.

There are 3 files in the folder main.tf, variables.tf, outputs.tf

main.tf contains all the main function code blocks as below.

in the first block defining the provide, since i am using amazon web services provider is “aws”. you can see so many values starting with “var.value”, those are variables and all the variables are containing in the variable file

fill your aws information accordingly.

next is the outputs.tf file, we can mention what we need to get with results end of the execution.

i need the public IP to be printed.

Running the Terraform script

Go to the project folder and type “terraform plan” , this command will show you what you will be creating in the AWS.

obviously you may receive this error since you don't have the aws plugin inside the project folder. don’t panic and type “terraform init”.

this will successfully install the plugin.

Now you can see in the above image which shows what are the services will be creating in the aws.

Then you run the command “terraform apply”, it will as to confirm again to make sure that you are provisioning the correct services.

Once you confirm, it will suddenly start to execute. You can confirm this by checking the console.

You can see that my script successfully executing in the AWS, once this has been successfully executed as i mentioned above it will print the public IP of the server.

Isn't this awesome just 28 Seconds and i have my EC2 instance ready 😃

This is just a simple script and will be publishing more in the future, if you have any questions please feel free to comment or write to hello@arshadzackeriya.com.

--

--

Arshad Zackeriya
Arshad Zackeriya

Written by Arshad Zackeriya

Enabling DevOPs ☁ | A Cloud Enthusiast | AWS Community Builder | AWS Fanboy!!

No responses yet