我刚刚转移到使用 Control Tower 设置的多帐户,并且正在使用 Terraform 在不同帐户中部署资源。
我的(简化的)帐户结构是:
|--Master
|--management (backends etc)
|--images (s3, ecr)
|--dev
|--test
作为一个简化的实验,我试图在图像帐户中创建一个 ecr。所以我认为我需要创建一个策略来启用角色切换并在目标帐户中提供权限。现在我手头很重,只是试图切换到管理员访问权限。AWSAdministratorAccess 角色由 Control Tower 在配置时创建。
provider "aws" {
region = "us-west-2"
version = "~> 3.1"
}
data "aws_iam_group" "admins" { // want to attach policy to admins to switch role
group_name = "administrators"
}
// Images account
resource "aws_iam_policy" "images-admin" {
name = "Assume-Role-Images_Admin"
description = "Allow assuming AWSAdministratorAccess role on Images account"
policy = <<EOP
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sts:AssumeRole"
],
"Resource": "arn:aws:iam::<Images_Account_ID>:role/AWSAdministratorAccess"
}
]
}
EOP
}
resource "aws_iam_group_policy_attachment" "assume-role-images-admin" {
group = data.aws_iam_group.admins.group_name
policy_arn = aws_iam_policy.images-admin.arn
}
部署此堆栈后,我尝试部署另一个堆栈,该堆栈在图像帐户中创建资源。
provider "aws" {
region = var.region
version = "~>3.1"
}
provider "aws" {
alias = "images"
region = var.region
version = "~> 3.1"
assume_role {
role_arn = "arn:aws:iam::<Images_Account_ID>:role/AWSAdministratorAccess"
}
}
resource "aws_ecr_repository" "boot-images" {
provider = aws.images
name = "boot-images"
}
在部署时我得到:
> Error: error configuring Terraform AWS Provider: IAM Role (arn:aws:iam::*********:role/AWSAdministratorAccess) cannot be assumed.
There are a number of possible causes of this - the most common are:
* The credentials used in order to assume the role are invalid
* The credentials do not have appropriate permission to assume the role
* The role ARN is not valid
Error: NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
第一个:提供的凭据来自始终在单帐户环境中工作的主帐户
第二:这是我认为通过附加政策实现的
第三:对此不太确定,但帐户中存在 AWSAdministratorAccess defo,我认为 arn 格式是正确的,而 AWS Single Sign On 将其称为权限集,控制台也将其描述为角色。
我发现使用 Terraform 部署到多个 AWS 账户?这很有帮助,但我在这里遗漏了一些东西。
我也不知道如何将这个想法扩展到将 s3 远程后端部署到我的“管理”帐户中。
Terraform 版本 0.12.29