Terraform with AWS

  1. 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)"
Source: https://brew.sh/
     2. After installation check the version by running below command.
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

Screen Shot 2020-04-12 at 9.12.47 p.m..jpg

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

Screen Shot 2020-04-12 at 9.13.39 p.m..jpg

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

Screen Shot 2020-04-12 at 9.18.59 p.m..jpg

There are two options you can get your access and secret key:

i) Go to access keys and get it from here.

Screen Shot 2020-04-12 at 9.21.24 p.m..jpg

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

Screen Shot 2020-04-12 at 9.30.09 p.m..jpg

Screen Shot 2020-04-12 at 9.43.11 p.m..jpg

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

Screen Shot 2020-04-12 at 9.45.26 p.m..jpg

Screen Shot 2020-04-12 at 9.45.38 p.m..jpg

 

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/
 

 

Screen Shot 2020-04-12 at 10.08.02 p.m..jpg

 

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

Screen Shot 2020-04-12 at 10.10.09 p.m..jpgScreen Shot 2020-04-12 at 10.17.59 p.m..jpg

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

Screen Shot 2020-04-12 at 10.18.44 p.m..jpg

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.

Screen Shot 2020-04-12 at 10.22.04 p.m..jpg

Now type Terraform apply.

Screen Shot 2020-04-12 at 10.23.30 p.m..jpg

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.

Screen Shot 2020-04-12 at 10.24.29 p.m..jpg

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

Screen Shot 2020-04-12 at 10.27.38 p.m..jpg

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

Screen Shot 2020-04-12 at 10.30.49 p.m..jpg

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

Screen Shot 2020-04-12 at 10.30.58 p.m..jpg

Now it start shutting down VM.

Screen Shot 2020-04-12 at 10.31.31 p.m..jpg

Here is the status.

Screen Shot 2020-04-12 at 10.31.42 p.m..jpg

And VM is terminated on AWS.

Screen Shot 2020-04-12 at 10.31.54 p.m..jpg

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.