我已经使用这个模块在 AWS VPC 中创建了一个安全组。如何在单独的文件中引用由此创建的资源?我在同一个 repo 的单独目录中创建我们的堡垒实例。
我的堡垒配置如下所示,使用 Terraform EC2 模块,如果我硬编码 vpc 安全组 ID,它就可以工作,但我希望它能够直接从创建安全组时获取它,因为这可能会在未来发生变化..
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"
}
}
这就是我创建安全组的方式: 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"
}
]
}
我是否需要将安全组 ID 输出到由 bastion_sg.tf 创建的 output.tf 中,然后才能在 bastion/main.tf 中引用它,如下所示?
module "bastion_sg"
source "../../global/vpc"
然后以某种方式将 ID 传递给 vpc_security_group_id = ?