wes*_*ywh 4 amazon-web-services terraform terragrunt
我一生都无法让它发挥作用。我需要AWS_PROFILE设置环境变量才能使 terragrunt 正常运行。如果我运行:
export AWS_PROFILE=myprofile; terragrunt plan
Run Code Online (Sandbox Code Playgroud)
这会起作用,但这不是我想要的只是运行:
terragrunt plan
Run Code Online (Sandbox Code Playgroud)
并让其自动选择我应该使用的正确的 aws 配置文件。这是我所拥有的:
generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
provider "aws" {
region = "${local.region}"
profile = "${trimspace(run_cmd("bash", "${get_parent_terragrunt_dir()}/../../set_profile.sh",local.profile))}"
}
EOF
}
remote_state {
backend = "s3"
generate = {
path = "backend.tf"
if_exists = "overwrite"
}
config = {
...
...
region = local.region
profile = local.profile
...
...
}
}
Run Code Online (Sandbox Code Playgroud)
它总是向我抛出错误:
Error finding AWS credentials (did you set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables?): NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
Run Code Online (Sandbox Code Playgroud)
脚本set_profile.sh如下:
#!/bin/bash
VALUE=$(echo $1 | sed $'s/\r//')
export AWS_PROFILE=$VALUE
echo "$AWS_PROFILE"
Run Code Online (Sandbox Code Playgroud)
如果我回显我的AWS_PROFILE它仍然是空白的。因此,运行命令实际上并未将导出值保存到我的控制台。
我究竟做错了什么?有没有人真正成功地能够AWS_PROFILE使用 terragrunt 动态设置它们?
小智 6
这就是我的解决方案。我有以下结构:
<project>
|-- <region1>
|-- <region2>
|-- account.hcl
terragrunt.hcl
Run Code Online (Sandbox Code Playgroud)
在account.hcl
locals {
aws_profile_name = "myprofile"
}
Run Code Online (Sandbox Code Playgroud)
在主要terragrunt.hcl
locals {
# Automatically load account-level variables
account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
aws_profile = local.account_vars.locals.aws_profile_name
}
terraform {
extra_arguments "aws_profile" {
commands = [
"init",
"apply",
"refresh",
"import",
"plan",
"taint",
"untaint"
]
env_vars = {
AWS_PROFILE = "${local.aws_profile}"
}
}
}
remote_state {
...
config = {
...
profile = "${local.aws_profile}"
}
}
generate "provider" {
...
contents
contents = <<EOF
provider "aws" {
profile = "${local.aws_profile}"
}
EOF
}
...
Run Code Online (Sandbox Code Playgroud)
这篇文章帮助我解决了我的问题:
我忘记了我的配置有 2 个要设置的 AWS 连接这一事实
因此 AWS 配置文件必须设置两次:
remote_state
remote_state {
backend = "s3"
config = {
...
profile = local.profile
...
}
}
Run Code Online (Sandbox Code Playgroud)
provider.tf
generate "provider" {
path = "provider.tf"
if_exists = "skip"
contents = <<EOF
provider "aws" {
...
profile = "${local.profile}"
...
}
EOF
}
Run Code Online (Sandbox Code Playgroud)
希望这可以拯救我今天浪费的所有时间!
| 归档时间: |
|
| 查看次数: |
9411 次 |
| 最近记录: |