【问题标题】:Terraform aws provider - how to use default region from ~/.aws/configTerraform aws 提供程序 - 如何使用 ~/.aws/config 中的默认区域
【发布时间】:2020-06-09 18:07:27
【问题描述】:

在我的 main.tf 中,我定义了一个空的 aws 提供程序

provider aws {}

在没有环境变量的情况下,aws 提供程序从~/.aws/credentials 中选择[default] 凭据。但是还是会提示我输入区域:

>terraform plan
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Enter a value: 

如何让 aws 提供程序自动获取[default] 中定义的~/.aws/config 凭据的对应区域?

【问题讨论】:

  • 你也可以作为变量赋值。

标签: terraform terraform-provider-aws aws-regions


【解决方案1】:

AWS 提供商具有 profile 属性,但它不会从 .aws/config 中获取区域。

$ cat main.tf
provider aws {
     profile="default"
}

$ terraform plan
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.
...

我现在能想到的方式是使用环境变量(我就是这样用的)。

$ export AWS_DEFAULT_REGION=$(aws configure get region --profile default)
$ terraform plan
Refreshing Terraform state in-memory prior to plan...
...

------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

【讨论】:

  • 可悲的是,人们会认为 terraform 会像 aws cli 使用它一样获取该区域。无论如何,感谢您的出色领导。
【解决方案2】:

如果您拥有provider 块的唯一原因是在代码中引用该区域,那么您可以简单地使用aws_region data source,它允许您引用当前区域而不是拥有provider 块(在这种情况下,我相信应该从默认配置文件中选择区域)


data "aws_region" "current-region" {}

// Then get the region using
data.aws_region.current-region.name 

【讨论】:

    猜你喜欢
    • 2020-05-20
    • 2020-09-24
    • 2019-06-10
    • 1970-01-01
    • 2020-09-03
    • 2020-08-11
    • 1970-01-01
    • 2020-09-07
    • 2022-07-28
    相关资源
    最近更新 更多