如何使 Terraform 读取 AWS 凭证文件?

2024-01-09

我正在尝试使用 terraform 创建一个 AWS S3 存储桶,这是我的代码:

provider "aws" {
  profile = "default"
  region  = "ap-south-1"
}

resource "aws_s3_bucket" "first_tf" {
  bucket = "svk-pl-2909202022"
  acl    = "private"
}

我已使用记事本手动创建了“Credentials”文件,并使用 Powershell 删除了“.txt”扩展名,并将该文件存储在C:\Users\terraform\.aws,该文件是这样的:

[default]
aws_access_key_id=**************
aws_secret_access_key=************

但当我尝试跑步时terraform plan,我收到一个错误,上面写着

错误:配置 Terraform AWS 提供商时出错:找不到 Terraform AWS 提供商的有效凭证源

然后,我还尝试通过安装 AWS CLI 创建“凭证”文件,我运行了命令

aws configure --profile terraform

where terraform是我的用户名。所以,它要求我输入aws_access_key_id and aws_secret_access_key。输入所有凭据后,我运行了命令terraform init,它运行成功但是当我运行时terraform plan,它再次显示错误,其中显示:

错误:配置 Terraform AWS 提供商时出错:找不到 Terraform AWS 提供商的有效凭证源


Answer recommended by AWS /collectives/aws Collective

当您手动创建配置文件时

provider "aws" {
  region                  = "your region"
  shared_credentials_file = "path_file_credentials like C:\Users\terraform\.aws\credentials"
  profile                 = "profile_name"
}

当您不想手动放置共享文件时

需要在这条路径上%USERPROFILE%.aws\credentials

provider "aws" {
  region                  = "your region"
  profile                 = "profile_name"
}

如果您想将凭据放入 tf 文件中

provider "aws" {
  region     = "us-west-2"
  access_key = "my-access-key"
  secret_key = "my-secret-key"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使 Terraform 读取 AWS 凭证文件? 的相关文章

随机推荐