0

I'm Terraform newbie. I have vpc module and ec2 module.

  • VPC module creates 3 public subnets.
  • Ec2 module creates 2 instances.

-> So I want that public subnet 1 used by the 1st instance, public subnet 3 used by the 2nd instance. I used output variables, but I don't know how to work with output. Pls, help me

The provider is AWS

VPC module

#/module/vpc/main.tf resource "aws_subnet" "pub_subnet" { count = var.create_vpc && length(var.public_subnet) > 0 ? length(var.public_subnet) : 0 vpc_id = local.vpc_id availability_zone = element(var.azs, count.index) cidr_block = element(var.public_subnet, count.index) map_public_ip_on_launch = true # network_acls = aws_network_acl.nacls tags = merge ({ Name = format("Pub_subnet %s", element(var.public_subnet, count.index)) }, var.tags ) } 
#module/vpc/output output "public_subnet_id" { value = aws_subnet.pub_subnet[0].id } 

EC2 Module

#/module/ec2/variables variable "ec2_instance" { type = map(object({ ec2_name = string ec2_ami = string ec2_instance_type = string ec2_subnet_id = string ec2_associate_public_ip_address = string ec2_key_name = string ec2_security_groups = list(string) ec2_user_data = string root_block_device_volume_type = string root_block_device_volume_size = number root_block_device_iops = number root_block_device_throughput = number })) } 
#/module/vpc/main.tf resource "aws_instance" "ec2" { for_each = var.ec2_instance ami = each.value.ec2_ami instance_type = each.value.ec2_instance_type subnet_id = each.value.ec2_subnet_id associate_public_ip_address = each.value.ec2_associate_public_ip_address key_name = each.value.ec2_key_name security_groups = each.value.ec2_security_groups user_data = each.value.ec2_user_data root_block_device { volume_type = each.value.root_block_device_volume_type volume_size = each.value.root_block_device_volume_size iops = each.value.root_block_device_iops throughput = each.value.root_block_device_throughput } tags = merge ({ Name = format("%s", each.value.ec2_name) }, var.tags ) } 

Root module

#/main.tf module "vpc" { source = "../../modules/ec2" public_subnet = [ "13.9.1.0/24", "13.9.2.0/24", "13.9.7.0/24" ] } module "ec2"{ source = "../../modules/ec2" ec2_instance = { "bastion_pub_sub_1" = { ec2_name = "bastion_pub_sub_1" ec2_ami = data.aws_ami.ubuntu_20_04.id ec2_associate_public_ip_address = true ec2_instance_type = "t2.micro" ec2_key_name = #secret ec2_subnet_id = module.vpc.public_subnet_id ec2_user_data = "value" root_block_device_iops = 3000 root_block_device_throughput = 125 root_block_device_volume_size = 8 root_block_device_volume_type = "gp3" }, source = "../../qa_modules/ec2" ec2_instance = { "bastion_pub_sub_2" = { ec2_name = "bastion_pub_sub_2" ec2_ami = data.aws_ami.ubuntu_20_04.id ec2_associate_public_ip_address = true ec2_instance_type = "t2.micro" ec2_key_name = #secret ec2_subnet_id = module.vpc.public_subnet_id ec2_user_data = "value" root_block_device_iops = 3000 root_block_device_throughput = 125 root_block_device_volume_size = 8 root_block_device_volume_type = "gp3" } } 

So, at ec2_subnet_id = module.vpc.public_subnet_id, how can i add the subnet "13.9.1.0/24" for instance "bastion_pub_sub_1", subnet "13.9.7.0/24" for instance "bastion_pub_sub_2"

4
  • You have to show your code, and explain what's wrong with it, provide any errors, and describe exactly what you want to achieve. Commented Nov 19, 2021 at 4:35
  • @Marcin sry, i have update. Help me pls Commented Nov 19, 2021 at 6:37
  • How many subnets do you have? What is var.public_subnet? Commented Nov 19, 2021 at 6:40
  • @Marcin updated subnet. i have 3 subnet [ "13.9.1.0/24", "13.9.2.0/24", "13.9.7.0/24" ]. and 2 instance Commented Nov 19, 2021 at 6:44

1 Answer 1

1

Your outputs should be:

output "public_subnet_id" { value = aws_subnet.pub_subnet[*].id } 

then:

module "ec2"{ source = "../../modules/ec2" ec2_instance = { "bastion_pub_sub_1" = { ec2_name = "bastion_pub_sub_1" ec2_ami = data.aws_ami.ubuntu_20_04.id ec2_associate_public_ip_address = true ec2_instance_type = "t2.micro" ec2_key_name = #secret ec2_subnet_id = module.vpc.public_subnet_id[0] ec2_user_data = "value" root_block_device_iops = 3000 root_block_device_throughput = 125 root_block_device_volume_size = 8 root_block_device_volume_type = "gp3" }, source = "../../qa_modules/ec2" ec2_instance = { "bastion_pub_sub_2" = { ec2_name = "bastion_pub_sub_2" ec2_ami = data.aws_ami.ubuntu_20_04.id ec2_associate_public_ip_address = true ec2_instance_type = "t2.micro" ec2_key_name = #secret ec2_subnet_id = module.vpc.public_subnet_id[2] ec2_user_data = "value" root_block_device_iops = 3000 root_block_device_throughput = 125 root_block_device_volume_size = 8 root_block_device_volume_type = "gp3" } } 
Sign up to request clarification or add additional context in comments.

5 Comments

ok, thanks. let me try it
nice!!! thank you for your help, I solved it. it took me 1 day for solving. thank god you are here
@LamboOP No problem. Acceptance of the answer would be appreciated if it helped.
I voted this answer is useful but I'm a new member of StackOverflow :(
@LamboOP To accept have to press a tick symbol as shown here meta.stackexchange.com/a/86979

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.