8

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

provider aws {}

在没有环境变量的情况下,aws 提供者[default]~/.aws/credentials. 但是我仍然被提示输入该区域:

>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

4

2 回答 2

8

AWS 提供商具有配置文件属性,但它不会从 .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.
于 2020-02-26T23:09:16.290 回答
2

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


data "aws_region" "current-region" {}

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

于 2020-10-31T17:39:12.767 回答