I have used this module to create a security group in AWS VPC. How do I reference the resource created from this in a separate file? I am creating our bastion instance in a separate directory in the same repo.
My bastion config looks like the following, uses the Terraform EC2 module and works if I hard code the vpc security group ID, but I want it to be able to take it directly from when the security group is created as this could change in the future..
terraform/aws/layers/bastion/main.tf
provider "aws" {
region = var.region
}
module "ec2-instance" {
source = "terraform-aws-modules/ec2-instance/aws"
name = "bastion"
instance_count. = 1
ami = var.image_id
instance_type = var.instance_type
vpc_security_group_ids = ["${}"]
subnet_id = var.subnet
iam_instance_profile = "aws-example-ec2-role"
tags = {
Layer = "Bastion"
}
}
This is how I have created the security group: terraform/aws/global/vpc/bastion_sg.tf
module "bastion-sg" {
source = "terraform-aws-modules/security-group/aws"
name = "Bastion"
description = "Bastion example group"
vpc_id = "vpc-12345"
ingress_with_cidr_blocks = [
{
from_port = ##
to_port = ##
protocol = "##"
description = "Bastion SSH"
cidr_blocks = "1.2.3.4/5"
},
{
from_port = ##
to_port = ##
protocol = "##"
description = "Bastion SSH"
cidr_blocks = "1.2.3.4/5"
}
]
egress_with_source_security_group_id = [
{
from_port = ##
to_port = ##
protocol = "##"
description = "Access to default server security group"
source_security_group_id = "sg-12345"
},
{
from_port = ##
to_port = ##
protocol = "##"
description = "Access to db"
source_security_group_id = "sg-12345"
}
]
}
Do I need to output the security group ID to outputs.tf where I have created by bastion_sg.tf before I can reference it within bastion/main.tf like below?
module "bastion_sg"
source "../../global/vpc"
and then somehow pass the ID into vpc_security_group_id = ?