在 Terraform 配置中引用现有 EC2 节点的公有 IP 地址



>我有一些地形提供程序,具体取决于现有EC2节点的公有IP地址。我在类似的 StackOverflow 问题中看到,您可以通过设置匹配的资源条目来导入现有节点,然后运行terraform import导入它:

terraform import aws_instance.test i-12345678

但是,当我运行它(当然使用正确的实例 ID(时,我收到此错误:

Error importing: Provider "kov" depends on non-var "aws_instance.test.0/aws_instance.test.N". Providers for import can currently
only depend on variables or must be hardcoded. You can stop import
from loading configurations by specifying `-config=""`.

上述命令的配置为:

provider "aws" {
# ...
}
resource "aws_instance" "test" {
ami = "ami-blablahblah"
instance_type = "t2.large"
# ...
}
provider "kov" {
host = "${aws_instance.test.public_ip}"
port = 8080
# ...
}

其他提供程序使用该主机和端口来配置连接到它的其他服务器。有什么想法可以让它发挥作用吗?

如果您只想以某种方式引用资源,则不必导入资源。

查看aws_instance数据源:https://www.terraform.io/docs/providers/aws/d/instance.html

data "aws_instance" "test" {
instance_id = "i-12345678"
}

然后,您应该能够从数据源访问public_ip

只是想根据@manojlds的有用答案进行跟进。工作配置如下所示:

provider "aws" {
# ...
}
data "aws_instance" "test" {
instance_type = "t2.large"
}
provider "kov" {
host = "${data.aws_instance.test.public_ip}"
# ...
}

更好的是,我可以根据其他属性筛选数据源,因此我不一定需要预先知道实例 ID。

最新更新