如何引用由 Terraform 模块创建的资源 [英] How to reference a resource created by a Terraform module
问题描述
我正在使用 AWS VPC Terraform 模块 创建一个 VPC.此外,我想使用 aws_internet_gateway 创建 Internet 网关并将其附加到此 VPC资源.
I'm using the AWS VPC Terraform module to create a VPC. Additionally, I want to create and attach an Internet Gateway to this VPC using the aws_internet_gateway resource.
这是我的代码:
module "vpc" "vpc_default" {
source = "terraform-aws-modules/vpc/aws"
name = "${var.env_name}-vpc-default"
cidr = "10.0.0.0/16"
enable_dns_hostnames = true
}
resource "aws_internet_gateway" "vpc_default_igw" {
vpc_id = "${vpc.vpc_default.id}"
tags {
Name = "${var.env_name}-vpc-igw-vpcDefault"
}
}
当我运行 terraform init
时,我收到以下错误:
When I run terraform init
, I get the following error:
正在初始化模块...- 模块.vpc
Initializing modules... - module.vpc
错误:资源aws_internet_gateway.vpc_default_igw"配置:变量 vpc.vpc_default.id 中引用的未知资源vpc.vpc_default"
Error: resource 'aws_internet_gateway.vpc_default_igw' config: unknown resource 'vpc.vpc_default' referenced in variable vpc.vpc_default.id
如何引用由 Terraform 模块创建的资源?
How can I reference a resource created by a Terraform module?
推荐答案
由于您使用的是模块,您需要稍微更改引用的格式.模块输出 使用 ${module.<模块名称>.<输出名称>}
.还需要注意的是,您只能引用从模块输出的值.
Since you're using a module, you need to change the format of the reference slightly. Module Outputs use the form ${module.<module name>.<output name>}
. It's also important to note, you can only reference values outputted from a module.
在您的具体情况下,这将成为 ${module.vpc.vpc_id}
基于 VPC 模块的输出.
In your specific case, this would become ${module.vpc.vpc_id}
based on the VPC Module's Outputs.
这篇关于如何引用由 Terraform 模块创建的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!