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 in AWS by Ashwini
• 3,750 points
76 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 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,100 points
1,069 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,100 points
2,392 views
0 votes
1 answer

How to create a subnet inside a VPC using AWS CLI?

To create a subnet inside a VPC ...READ MORE

answered Feb 21, 2019 in AWS by Priyaj
• 58,100 points
341 views
0 votes
1 answer

What is the use of a Private Subnet in a VPC?

Private subnets are available across all AWS ...READ MORE

answered Mar 12, 2019 in AWS by Reena Mishra
616 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,100 points
1,544 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,460 points
7,642 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,460 points
7,198 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,460 points
1,512 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