如何将 aws 提供程序凭证传递给 null_resource local-exec 配置程序?

dei*_*tch 8 terraform terraform-provider-aws

有没有人想出一个合适的方法来做到这一点?

简而言之,你有一个provider "aws",通过环境变量或配置文件配置,有或没有sts,都没关系。也许你有几个。

现在您想要调用awscli,因为 aws 提供程序中的某些内容没有得到很好的实现。就我而言,我需要生成一些敏感信息并将其直接上传到我不希望出现在状态文件中的 S3 存储桶。无论如何,它是s3 sync,因此该操作是幂等的。

但是,似乎无法将提供者凭据(永久、环境变量、配置文件到临时 sts)传递给子句null_resource

provider "aws" {
  # set using explicit setting or profile or however
  alias = "myaws"
}

resource "null_resource" "cli" {
  provisioner "local-exec" {
    command = "aws <do something>"
    environment {
      # happy to pass AWS_PROFILE or AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY here...
      # if there were a way to retrieve it from the "myaws" provider
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Mbu*_*aac 1

您可以将 AWS role_arn 传递到local-exec脚本中。例如:



variable "aws_role" {
  type        = string
  description = "AWS role for local exec to assume"
  default     = "arn:aws:iam::123456789012:role/DBMigrateRole"
}

resource "null_resource" "call-db-migrate" {
  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    command = <<EOF
set -e
CREDENTIALS=(`aws sts assume-role \
  --role-arn ${var.aws_role} \
  --role-session-name "db-migration-cli" \
  --query "[Credentials.AccessKeyId,Credentials.SecretAccessKey,Credentials.SessionToken]" \
  --output text`)

unset AWS_PROFILE
export AWS_DEFAULT_REGION=us-east-1
export AWS_ACCESS_KEY_ID="$${CREDENTIALS[0]}"
export AWS_SECRET_ACCESS_KEY="$${CREDENTIALS[1]}"
export AWS_SESSION_TOKEN="$${CREDENTIALS[2]}"

aws sts get-caller-identity
EOF
  }
}
Run Code Online (Sandbox Code Playgroud)

归功于https://github.com/hashicorp/terraform-provider-aws/issues/8242#issuecomment-586687360