How do I get vpc id so I can create a VPC subnet in Terraform

0 votes

I am creating an AWS VPC with a single public subnet in a brand-new Terraform project, consisting only of a main.tf file. In that file, I am using two resource blocks, aws_vpc and aws_subnet. The second resource must be attached to the first using the vpc_id attribute. The value of this attribute is created only upon application, so it cannot be hard-coded. How do I get the ID of the resource I just created, so I can use it in the subsequent block?

resource "aws_vpc" "my_vpc" { cidr_block = "102.0.0.0/16" tags = { Name = "My-VPC" } } resource "aws_subnet" "my_subnet" { vpc_id = # what goes here? cidr_block = "102.0.0.0/24" tags = { Name = "My-Subnet" } }


The docs give the example of data.aws_subnet.selected.vpc_id for vpc_id. The value of this appears to depend on two other blocks, variable and data. I am having a hard time seeing how to wire these up to my VPC. I tried copying them directly from the docs. Upon running terraform plan I get the prompt:

var.subnet_id Enter a value:


This is no good; I want to pull the value from the VPC I just created, not enter it at the command prompt. Where do I specify that the data source is the resource that I just created in the previous code block?

I have heard that people create a separate file to hold Terraform variables. Is that what I should do here? It seems like it should be so basic to get an ID from one resource and use it in the next. Is there a one-liner to pass along this information?

Apr 12, 2022 in IoT (Internet of Things) by Aditya
• 7,680 points
1,418 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In IoT (Internet of Things)

0 votes
1 answer
0 votes
1 answer
0 votes
0 answers

How do I get updated temperature sensor readings with CoAPthon?

Hi guys, I'm new to IOT and I'm ...READ MORE

Nov 11, 2019 in IoT (Internet of Things) by RiceMan
• 120 points
1,225 views
0 votes
1 answer
0 votes
1 answer

How to get Unicast, Dns and Gateway Address in UWP?

Try this code Snippet I found here: https://social.msdn.microsoft.com/Forums/en-US/27a8b7a8-8071-4bc1-bbd4-e7c1fc2bd8d7/windows-10-iot-core-how-do-you-create-a-tcp-server-and-client?forum=WindowsIoT ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,231 views
0 votes
1 answer

how to access AWS S3 from Lambda in VPC

With boto3, the S3 urls are virtual by default, ...READ MORE

answered Sep 28, 2018 in AWS by Priyaj
• 58,020 points
10,132 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP