【问题标题】:How to access terraform provider attributes within resources in terraform script?如何在 terraform 脚本的资源中访问 terraform 提供程序属性?
【发布时间】:2020-03-17 02:41:56
【问题描述】:

我正在尝试创建一个基本的 terraform 脚本以在 aws 上启动 ec2。暂时的。我正在使用local-exec 供应商。脚本如下所示。

    provider "aws" {
      profile = "default"
      region  = "eu-central-1"
      version = "2.53"
    }

    resource "aws_instance" "gsb_ec2" {
      ami           = "ami-0b418580298265d5c"
      instance_type = "t2.micro"

      provisioner "local-exec" {
        command = "echo ${aws_instance.gsb_ec2.public_ip} > ip_address.txt"

      }
      provisioner "local-exec" {
        command = "echo ${aws_instance.gsb_ec2.public_ip} > ip_address.txt"
          }
    }

我想回显 provider region,就像我回显 aws 实例公共 ip。

我收到以下错误,因为提供者显然不是资源。

Error: Reference to undeclared resource

那么,如何访问提供者属性?

谢谢

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    您需要使用数据组件来get to the current region

    data "aws_region" "current" {}
    

    然后你可以回显data.aws_region.current.name

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 2022-08-08
      • 2021-12-27
      • 1970-01-01
      • 2021-07-17
      • 2021-12-28
      • 2017-04-21
      相关资源
      最近更新 更多