Terraform-AWS-Modules-using subnet-id of a VPC child module in another ec2 child module

0 votes

I will start by thanking you for your time and I have been googling and reading and maybe just overlooking something very simple. I have tried my best with some articles on medium and the terraform documentation.

So, my problem is.. I have a root module that looks like this

module "VPC" {
    source = "/home/jamie/Terraform_Project/modules/1_VPC/"
    subnet_id = "module.VPC.Public_Subnet_id" 
}

module "Key_Pair" {
    source = "/home/jamie/Terraform_Project/modules/2_Key_Pair/"
}

module "EC2_VPN" {
    source = "/home/jamie/Terraform_Project/modules/3_EC2_VPN/"
}

and three child modules as you can see. I cannot reference the "Public_Subnet_ID" from my VPC module in my EC2 module. I will show my main.tfs and my output.tfs below. I think its worth mentioning that I have tried various things I have found on google and don't seem to get anywhere below is my latest attempt. i have seen other answers on stackoverflow but they have not worked for me or i am still doing something wrong.

VPC - main.tf (will show subnet bit only)

/* Public Subnet */
resource "aws_subnet" "Public_Subnet" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
  map_public_ip_on_launch = true

  tags = {
    Name = "Public"
    Project = "${var.project}"
    Architect = "${var.architect}"
  }
}

VPC - output.tf (2 options i have tried)

**this**
output "Public_Subnet_id" {
    value = "${aws_subnet.Public_Subnet.id}"
}
**or this**
output "Public_Subnet_id" {
    value = aws_subnet.Public_Subnet.id
}

**EC2 - main.tf (problem bit)

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
  subnet_id = "module.1_VPC.Public_Subnet_id"
  key_name = "${var.project}_Key"

  tags = {
    Name = "VPN_Server"
    Project = "${var.project}"
    Architect = "${var.architect}"
  }
}

i also tried the above with variable (maybe wrong from another thread/guide)

my first errors where the "module.1_VPC.Public_Subnet_id" wasnt referenced but managed to get that bit but now it just ends up with

Error: creating EC2 Instance: InvalidSubnetID.NotFound: The subnet ID 'module.1_VPC.Public_Subnet_id' does not exist │ status code: 400, request id: 00fa3944-4ea3-450b-9fd4-39645785269f │ │ with module.EC2_VPN.aws_instance.web, │ on .terraform/modules/EC2_VPN/main.tf line 17, in resource "aws_instance" "web": │ 17: resource "aws_instance" "web" {

Again thankyou for taking the time, I am learning and trying to build / learn as I go not just copy and paste other templates.

tried various guides / terraform docs (most ref modules but in same file not separated folders)

i just need to be able to export a resourse_id for use in another child modules. once i can do this i will be able to duplicate for security groups and anything else i need to ref.

Feb 16, 2023 in AWS by Ashwini
• 5,430 points
949 views

1 answer to this question.

0 votes

It looks like the issue is with how you are referencing the output variable Public_Subnet_id from the VPC module in your EC2 module. Here are some things you can try to resolve this issue:

  1. First, make sure that the Public_Subnet_id output variable is correctly defined in the output.tf file of the VPC module. You can check this by running terraform output Public_Subnet_id in the VPC module directory and verifying that the output is the expected subnet ID.

  2. In your EC2 module's main.tf file, instead of using a string for the subnet_id parameter, use an interpolation to reference the output variable from the VPC module. Here's an example of what the code might look like:

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
  subnet_id = module.VPC.Public_Subnet_id
  key_name = "${var.project}_Key"

  tags = {
    Name = "VPN_Server"
    Project = "${var.project}"
    Architect = "${var.architect}"
  }
}

Note that in the subnet_id parameter, we are using the syntax module.VPC.Public_Subnet_id to reference the output variable from the VPC module.

  1. Make sure that the VPC module has been successfully applied before running the EC2 module. You can do this by running terraform apply in the VPC module directory, and then running terraform apply in the EC2 module directory.

I hope this helps!

Propel Your Skills with Comprehensive Microservices Training!

answered Feb 17, 2023 by sarit
• 1,830 points

Related Questions In AWS

0 votes
1 answer

Pass account id of an AWS sub account using a variable as an argument in CloudWatch Alarm Actions with python (boto3)?

Python String and Integer concatenation >>> print("arn:aws:swf:us-east-2:{0}:action/actions/AWS_EC2.InstanceId.Stop/1.0".format(acccnum)) arn:aws:swf:us-east-2:12312312312312:action/actions/AWS_EC2.InstanceId.Stop/1.0 >>> print("arn:aws:swf:us-east-2:" ...READ MORE

answered Oct 5, 2018 in AWS by Priyaj
• 58,090 points
1,357 views
0 votes
1 answer

How to Pass the VPC ID while creating the Ec2 instance in AWS using Python Boto3

import boto3 ec2 = boto3.resource('ec2') instance = ec2.create_instances( ...READ MORE

answered Jan 29, 2019 in AWS by Priyaj
• 58,090 points
2,928 views
0 votes
1 answer

Create Snapshot of EBS and attach to EC2 using Terraform

You can also create a snapshot using ...READ MORE

answered Oct 29, 2018 in AWS by Priyaj
• 58,090 points
1,903 views
0 votes
1 answer

How to attach an EBS volume to EC2 instance using terraform?

Hi@akhtar, You can use aws_volume_attachment resource to attach ...READ MORE

answered Jun 12, 2020 in Terraform by MD
• 95,440 points
8,445 views
0 votes
1 answer

How to attach Elastic IP in EC2 instance using Terraform code?

Hi@akhtar, You can use aws_eip resource in your ...READ MORE

answered Jul 22, 2020 in Terraform by MD
• 95,440 points
8,161 views
+2 votes
1 answer

What is the difference between modules and workspaces in Terraform?

Hi@akhtar, I think a key difference between Terraform ...READ MORE

answered Aug 17, 2020 in Terraform by MD
• 95,440 points
1,890 views
0 votes
1 answer
0 votes
1 answer

DMZ kind of network design in the AKS

It sounds like you want to implement ...READ MORE

answered Feb 17, 2023 in AWS by sarit
• 1,830 points
413 views
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