如何访问 Terraform 脚本中资源内的 Terraform 提供者属性?

How to access terraform provider attributes within resources in terraform script?

我正在尝试创建一个基本的 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"
          }
    }

我想像回显 aws 实例 public ip 一样回显 provider region

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

Error: Reference to undeclared resource

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

谢谢

您需要使用数据组件才能get to the current region:

data "aws_region" "current" {}

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