
Let's say you have an EC2 instance named "tc.micro instance 1".
This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.
Let's say you have the following files on your Terraform server.
├── required_providers.tf
├── ec2 (directory)
│ ├── data.tf
│ ├── outputs.tf
│ ├── provider.tf
│ ├── resources.tf
required_providers.tf will almost always have this.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.
provider "aws" {
alias = "default"
profile = "default"
region = "default"
}
data.tf could have something like this. In this example, tag:Name and "t2.micro instance 1" is used to find the instance tagged "t2.micro instance 1".
data "aws_instance" "my-instance" {
filter {
name = "tag:Name"
values = ["my-instance"]
}
}
The terraform refresh command should then output something like this.
my-instance = {
"ami" = "ami-026b57f3c383c2eec"
"arn" = "arn:aws:ec2:us-east-1:123456789012:instance/i-03870f20800891c34"
"associate_public_ip_address" = true
"availability_zone" = "us-east-1b"
"credit_specification" = tolist([
{
"cpu_credits" = "standard"
},
])
"disable_api_stop" = false
"disable_api_termination" = false
"ebs_block_device" = toset([])
"ebs_optimized" = false
"enclave_options" = tolist([
{
"enabled" = false
},
])
"ephemeral_block_device" = tolist([])
"filter" = toset([
{
"name" = "tag:Name"
"values" = toset([
"my-instance",
])
},
])
"get_password_data" = false
"get_user_data" = false
"host_id" = tostring(null)
"host_resource_group_arn" = tostring(null)
"iam_instance_profile" = ""
"id" = "i-03870f20800891c34"
"instance_id" = tostring(null)
"instance_state" = "running"
"instance_tags" = tomap(null) /* of string */
"instance_type" = "t2.micro"
"ipv6_addresses" = toset([])
"key_name" = "default"
"maintenance_options" = tolist([
{
"auto_recovery" = "default"
},
])
"metadata_options" = tolist([
{
"http_endpoint" = "enabled"
"http_put_response_hop_limit" = 1
"http_tokens" = "optional"
"instance_metadata_tags" = "disabled"
},
])
"monitoring" = false
"network_interface_id" = "eni-04bfc54ff3158b01a"
"outpost_arn" = ""
"password_data" = tostring(null)
"placement_group" = ""
"placement_partition_number" = tonumber(null)
"private_dns" = "ip-172-31-87-28.ec2.internal"
"private_dns_name_options" = tolist([
{
"enable_resource_name_dns_a_record" = true
"enable_resource_name_dns_aaaa_record" = false
"hostname_type" = "ip-name"
},
])
"private_ip" = "172.31.87.28"
"public_dns" = "ec2-18-234-187-17.compute-1.amazonaws.com"
"public_ip" = "18.234.187.17"
"root_block_device" = toset([
{
"delete_on_termination" = true
"device_name" = "/dev/xvda"
"encrypted" = false
"iops" = 100
"kms_key_id" = ""
"tags" = tomap({})
"throughput" = 0
"volume_id" = "vol-0ef385c7b3d5c6f8b"
"volume_size" = 8
"volume_type" = "gp2"
},
])
"secondary_private_ips" = toset([])
"security_groups" = toset([
"default",
])
"source_dest_check" = true
"subnet_id" = "subnet-0316e4d9fcd4efccc"
"tags" = tomap({
"Name" = "my-instance"
})
"tenancy" = "default"
"timeouts" = null /* object */
"user_data" = tostring(null)
"user_data_base64" = tostring(null)
"vpc_security_group_ids" = toset([
"sg-0c3296b3cd153fdc1",
])
}
Did you find this article helpful?
If so, consider buying me a coffee over at