
- First install Terraform i am using mac so using below command you can install this software.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"sandeeps-MacBook-Pro:~ sandeep$ terraform Usage: terraform [-version] [-help] <command> [args] The available commands for execution are listed below. The most common, useful commands are shown first, followed by less common or more advanced commands. If you're just getting started with Terraform, stick with the common commands. For the other commands, please read the help and docs before usage. Common commands: applyBuilds or changes infrastructure consoleInteractive console for Terraform interpolations destroyDestroy Terraform-managed infrastructure envWorkspace management fmtRewrites config files to canonical format getDownload and install modules for the configuration graphCreate a visual graph of Terraform resources import Import existing infrastructure into Terraform init Initialize a Terraform working directory loginObtain and save credentials for a remote host logout Remove locally-stored credentials for a remote host output Read an output from a state file plan Generate and show an execution plan providersPrints a tree of the providers used in the configuration refreshUpdate local state file against real resources show Inspect Terraform state or plan taintManually mark a resource for recreation untaintManually unmark a resource as tainted validate Validates the Terraform files versionPrints the Terraform version workspaceWorkspace management All other commands: 0.12upgradeRewrites pre-0.12 module source code for v0.12 debugDebug output management (experimental) force-unlock Manually unlock the terraform state push Obsolete command for Terraform Enterprise legacy (v1) stateAdvanced state management sandeeps-MacBook-Pro:~ sandeep$
3. Now open Text Editor i am using Visual Studio Code
4. Create a new Project

5. Create a new File with extension tf. example: aws.tf

6. Now go to terraform website to check the provider.
https://www.terraform.io/docs/providers/aws/index.html
7. Here you can see all the examples and you can use to create your own.
8. Lets start with code
Note: Prerequisites
a) AWS account and
b) Access key for AWS account.
Note: Login into AWS account and click on you name and under that click on

There are two options you can get your access and secret key:
i) Go to access keys and get it from here.

ii) Go to users tab and get it from there.


Now let see if there is any EC2 instance is running in my infra or not before using Terraform.


Now go back to visual code and use below code to create a VM
/* AWS Provider config */
provider "aws" {
# Access key from AWS account
access_key = "AKIATXKMI7Q"
secret_key = "djRnkqKNyg15PREijU/NAWfme"
# Here you have to provide Region as per your service location or account
region = "eu-west-1"
}
# Here you have to select resource its from aws so we have to use aws_instance and name of the instance
resource "aws_instance" "Skumar-Terraform" {
# here you have to select AWS images ami number which you want to deploy
ami = "ami-0aaada7cbb0d829fc"
instance_type = "t2.micro"
}From here you can get AWS instance types.
https://aws.amazon.com/ec2/instance-types/

open terminal and go to the folder where you have stored this “awstest.tf” file


Once you hit enter it will check and build the plan and let you know what action it will going to perform.

On bottom it will show you status like Add, Change or destroy. in our case first we are going to add and it’s 1 change only.

Now type Terraform apply.

here you have to type: yes and it will start building.
once script is executed it will let you know the status whether its successful or there is an error.

Now go to AWS console and check if vm is there or not.

Now VM is up and running.
Now lets delete everything with one click as you know in AWS its not easy to delete everything easily like you have to clean everything before deleting a VM but in Terraform it’s only one command.
terraform destroy

You have to type yes again and it will delete the VM from AWS.

Now it start shutting down VM.

Here is the status.

And VM is terminated on AWS.

Leave a Reply