Spinning aN EC2 VM in AWS Using the Terraform

  • Free tier AWS account
  • Linux OS with terraform installed and configured.
  • Login to linux server and create directory which will be keeping our terraform code
    • mkdir terraformworkspace
    • cd terraformworkspace
  • Now post creating the directory write main.tf file under above created directory
    • vi main.tf
    • Put this content under main.tf

provider “aws” {
region = “us-east-1”
resource “aws_instance” “vm” {
ami = “ami-0e1c5d8c23330dee3”
subnet_id = “ubnet-08d77c2d5933004b7”
instance_type = “t3.micro”
tags = {
Name = “my-first-tf-node”

  • Save the content under main.tf and come out of vi editor.
  • Go to the directory terraformworkspace which we created and execute this commands
    • terrafrom init
      • Once initialization is successful it will show this message

      • Post initialization execute command terraform plan out of this command should show how many resources will get created as part of this terraform script execution.
        Here it’s showing 1 resource will be added once the script is executed.
  • Deploy the terraform code using command terraform apply when command is executed it will ask for option type yes to accept.

  • Once the terraform apply completes output will be like below

  • So, it’s showing 1 resource added.
  • To verify that resource is created.
    • Navigate to the AWS Management Console in your browser.
    • Type EC2 in the search bar and select EC2 from the contextual menu.
    • On the Resources page, click Instances (running).

Use Terraform destroy command clean up the resource created.

  • From same location from linux CLI execute command terraform destroy after review choose yes once command is successful below will be output the EC2 VM which was got created will be destroyed.

So, we have successfully spun up a EC2 VM and then cleaned it up using terraform destroy command, so we have seen implementation basic flow of terraform commands.