Table of Contents
In this tutorial, we will see how to create an AWS instance in 9 Easy Steps using terraform tool. Now a days Terraform is a widely used tool across multi Cloud Platform Setup where you can create and deploy resources in Cloud using few lines of code instead of following all the steps manually.
You can also find the wide usage of this tool in DevOps Framework integrated with Cloud. This tool will reduce the automation task complexity and will work as easy to deploy tool. You can easily track the usage of this tool in many larger firms and Organizations.
Terraform AWS Tutorial: Best Steps to Create 3 Node Setup in AWS
Also Read: How to Install Terraform on RedHat/CentOS 7
Step 1: Prerequisites
There are few prerequisites which you needed to fulfill before proceeding to create and deploy resources in this terraform aws tutorial.
a) You should have an AWS Account and access to create and deploy resources.
b) There are few tools like unzip and wget which must have been installed in your RedHat/CentOS based systems to proceed further in this terraform aws tutorial.
Step 2: Create AMI Image
To start with, first we need to create an AMI Image that can be used in our terraform script. For this example, I have already created the image so i will directly use the AMI ID of those images in below steps.
Step 3: Install terraform tool
First you need to install terraform tool in any of the RedHat/CentOS based machine using below steps:-
a) Download Terraform using wget.
You can download latest version of terraform from Terraform Official Website using wget
command as shown below.
[root@ip-172-87-26-247 ~]# wget https://releases.hashicorp.com/terraform/0.12.19/terraform_0.12.19_linux_amd64.zip
b) Install unzip package through yum
Since unzip will not be installed by default in Linux System so we need to download unzip in RedHat/CentOS based system using yum install unzip
command.
[root@ip-172-87-26-247 ~]# yum install unzip
c) Extract terraform package in /bin directory.
Once unzip is installed, you can unzip the terraform zip file in /bin
directory which is already exported in System $PATH
variable. If you want you can also extract is some other directory and add the directory in $PATH
variable.
[root@ip-172-87-26-247 ~]# unzip terraform_0.12.19_linux_amd64.zip -d /bin/
d) Verify if it is installed properly.
Lastly, you can verify the installation of terraform by checking the version of terraform using terraform -v
command.
[root@ip-172-87-26-247 ~]# terraform -v
Terraform v0.12.19
Step 4: Create Script for 3 Node Setup
You can create 3 node setup using below script in main.tf file.
[root@ip-172-87-26-247 ~]# cat main.tf resource "aws_eip" "lb" { count = 3 instance = "${aws_instance.web[count.index].id}" vpc = true } provider "aws" { region = "us-west-1" access_key = "AKIAREKJUOLJ5SFV5K" secret_key = "l5SlJ786yhXG7ap0rhfgytcxv/0dZ2NlopNPLym+h4x" } resource "aws_instance" "web" { count = "3" ami = "ami-04d77db037328859b" instance_type = "t2.2xlarge" tags = { Name = "Terraform AWS Tutorial Node" } }
Step 5: Initialize Terraform Using terraform init
Now you need to initialize terraform using below terraform init
command.
[root@ip-172-87-26-247 ~]# terraform init
Step 6: Check Your Plan Using terraform plan
You can also check your plan using terraform plan
command before doing any changes as shown below.
[root@ip-172-87-26-247 ~]# terraform plan
Step 7: Run terraform apply command to create all resources
Now you can run main.tf script by using terraform apply
command and create all the resources as specified in main.tf script.
[root@ip-172-87-26-247 ~]# terraform apply Enter a value: yes aws_instance.web[0]: Creating... aws_instance.web[2]: Creating... aws_instance.web[1]: Creating... aws_instance.web[2]: Still creating... [10s elapsed] aws_instance.web[0]: Still creating... [10s elapsed] aws_instance.web[1]: Still creating... [10s elapsed] aws_instance.web[2]: Creation complete after 15s [id=i-0b56daa4ced0e540b] aws_instance.web[1]: Creation complete after 15s [id=i-02618325bce629de6] aws_instance.web[0]: Creation complete after 15s [id=i-051f26c110c3e163a] aws_eip.lb[0]: Creating... aws_eip.lb[2]: Creating... aws_eip.lb[1]: Creating... aws_eip.lb[0]: Creation complete after 1s [id=eipalloc-0bed4d55356001b44] aws_eip.lb[1]: Creation complete after 1s [id=eipalloc-030a950453a86f35c] aws_eip.lb[2]: Creation complete after 1s [id=eipalloc-00667a6945c888aec] Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
Step 8: Verify in AWS Console
You can verify from AWS Console. All the nodes are created and EIPs are attached. Rest of the stuff like Security Group, attach storage will be taken care by AMI Image which I already created.
Step 9: Destroy all the Created Instances Using terraform destroy
Once your work is done, you can also destroy all the created instances through terraform by running terraform destroy
command as you can see below.
[root@ip-172-87-26-247 ~]# terraform destroy aws_eip.lb[0]: Destroying... [id=eipalloc-0f91cfc1ed41a8196] aws_eip.lb[2]: Destroying... [id=eipalloc-0973e9b44f068bbde] aws_eip.lb[1]: Destroying... [id=eipalloc-01e50f01884aea672] aws_eip.lb[1]: Destruction complete after 1s aws_eip.lb[2]: Destruction complete after 1s aws_eip.lb[0]: Destruction complete after 1s aws_instance.web[2]: Destroying... [id=i-0597859768304cf66] aws_instance.web[0]: Destroying... [id=i-049995453b037b068] aws_instance.web[1]: Destroying... [id=i-075d1163c3b189812] aws_instance.web[2]: Still destroying... [id=i-0597859768304cf66, 10s elapsed] aws_instance.web[0]: Still destroying... [id=i-049995453b037b068, 10s elapsed] aws_instance.web[1]: Still destroying... [id=i-075d1163c3b189812, 10s elapsed] aws_instance.web[1]: Destruction complete after 20s aws_instance.web[2]: Destruction complete after 20s aws_instance.web[0]: Destruction complete after 20s Destroy complete! Resources: 6 destroyed.
I hope this terraform aws tutorial was helpful and informative. Please do let me know your feedback.