Lab Activity 1

 Lab Activity 1:

Installing AWS CLI:

For the latest version of the AWS CLI, use the following command block:

$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

unzip awscliv2.zip

sudo ./aws/install


mohan:~$ /usr/local/bin/aws --version

aws-cli/2.2.44 Python/3.8.8 Linux/4.18.0-305.17.1.el8_4.x86_64 exe/x86_64.rhel.8 prompt/off


Configure the AWS CLI:

mohan:~$ aws configure

AWS Access Key ID [None]: Supply Access Key ID

AWS Secret Access Key [None]: Supply your Secret Access Key

Default region name [None]: 

Default output format [None]: 

mohan:~$ ls -al ~/.aws/credentials

-rw-------. 1 mohan mohan 116 Oct  8 20:52 /home/mohan/.aws/credentials


Build Infrastructure:

mohan:~$ mkdir learn-terraform-aws-instance

mohan:~$ cd learn-terraform-aws-instance/

mohan:learn-terraform-aws-instance$ touch main.tf

mohan:learn-terraform-aws-instance$ vi main.tf 

terraform {

  required_providers {

    aws = {

      source  = "hashicorp/aws"

      version = "~> 3.27"

    }

  }


  required_version = ">= 0.14.9"

}


provider "aws" {

  profile = "default"

  region  = "us-west-2"

}


resource "aws_instance" "app_server" {

  ami           = "ami-830c94e3"

  instance_type = "t2.micro"


  tags = {

    Name = "ExampleAppServerInstance"

  }

}


mohan:learn-terraform-aws-instance$ terraform init


Initializing the backend...


Initializing provider plugins...

- Finding hashicorp/aws versions matching "~> 3.27"...

- Installing hashicorp/aws v3.62.0...

- Installed hashicorp/aws v3.62.0 (signed by HashiCorp)


Terraform has created a lock file .terraform.lock.hcl to record the provider

selections it made above. Include this file in your version control repository

so that Terraform can guarantee to make the same selections by default when

you run "terraform init" in the future.


Terraform has been successfully initialized!


You may now begin working with Terraform. Try running "terraform plan" to see

any changes that are required for your infrastructure. All Terraform commands

should now work.


If you ever set or change modules or backend configuration for Terraform,

rerun this command to reinitialize your working directory. If you forget, other

commands will detect it and remind you to do so if necessary.

Format and validate the configuration::

mohan:learn-terraform-aws-instance$ terraform fmt

mohan:learn-terraform-aws-instance$ ls -al

total 20

drwxrwxr-x.  3 mohan mohan 4096 Oct  8 20:55 .

drwx------. 52 mohan mohan 4096 Oct  8 20:55 ..

-rw-rw-r--.  1 mohan mohan  376 Oct  8 20:55 main.tf

drwxr-xr-x.  3 mohan mohan 4096 Oct  8 20:55 .terraform

-rw-r--r--.  1 mohan mohan 1107 Oct  8 20:55 .terraform.lock.hcl

mohan:learn-terraform-aws-instance$ terraform validate

Success! The configuration is valid.


mohan:learn-terraform-aws-instance$ terraform plan


Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

  + create


Terraform will perform the following actions:


  # aws_instance.app_server will be created

  + resource "aws_instance" "app_server" {

      + ami                                  = "ami-830c94e3"

      + arn                                  = (known after apply)

      + associate_public_ip_address          = (known after apply)

      + availability_zone                    = (known after apply)

      + cpu_core_count                       = (known after apply)

      + cpu_threads_per_core                 = (known after apply)

      + disable_api_termination              = (known after apply)

      + ebs_optimized                        = (known after apply)

      + get_password_data                    = false

      + host_id                              = (known after apply)

      + id                                   = (known after apply)

      + instance_initiated_shutdown_behavior = (known after apply)

      + instance_state                       = (known after apply)

      + instance_type                        = "t2.micro"

      + ipv6_address_count                   = (known after apply)

      + ipv6_addresses                       = (known after apply)

      + key_name                             = (known after apply)

      + monitoring                           = (known after apply)

      + outpost_arn                          = (known after apply)

      + password_data                        = (known after apply)

      + placement_group                      = (known after apply)

      + primary_network_interface_id         = (known after apply)

      + private_dns                          = (known after apply)

      + private_ip                           = (known after apply)

      + public_dns                           = (known after apply)

      + public_ip                            = (known after apply)

      + secondary_private_ips                = (known after apply)

      + security_groups                      = (known after apply)

      + source_dest_check                    = true

      + subnet_id                            = (known after apply)

      + tags                                 = {

          + "Name" = "ExampleAppServerInstance"

        }

      + tags_all                             = {

          + "Name" = "ExampleAppServerInstance"

        }

      + tenancy                              = (known after apply)

      + user_data                            = (known after apply)

      + user_data_base64                     = (known after apply)

      + vpc_security_group_ids               = (known after apply)


      + capacity_reservation_specification {

          + capacity_reservation_preference = (known after apply)


          + capacity_reservation_target {

              + capacity_reservation_id = (known after apply)

            }

        }


      + ebs_block_device {

          + delete_on_termination = (known after apply)

          + device_name           = (known after apply)

          + encrypted             = (known after apply)

          + iops                  = (known after apply)

          + kms_key_id            = (known after apply)

          + snapshot_id           = (known after apply)

          + tags                  = (known after apply)

          + throughput            = (known after apply)

          + volume_id             = (known after apply)

          + volume_size           = (known after apply)

          + volume_type           = (known after apply)

        }


      + enclave_options {

          + enabled = (known after apply)

        }


      + ephemeral_block_device {

          + device_name  = (known after apply)

          + no_device    = (known after apply)

          + virtual_name = (known after apply)

        }


      + metadata_options {

          + http_endpoint               = (known after apply)

          + http_put_response_hop_limit = (known after apply)

          + http_tokens                 = (known after apply)

        }


      + network_interface {

          + delete_on_termination = (known after apply)

          + device_index          = (known after apply)

          + network_interface_id  = (known after apply)

        }


      + root_block_device {

          + delete_on_termination = (known after apply)

          + device_name           = (known after apply)

          + encrypted             = (known after apply)

          + iops                  = (known after apply)

          + kms_key_id            = (known after apply)

          + tags                  = (known after apply)

          + throughput            = (known after apply)

          + volume_id             = (known after apply)

          + volume_size           = (known after apply)

          + volume_type           = (known after apply)

        }

    }


Plan: 1 to add, 0 to change, 0 to destroy.


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────


Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.


Create infrastructure:

mohan:learn-terraform-aws-instance$ terraform apply


Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

  + create


Terraform will perform the following actions:


  # aws_instance.app_server will be created

  + resource "aws_instance" "app_server" {

      + ami                                  = "ami-830c94e3"

      + arn                                  = (known after apply)

      + associate_public_ip_address          = (known after apply)

      + availability_zone                    = (known after apply)

      + cpu_core_count                       = (known after apply)

      + cpu_threads_per_core                 = (known after apply)

      + disable_api_termination              = (known after apply)

      + ebs_optimized                        = (known after apply)

      + get_password_data                    = false

      + host_id                              = (known after apply)

      + id                                   = (known after apply)

      + instance_initiated_shutdown_behavior = (known after apply)

      + instance_state                       = (known after apply)

      + instance_type                        = "t2.micro"

      + ipv6_address_count                   = (known after apply)

      + ipv6_addresses                       = (known after apply)

      + key_name                             = (known after apply)

      + monitoring                           = (known after apply)

      + outpost_arn                          = (known after apply)

      + password_data                        = (known after apply)

      + placement_group                      = (known after apply)

      + primary_network_interface_id         = (known after apply)

      + private_dns                          = (known after apply)

      + private_ip                           = (known after apply)

      + public_dns                           = (known after apply)

      + public_ip                            = (known after apply)

      + secondary_private_ips                = (known after apply)

      + security_groups                      = (known after apply)

      + source_dest_check                    = true

      + subnet_id                            = (known after apply)

      + tags                                 = {

          + "Name" = "ExampleAppServerInstance"

        }

      + tags_all                             = {

          + "Name" = "ExampleAppServerInstance"

        }

      + tenancy                              = (known after apply)

      + user_data                            = (known after apply)

      + user_data_base64                     = (known after apply)

      + vpc_security_group_ids               = (known after apply)


      + capacity_reservation_specification {

          + capacity_reservation_preference = (known after apply)


          + capacity_reservation_target {

              + capacity_reservation_id = (known after apply)

            }

        }


      + ebs_block_device {

          + delete_on_termination = (known after apply)

          + device_name           = (known after apply)

          + encrypted             = (known after apply)

          + iops                  = (known after apply)

          + kms_key_id            = (known after apply)

          + snapshot_id           = (known after apply)

          + tags                  = (known after apply)

          + throughput            = (known after apply)

          + volume_id             = (known after apply)

          + volume_size           = (known after apply)

          + volume_type           = (known after apply)

        }


      + enclave_options {

          + enabled = (known after apply)

        }


      + ephemeral_block_device {

          + device_name  = (known after apply)

          + no_device    = (known after apply)

          + virtual_name = (known after apply)

        }


      + metadata_options {

          + http_endpoint               = (known after apply)

          + http_put_response_hop_limit = (known after apply)

          + http_tokens                 = (known after apply)

        }


      + network_interface {

          + delete_on_termination = (known after apply)

          + device_index          = (known after apply)

          + network_interface_id  = (known after apply)

        }


      + root_block_device {

          + delete_on_termination = (known after apply)

          + device_name           = (known after apply)

          + encrypted             = (known after apply)

          + iops                  = (known after apply)

          + kms_key_id            = (known after apply)

          + tags                  = (known after apply)

          + throughput            = (known after apply)

          + volume_id             = (known after apply)

          + volume_size           = (known after apply)

          + volume_type           = (known after apply)

        }

    }


Plan: 1 to add, 0 to change, 0 to destroy.


Do you want to perform these actions?

  Terraform will perform the actions described above.

  Only 'yes' will be accepted to approve.


  Enter a value: yes


aws_instance.app_server: Creating...

aws_instance.app_server: Still creating... [10s elapsed]

aws_instance.app_server: Still creating... [20s elapsed]

aws_instance.app_server: Still creating... [30s elapsed]

aws_instance.app_server: Still creating... [40s elapsed]

aws_instance.app_server: Creation complete after 47s [id=i-0e02921b39173658e]


Apply complete! Resources: 1 added, 0 changed, 0 destroyed.



mohan:learn-terraform-aws-instance$ terraform show

# aws_instance.app_server:

resource "aws_instance" "app_server" {

    ami                                  = "ami-830c94e3"

    arn                                  = "arn:aws:ec2:us-west-2:336296443354:instance/i-0e02921b39173658e"

    associate_public_ip_address          = true

    availability_zone                    = "us-west-2a"

    cpu_core_count                       = 1

    cpu_threads_per_core                 = 1

    disable_api_termination              = false

    ebs_optimized                        = false

    get_password_data                    = false

    hibernation                          = false

    id                                   = "i-0e02921b39173658e"

    instance_initiated_shutdown_behavior = "stop"

    instance_state                       = "running"

    instance_type                        = "t2.micro"

    ipv6_address_count                   = 0

    ipv6_addresses                       = []

    monitoring                           = false

    primary_network_interface_id         = "eni-005bbe0c39c4f5255"

    private_dns                          = "ip-172-31-26-66.us-west-2.compute.internal"

    private_ip                           = "172.31.26.66"

    public_dns                           = "ec2-34-219-9-130.us-west-2.compute.amazonaws.com"

    public_ip                            = "34.219.9.130"

    secondary_private_ips                = []

    security_groups                      = [

        "default",

    ]

    source_dest_check                    = true

    subnet_id                            = "subnet-b88413c0"

    tags                                 = {

        "Name" = "ExampleAppServerInstance"

    }

    tags_all                             = {

        "Name" = "ExampleAppServerInstance"

    }

    tenancy                              = "default"

    vpc_security_group_ids               = [

        "sg-bb8ccf8c",

    ]


    capacity_reservation_specification {

        capacity_reservation_preference = "open"

    }


    credit_specification {

        cpu_credits = "standard"

    }


    enclave_options {

        enabled = false

    }


    metadata_options {

        http_endpoint               = "enabled"

        http_put_response_hop_limit = 1

        http_tokens                 = "optional"

    }


    root_block_device {

        delete_on_termination = true

        device_name           = "/dev/sda1"

        encrypted             = false

        iops                  = 0

        tags                  = {}

        throughput            = 0

        volume_id             = "vol-0cfc53132413bf87f"

        volume_size           = 8

        volume_type           = "standard"

    }

}

mohan:learn-terraform-aws-instance$ 


mohan:learn-terraform-aws-instance$ terraform state list

aws_instance.app_server


Change Infrastructure:

Just change the AMI value in main.tf from ami-830c94e3 to ami-08d70e59c07c61a3a

resource "aws_instance" "app_server" {

   ami           = "ami-08d70e59c07c61a3a"

   instance_type = "t2.micro"

 }

mohan:learn-terraform-aws-instance$ terraform plan

aws_instance.app_server: Refreshing state... [id=i-0e02921b39173658e]


Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

-/+ destroy and then create replacement


Terraform will perform the following actions:


  # aws_instance.app_server must be replaced

-/+ resource "aws_instance" "app_server" {

      ~ ami                                  = "ami-830c94e3" -> "ami-08d70e59c07c61a3a" # forces replacement

      ~ arn                                  = "arn:aws:ec2:us-west-2:336296443354:instance/i-0e02921b39173658e" -> (known after apply)

      ~ associate_public_ip_address          = true -> (known after apply)

      ~ availability_zone                    = "us-west-2a" -> (known after apply)

      ~ cpu_core_count                       = 1 -> (known after apply)

      ~ cpu_threads_per_core                 = 1 -> (known after apply)

      ~ disable_api_termination              = false -> (known after apply)

      ~ ebs_optimized                        = false -> (known after apply)

      - hibernation                          = false -> null

      + host_id                              = (known after apply)

      ~ id                                   = "i-0e02921b39173658e" -> (known after apply)

      ~ instance_initiated_shutdown_behavior = "stop" -> (known after apply)

      ~ instance_state                       = "running" -> (known after apply)

      ~ ipv6_address_count                   = 0 -> (known after apply)

      ~ ipv6_addresses                       = [] -> (known after apply)

      + key_name                             = (known after apply)

      ~ monitoring                           = false -> (known after apply)

      + outpost_arn                          = (known after apply)

      + password_data                        = (known after apply)

      + placement_group                      = (known after apply)

      ~ primary_network_interface_id         = "eni-005bbe0c39c4f5255" -> (known after apply)

      ~ private_dns                          = "ip-172-31-26-66.us-west-2.compute.internal" -> (known after apply)

      ~ private_ip                           = "172.31.26.66" -> (known after apply)

      ~ public_dns                           = "ec2-34-219-9-130.us-west-2.compute.amazonaws.com" -> (known after apply)

      ~ public_ip                            = "34.219.9.130" -> (known after apply)

      ~ secondary_private_ips                = [] -> (known after apply)

      ~ security_groups                      = [

          - "default",

        ] -> (known after apply)

      ~ subnet_id                            = "subnet-b88413c0" -> (known after apply)

        tags                                 = {

            "Name" = "ExampleAppServerInstance"

        }

      ~ tenancy                              = "default" -> (known after apply)

      + user_data                            = (known after apply)

      + user_data_base64                     = (known after apply)

      ~ vpc_security_group_ids               = [

          - "sg-bb8ccf8c",

        ] -> (known after apply)

        # (4 unchanged attributes hidden)


      ~ capacity_reservation_specification {

          ~ capacity_reservation_preference = "open" -> (known after apply)


          + capacity_reservation_target {

              + capacity_reservation_id = (known after apply)

            }

        }


      - credit_specification {

          - cpu_credits = "standard" -> null

        }


      + ebs_block_device {

          + delete_on_termination = (known after apply)

          + device_name           = (known after apply)

          + encrypted             = (known after apply)

          + iops                  = (known after apply)

          + kms_key_id            = (known after apply)

          + snapshot_id           = (known after apply)

          + tags                  = (known after apply)

          + throughput            = (known after apply)

          + volume_id             = (known after apply)

          + volume_size           = (known after apply)

          + volume_type           = (known after apply)

        }


      ~ enclave_options {

          ~ enabled = false -> (known after apply)

        }


      + ephemeral_block_device {

          + device_name  = (known after apply)

          + no_device    = (known after apply)

          + virtual_name = (known after apply)

        }


      ~ metadata_options {

          ~ http_endpoint               = "enabled" -> (known after apply)

          ~ http_put_response_hop_limit = 1 -> (known after apply)

          ~ http_tokens                 = "optional" -> (known after apply)

        }


      + network_interface {

          + delete_on_termination = (known after apply)

          + device_index          = (known after apply)

          + network_interface_id  = (known after apply)

        }


      ~ root_block_device {

          ~ delete_on_termination = true -> (known after apply)

          ~ device_name           = "/dev/sda1" -> (known after apply)

          ~ encrypted             = false -> (known after apply)

          ~ iops                  = 0 -> (known after apply)

          + kms_key_id            = (known after apply)

          ~ tags                  = {} -> (known after apply)

          ~ throughput            = 0 -> (known after apply)

          ~ volume_id             = "vol-0cfc53132413bf87f" -> (known after apply)

          ~ volume_size           = 8 -> (known after apply)

          ~ volume_type           = "standard" -> (known after apply)

        }

    }


Plan: 1 to add, 0 to change, 1 to destroy.


───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────


Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.


mohan:learn-terraform-aws-instance$ terraform apply

aws_instance.app_server: Refreshing state... [id=i-0e02921b39173658e]


Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

-/+ destroy and then create replacement


Terraform will perform the following actions:


  # aws_instance.app_server must be replaced

-/+ resource "aws_instance" "app_server" {

      ~ ami                                  = "ami-830c94e3" -> "ami-08d70e59c07c61a3a" # forces replacement

      ~ arn                                  = "arn:aws:ec2:us-west-2:336296443354:instance/i-0e02921b39173658e" -> (known after apply)

      ~ associate_public_ip_address          = true -> (known after apply)

      ~ availability_zone                    = "us-west-2a" -> (known after apply)

      ~ cpu_core_count                       = 1 -> (known after apply)

      ~ cpu_threads_per_core                 = 1 -> (known after apply)

      ~ disable_api_termination              = false -> (known after apply)

      ~ ebs_optimized                        = false -> (known after apply)

      - hibernation                          = false -> null

      + host_id                              = (known after apply)

      ~ id                                   = "i-0e02921b39173658e" -> (known after apply)

      ~ instance_initiated_shutdown_behavior = "stop" -> (known after apply)

      ~ instance_state                       = "running" -> (known after apply)

      ~ ipv6_address_count                   = 0 -> (known after apply)

      ~ ipv6_addresses                       = [] -> (known after apply)

      + key_name                             = (known after apply)

      ~ monitoring                           = false -> (known after apply)

      + outpost_arn                          = (known after apply)

      + password_data                        = (known after apply)

      + placement_group                      = (known after apply)

      ~ primary_network_interface_id         = "eni-005bbe0c39c4f5255" -> (known after apply)

      ~ private_dns                          = "ip-172-31-26-66.us-west-2.compute.internal" -> (known after apply)

      ~ private_ip                           = "172.31.26.66" -> (known after apply)

      ~ public_dns                           = "ec2-34-219-9-130.us-west-2.compute.amazonaws.com" -> (known after apply)

      ~ public_ip                            = "34.219.9.130" -> (known after apply)

      ~ secondary_private_ips                = [] -> (known after apply)

      ~ security_groups                      = [

          - "default",

        ] -> (known after apply)

      ~ subnet_id                            = "subnet-b88413c0" -> (known after apply)

        tags                                 = {

            "Name" = "ExampleAppServerInstance"

        }

      ~ tenancy                              = "default" -> (known after apply)

      + user_data                            = (known after apply)

      + user_data_base64                     = (known after apply)

      ~ vpc_security_group_ids               = [

          - "sg-bb8ccf8c",

        ] -> (known after apply)

        # (4 unchanged attributes hidden)


      ~ capacity_reservation_specification {

          ~ capacity_reservation_preference = "open" -> (known after apply)


          + capacity_reservation_target {

              + capacity_reservation_id = (known after apply)

            }

        }


      - credit_specification {

          - cpu_credits = "standard" -> null

        }


      + ebs_block_device {

          + delete_on_termination = (known after apply)

          + device_name           = (known after apply)

          + encrypted             = (known after apply)

          + iops                  = (known after apply)

          + kms_key_id            = (known after apply)

          + snapshot_id           = (known after apply)

          + tags                  = (known after apply)

          + throughput            = (known after apply)

          + volume_id             = (known after apply)

          + volume_size           = (known after apply)

          + volume_type           = (known after apply)

        }


      ~ enclave_options {

          ~ enabled = false -> (known after apply)

        }


      + ephemeral_block_device {

          + device_name  = (known after apply)

          + no_device    = (known after apply)

          + virtual_name = (known after apply)

        }


      ~ metadata_options {

          ~ http_endpoint               = "enabled" -> (known after apply)

          ~ http_put_response_hop_limit = 1 -> (known after apply)

          ~ http_tokens                 = "optional" -> (known after apply)

        }


      + network_interface {

          + delete_on_termination = (known after apply)

          + device_index          = (known after apply)

          + network_interface_id  = (known after apply)

        }


      ~ root_block_device {

          ~ delete_on_termination = true -> (known after apply)

          ~ device_name           = "/dev/sda1" -> (known after apply)

          ~ encrypted             = false -> (known after apply)

          ~ iops                  = 0 -> (known after apply)

          + kms_key_id            = (known after apply)

          ~ tags                  = {} -> (known after apply)

          ~ throughput            = 0 -> (known after apply)

          ~ volume_id             = "vol-0cfc53132413bf87f" -> (known after apply)

          ~ volume_size           = 8 -> (known after apply)

          ~ volume_type           = "standard" -> (known after apply)

        }

    }


Plan: 1 to add, 0 to change, 1 to destroy.


Do you want to perform these actions?

  Terraform will perform the actions described above.

  Only 'yes' will be accepted to approve.


  Enter a value: yes


aws_instance.app_server: Destroying... [id=i-0e02921b39173658e]

aws_instance.app_server: Still destroying... [id=i-0e02921b39173658e, 10s elapsed]

aws_instance.app_server: Still destroying... [id=i-0e02921b39173658e, 20s elapsed]

aws_instance.app_server: Destruction complete after 27s

aws_instance.app_server: Creating...

aws_instance.app_server: Still creating... [10s elapsed]

aws_instance.app_server: Still creating... [20s elapsed]

aws_instance.app_server: Still creating... [30s elapsed]

aws_instance.app_server: Still creating... [40s elapsed]

aws_instance.app_server: Still creating... [50s elapsed]

aws_instance.app_server: Creation complete after 51s [id=i-0d172e710677fc978]


Apply complete! Resources: 1 added, 0 changed, 1 destroyed.


mohan:learn-terraform-aws-instance$ terraform show

# aws_instance.app_server:

resource "aws_instance" "app_server" {

    ami                                  = "ami-08d70e59c07c61a3a"

    arn                                  = "arn:aws:ec2:us-west-2:336296443354:instance/i-0d172e710677fc978"

    associate_public_ip_address          = true

    availability_zone                    = "us-west-2a"

    cpu_core_count                       = 1

    cpu_threads_per_core                 = 1

    disable_api_termination              = false

    ebs_optimized                        = false

    get_password_data                    = false

    hibernation                          = false

    id                                   = "i-0d172e710677fc978"

    instance_initiated_shutdown_behavior = "stop"

    instance_state                       = "running"

    instance_type                        = "t2.micro"

    ipv6_address_count                   = 0

    ipv6_addresses                       = []

    monitoring                           = false

    primary_network_interface_id         = "eni-0c1174dd2ee6112e4"

    private_dns                          = "ip-172-31-16-31.us-west-2.compute.internal"

    private_ip                           = "172.31.16.31"

    public_dns                           = "ec2-35-86-76-14.us-west-2.compute.amazonaws.com"

    public_ip                            = "35.86.76.14"

    secondary_private_ips                = []

    security_groups                      = [

        "default",

    ]

    source_dest_check                    = true

    subnet_id                            = "subnet-b88413c0"

    tags                                 = {

        "Name" = "ExampleAppServerInstance"

    }

    tags_all                             = {

        "Name" = "ExampleAppServerInstance"

    }

    tenancy                              = "default"

    vpc_security_group_ids               = [

        "sg-bb8ccf8c",

    ]


    capacity_reservation_specification {

        capacity_reservation_preference = "open"

    }


    credit_specification {

        cpu_credits = "standard"

    }


    enclave_options {

        enabled = false

    }


    metadata_options {

        http_endpoint               = "enabled"

        http_put_response_hop_limit = 1

        http_tokens                 = "optional"

    }


    root_block_device {

        delete_on_termination = true

        device_name           = "/dev/sda1"

        encrypted             = false

        iops                  = 100

        tags                  = {}

        throughput            = 0

        volume_id             = "vol-04b66715193169bf7"

        volume_size           = 8

        volume_type           = "gp2"

    }

}




Destroy Infrastructure:


mohan:learn-terraform-aws-instance$ terraform destroy

aws_instance.app_server: Refreshing state... [id=i-0d172e710677fc978]


Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

  - destroy


Terraform will perform the following actions:


  # aws_instance.app_server will be destroyed

  - resource "aws_instance" "app_server" {

      - ami                                  = "ami-08d70e59c07c61a3a" -> null

      - arn                                  = "arn:aws:ec2:us-west-2:336296443354:instance/i-0d172e710677fc978" -> null

      - associate_public_ip_address          = true -> null

      - availability_zone                    = "us-west-2a" -> null

      - cpu_core_count                       = 1 -> null

      - cpu_threads_per_core                 = 1 -> null

      - disable_api_termination              = false -> null

      - ebs_optimized                        = false -> null

      - get_password_data                    = false -> null

      - hibernation                          = false -> null

      - id                                   = "i-0d172e710677fc978" -> null

      - instance_initiated_shutdown_behavior = "stop" -> null

      - instance_state                       = "running" -> null

      - instance_type                        = "t2.micro" -> null

      - ipv6_address_count                   = 0 -> null

      - ipv6_addresses                       = [] -> null

      - monitoring                           = false -> null

      - primary_network_interface_id         = "eni-0c1174dd2ee6112e4" -> null

      - private_dns                          = "ip-172-31-16-31.us-west-2.compute.internal" -> null

      - private_ip                           = "172.31.16.31" -> null

      - public_dns                           = "ec2-35-86-76-14.us-west-2.compute.amazonaws.com" -> null

      - public_ip                            = "35.86.76.14" -> null

      - secondary_private_ips                = [] -> null

      - security_groups                      = [

          - "default",

        ] -> null

      - source_dest_check                    = true -> null

      - subnet_id                            = "subnet-b88413c0" -> null

      - tags                                 = {

          - "Name" = "ExampleAppServerInstance"

        } -> null

      - tags_all                             = {

          - "Name" = "ExampleAppServerInstance"

        } -> null

      - tenancy                              = "default" -> null

      - vpc_security_group_ids               = [

          - "sg-bb8ccf8c",

        ] -> null


      - capacity_reservation_specification {

          - capacity_reservation_preference = "open" -> null

        }


      - credit_specification {

          - cpu_credits = "standard" -> null

        }


      - enclave_options {

          - enabled = false -> null

        }


      - metadata_options {

          - http_endpoint               = "enabled" -> null

          - http_put_response_hop_limit = 1 -> null

          - http_tokens                 = "optional" -> null

        }


      - root_block_device {

          - delete_on_termination = true -> null

          - device_name           = "/dev/sda1" -> null

          - encrypted             = false -> null

          - iops                  = 100 -> null

          - tags                  = {} -> null

          - throughput            = 0 -> null

          - volume_id             = "vol-04b66715193169bf7" -> null

          - volume_size           = 8 -> null

          - volume_type           = "gp2" -> null

        }

    }


Plan: 0 to add, 0 to change, 1 to destroy.


Do you really want to destroy all resources?

  Terraform will destroy all your managed infrastructure, as shown above.

  There is no undo. Only 'yes' will be accepted to confirm.


  Enter a value: yes


aws_instance.app_server: Destroying... [id=i-0d172e710677fc978]

aws_instance.app_server: Still destroying... [id=i-0d172e710677fc978, 10s elapsed]

aws_instance.app_server: Still destroying... [id=i-0d172e710677fc978, 20s elapsed]

aws_instance.app_server: Still destroying... [id=i-0d172e710677fc978, 30s elapsed]

aws_instance.app_server: Still destroying... [id=i-0d172e710677fc978, 40s elapsed]

aws_instance.app_server: Destruction complete after 48s


Destroy complete! Resources: 1 destroyed.


Note: Terraform identified a single instance with no other dependencies, so it destroyed the instance. In more complicated cases with multiple resources, Terraform will destroy them in a suitable order to respect dependencies.

Query Data with Outputs:

mohan:learn-terraform-aws-instance$ terraform apply

aws_instance.app_server: Refreshing state... [id=i-02eaef30c14d5f762]


Changes to Outputs:

  + instance_id        = "i-02eaef30c14d5f762"

  + instance_public_ip = "52.26.220.239"


You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.


Do you want to perform these actions?

  Terraform will perform the actions described above.

  Only 'yes' will be accepted to approve.


  Enter a value: yes



Apply complete! Resources: 0 added, 0 changed, 0 destroyed.


Outputs:


instance_id = "MASKED"

instance_public_ip = "MASKED"

mohan:learn-terraform-aws-instance$ terraform output
instance_id = "MASKED"
instance_public_ip = "MASKED"

Configure the remote backend:

Update main.tf to add a remote backend block to your Terraform configuration, and replace <ORG_NAME> with your organization name.


backend "remote" {

   organization = "mohico"

   workspaces {

     name = "Mohi-Workspace"

   }

 }



mohan:learn-terraform-aws-instance$ terraform init


Initializing the backend...

Acquiring state lock. This may take a few moments...

Do you want to copy existing state to the new backend?

  Pre-existing state was found while migrating the previous "local" backend to the

  newly configured "remote" backend. No existing state was found in the newly

  configured "remote" backend. Do you want to copy this state to the new "remote"

  backend? Enter "yes" to copy and "no" to start with an empty state.


  Enter a value: yes



Successfully configured the backend "remote"! Terraform will automatically

use this backend unless the backend configuration changes.


Initializing provider plugins...

- Reusing previous version of hashicorp/aws from the dependency lock file

- Using previously-installed hashicorp/aws v3.62.0


Terraform has been successfully initialized!


You may now begin working with Terraform. Try running "terraform plan" to see

any changes that are required for your infrastructure. All Terraform commands

should now work.


If you ever set or change modules or backend configuration for Terraform,

rerun this command to reinitialize your working directory. If you forget, other

commands will detect it and remind you to do so if necessary.


mohan:learn-terraform-aws-instance$ terraform apply

Running apply in the remote backend. Output will stream here. Pressing Ctrl-C

will cancel the remote apply if it's still pending. If the apply started it

will stop streaming the logs, but will not stop the apply running remotely.


Preparing the remote apply...


To view this run in a browser, visit:

https://app.terraform.io/app/mohico/Mohi-Workspace/runs/run-abcdefgh


Waiting for the plan to start...


Terraform v1.0.8

on linux_amd64

Configuring remote state backend...

Initializing Terraform configuration...

aws_instance.app_server: Refreshing state... [id=i-02eaef30c14d5f762]


No changes. Your infrastructure matches the configuration.


Terraform has compared your real infrastructure against your configuration

and found no differences, so no changes are needed.


Destroy the environment:

mohan:learn-terraform-aws-instance$ terraform destroy

Running apply in the remote backend. Output will stream here. Pressing Ctrl-C

will cancel the remote apply if it's still pending. If the apply started it

will stop streaming the logs, but will not stop the apply running remotely.


Preparing the remote apply...


To view this run in a browser, visit:

https://app.terraform.io/app/mohico/Mohi-Workspace/runs/run-Aw6YvYknVa1Empkc


Waiting for the plan to start...


Terraform v1.0.8

on linux_amd64

Configuring remote state backend...

Initializing Terraform configuration...

aws_instance.app_server: Refreshing state... [id=i-02eaef30c14d5f762]


Terraform used the selected providers to generate the following execution

plan. Resource actions are indicated with the following symbols:

  - destroy


Terraform will perform the following actions:


  # aws_instance.app_server will be destroyed

  - resource "aws_instance" "app_server" {

      - ami                                  = "ami-08d70e59c07c61a3a" -> null

      - arn                                  = "arn:aws:ec2:us-west-2:336296443354:instance/i-02eaef30c14d5f762" -> null

      - associate_public_ip_address          = true -> null

      - availability_zone                    = "us-west-2a" -> null

      - cpu_core_count                       = 1 -> null

      - cpu_threads_per_core                 = 1 -> null

      - disable_api_termination              = false -> null

      - ebs_optimized                        = false -> null

      - get_password_data                    = false -> null

      - hibernation                          = false -> null

      - id                                   = "i-02eaef30c14d5f762" -> null

      - instance_initiated_shutdown_behavior = "stop" -> null

      - instance_state                       = "running" -> null

      - instance_type                        = "t2.micro" -> null

      - ipv6_address_count                   = 0 -> null

      - ipv6_addresses                       = [] -> null

      - monitoring                           = false -> null

      - primary_network_interface_id         = "eni-03dcf1f950903db84" -> null

      - private_dns                          = "ip-172-31-27-44.us-west-2.compute.internal" -> null

      - private_ip                           = "172.31.27.44" -> null

      - public_dns                           = "ec2-MASKED.us-west-2.compute.amazonaws.com" -> null

      - public_ip                            = "MASKED" -> null

      - secondary_private_ips                = [] -> null

      - security_groups                      = [

          - "default",

        ] -> null

      - source_dest_check                    = true -> null

      - subnet_id                            = "subnet-b88413c0" -> null

      - tags                                 = {

          - "Name" = "ExampleAppServerInstance"

        } -> null

      - tags_all                             = {

          - "Name" = "ExampleAppServerInstance"

        } -> null

      - tenancy                              = "default" -> null

      - vpc_security_group_ids               = [

          - "sg-bb8ccf8c",

        ] -> null


      - capacity_reservation_specification {

          - capacity_reservation_preference = "open" -> null

        }


      - credit_specification {

          - cpu_credits = "standard" -> null

        }


      - enclave_options {

          - enabled = false -> null

        }


      - metadata_options {

          - http_endpoint               = "enabled" -> null

          - http_put_response_hop_limit = 1 -> null

          - http_tokens                 = "optional" -> null

        }


      - root_block_device {

          - delete_on_termination = true -> null

          - device_name           = "/dev/sda1" -> null

          - encrypted             = false -> null

          - iops                  = 100 -> null

          - tags                  = {} -> null

          - throughput            = 0 -> null

          - volume_id             = "vol-05c5b551e9c726edb" -> null

          - volume_size           = 8 -> null

          - volume_type           = "gp2" -> null

        }

    }


Plan: 0 to add, 0 to change, 1 to destroy.


Changes to Outputs:

  - instance_id        = "MASKED" -> null

  - instance_public_ip = "MASKED" -> null


Do you really want to destroy all resources in workspace "Mohi-Workspace"?

  Terraform will destroy all your managed infrastructure, as shown above.

  There is no undo. Only 'yes' will be accepted to confirm.


  Enter a value: yes


aws_instance.app_server: Destroying... [id=i-02eaef30c14d5f762]

aws_instance.app_server: Still destroying... [id=i-02eaef30c14d5f762, 10s elapsed]

aws_instance.app_server: Still destroying... [id=i-02eaef30c14d5f762, 20s elapsed]

aws_instance.app_server: Still destroying... [id=i-02eaef30c14d5f762, 30s elapsed]

aws_instance.app_server: Destruction complete after 31s


Apply complete! Resources: 0 added, 0 changed, 1 destroyed.



Comments