Step 0: create a API user and make sure that this API user has the proper access
Step 1: create instance.tf file as the below. This will creare a micro virtual instance in AWS Sydney
provider “aws” {
access_key = “myaccess_key”
secret_key = “mysecre_key”
region = “ap-southeast-2”
}
resource “aws_instance” “terraform_linux” {
ami = “ami-fe71759d”
instance_type = “t2.micro”
}
Step 2: Terraform appy to start the build
[dzhang@localhost terraform]$ terraform apply
aws_instance.terraform_linux: Creating…
ami: “” => “ami-fe71759d”
associate_public_ip_address: “” => “”
availability_zone: “” => “”
ebs_block_device.#: “” => “”
ephemeral_block_device.#: “” => “”
instance_state: “” => “”
instance_type: “” => “t2.micro”
key_name: “” => “”
network_interface_id: “” => “”
placement_group: “” => “”
private_dns: “” => “”
private_ip: “” => “”
public_dns: “” => “”
public_ip: “” => “”
root_block_device.#: “” => “”
security_groups.#: “” => “”
source_dest_check: “” => “true”
subnet_id: “” => “”
tenancy: “” => “”
vpc_security_group_ids.#: “” => “”
aws_instance.terraform_linux: Still creating… (10s elapsed)
aws_instance.terraform_linux: Still creating… (20s elapsed)
aws_instance.terraform_linux: Creation complete
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.
State path: terraform.tfstate
Step3: Verify the virtual instance has been created in AWS console
Step 4: Destroy the created Virtual Instance
[dzhang@localhost terraform]$ terraform destroy
Do you really want to destroy?
Terraform will delete all your managed infrastructure.
There is no undo. Only ‘yes’ will be accepted to confirm.
Enter a value: yes
aws_instance.terraform_linux: Refreshing state… (ID: i-04e8f2757c43a5da8)
aws_instance.terraform_linux: Destroying…
aws_instance.terraform_linux: Still destroying… (10s elapsed)
aws_instance.terraform_linux: Still destroying… (20s elapsed)
aws_instance.terraform_linux: Still destroying… (30s elapsed)
aws_instance.terraform_linux: Still destroying… (40s elapsed)
aws_instance.terraform_linux: Still destroying… (50s elapsed)
aws_instance.terraform_linux: Still destroying… (1m0s elapsed)
aws_instance.terraform_linux: Still destroying… (1m10s elapsed)
aws_instance.terraform_linux: Destruction complete
Destroy complete! Resources: 1 destroyed.
Step 5: Verify the virtual instance are terminated.
You can check the history status of the virtual instance by veiwing the content of state files: terraform.tfstate and terraform.tfstate.backup
[dzhang@localhost terraform]$ cat terraform.tfstate
{
“version”: 3,
“terraform_version”: “0.8.6”,
“serial”: 2,
“lineage”: “7da04b67-0d9d-4337-80a7-9ffe05753f83”,
“modules”: [
{
“path”: [
“root”
],
“outputs”: {},
“resources”: {},
“depends_on”: []
}
]
}
[dzhang@localhost terraform]$ cat terraform.tfstate.backup
{
“version”: 3,
“terraform_version”: “0.8.6”,
“serial”: 1,
“lineage”: “7da04b67-0d9d-4337-80a7-9ffe05753f83”,
“modules”: [
{
“path”: [
“root”
],
“outputs”: {},
“resources”: {
“aws_instance.terraform_linux”: {
“type”: “aws_instance”,
“depends_on”: [],
“primary”: {
“id”: “i-04e8f2757c43a5da8”,
“attributes”: {
“ami”: “ami-fe71759d”,
“associate_public_ip_address”: “true”,
“availability_zone”: “ap-southeast-2b”,
“disable_api_termination”: “false”,
“ebs_block_device.#”: “0”,
“ebs_optimized”: “false”,
“ephemeral_block_device.#”: “0”,
“iam_instance_profile”: “”,
“id”: “i-04e8f2757c43a5da8”,
“instance_state”: “running”,
“instance_type”: “t2.micro”,
“key_name”: “”,
“monitoring”: “false”,
“network_interface_id”: “eni-8aac01f1”,
“private_dns”: “ip-172-31-8-189.ap-southeast-2.compute.internal”,
“private_ip”: “172.31.8.189”,
“public_dns”: “ec2-52-62-134-247.ap-southeast-2.compute.amazonaws.com”,
“public_ip”: “52.62.134.247”,
“root_block_device.#”: “1”,
“root_block_device.0.delete_on_termination”: “true”,
“root_block_device.0.iops”: “100”,
“root_block_device.0.volume_size”: “8”,
“root_block_device.0.volume_type”: “gp2”,
“security_groups.#”: “0”,
“source_dest_check”: “true”,
“subnet_id”: “subnet-4d175128”,
“tags.%”: “0”,
“tenancy”: “default”,
“vpc_security_group_ids.#”: “1”,
“vpc_security_group_ids.3152370517”: “sg-c49495a1”
},
“meta”: {
“schema_version”: “1”
},
“tainted”: false
},
“deposed”: [],
“provider”: “”
}
},
“depends_on”: []
}
]
}