0
0
Terraformcloud~5 mins

Querying existing resources in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to use resources that already exist outside your Terraform setup. Querying existing resources lets you get their details so you can use them in your Terraform configuration without recreating them.
When you want to use an existing cloud network in your Terraform project without creating a new one.
When you need to attach a new server to an existing database managed outside Terraform.
When you want to reference an existing storage bucket for your application files.
When you want to get the ID of an existing virtual machine to add security rules.
When you want to import details of existing infrastructure to avoid duplication.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

# Data block to query an existing VPC by its ID

data "aws_vpc" "existing_vpc" {
  id = "vpc-0a1b2c3d4e5f6g7h"
}

# Use the queried VPC ID in a new subnet
resource "aws_subnet" "example_subnet" {
  vpc_id            = data.aws_vpc.existing_vpc.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "us-east-1a"
}

The provider block sets the AWS region.

The data "aws_vpc" "existing_vpc" block queries an existing VPC by its ID.

The aws_subnet resource creates a new subnet inside the existing VPC using the queried VPC ID.

Commands
Initializes the Terraform working directory and downloads the AWS provider plugin.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) 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.
Shows the execution plan. It will display the new subnet resource to be created using the existing VPC data.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_subnet.example_subnet will be created + resource "aws_subnet" "example_subnet" { + arn = (known after apply) + availability_zone = "us-east-1a" + cidr_block = "10.0.1.0/24" + id = (known after apply) + vpc_id = "vpc-0a1b2c3d4e5f6g7h" } Plan: 1 to add, 0 to change, 0 to destroy.
Applies the plan and creates the new subnet inside the existing VPC without manual approval.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_subnet.example_subnet: Creating... aws_subnet.example_subnet: Creation complete after 3s [id=subnet-1234abcd] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips manual approval prompt to apply changes immediately
Displays the current state of the infrastructure managed by Terraform, including the new subnet and the referenced existing VPC ID.
Terminal
terraform show
Expected OutputExpected
... # aws_subnet.example_subnet: resource "aws_subnet" "example_subnet" { availability_zone = "us-east-1a" cidr_block = "10.0.1.0/24" id = "subnet-1234abcd" vpc_id = "vpc-0a1b2c3d4e5f6g7h" } ...
Key Concept

If you remember nothing else from this pattern, remember: data blocks let you safely use existing resources without recreating them.

Common Mistakes
Trying to create a resource with the same name or ID as an existing resource without querying it first.
Terraform will try to create a duplicate resource, causing conflicts or errors.
Use a data block to query the existing resource and reference its ID instead of creating a new one.
Using incorrect or non-existent IDs in the data block.
Terraform will fail to find the resource and return an error during planning or applying.
Verify the exact resource ID from your cloud provider before using it in the data block.
Summary
Use data blocks in Terraform to query existing resources by their IDs or attributes.
Run 'terraform init' to prepare your environment and 'terraform plan' to see changes before applying.
Apply changes with 'terraform apply' to create new resources that use existing ones safely.