gpt4 book ai didi

amazon-ec2 - 如何将多个 IAM 角色附加到 AWS 上的实例配置文件?

转载 作者:行者123 更新时间:2023-12-05 08:36:25 25 4
gpt4 key购买 nike

我正在使用 Terraform 创建 IAM 和 EC2,如下所示。

我想将名为 ec2_role 的角色附加到 EC2 实例配置文件。但它似乎只能附加一个由 aws_iam_instance_profile 创建的。

resource "aws_instance" "this" {
# ..
iam_instance_profile = aws_iam_instance_profile.this.name
}

resource "aws_iam_instance_profile" "this" {
name = "ec2-profile"
role = aws_iam_role.ec2_role.name
}

关于ec2_role,它使用了ec2_role_policy。但是,如果我将 source_json = data.aws_iam_policy.amazon_ssm_managed_instance_core.policy 设置为 data "aws_iam_policy_document""ec2_role_policy"{,它会引发错误。

resource "aws_iam_role" "ec2_role" {
name = "ec2-role"
assume_role_policy = data.aws_iam_policy_document.ec2_role_policy.json
}

resource "aws_iam_policy" "ec2_policy" {
name = "ec2-policy"
policy = data.aws_iam_policy_document.ec2_use_role_policy.json
}

resource "aws_iam_role_policy_attachment" "attach" {
role = aws_iam_role.ec2_role.name
policy_arn = aws_iam_policy.ec2_policy.arn
}

data "aws_iam_policy" "amazon_ssm_managed_instance_core" {
arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}

data "aws_iam_policy_document" "ec2_role_policy" {
source_json = data.aws_iam_policy.amazon_ssm_managed_instance_core.policy

statement { # Doc A
effect = "Allow"
principals {
identifiers = ["ec2.amazonaws.com"]
type = "Service"
}
actions = ["sts:AssumeRole"]
}
}

data "aws_iam_policy_document" "ec2_use_role_policy" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
resources = ["arn:aws:iam::12313113231:role/s3-role"]
}
}

错误信息是:

Error: Error creating IAM Role ec2-role: MalformedPolicyDocument: Has prohibited field Resource
status code: 400, request id: 1111111-3333-2222-4444-2131331312

with aws_iam_role.ec2_role,
on main.tf line 10, in resource "aws_iam_role" "ec2_role":
10: resource "aws_iam_role" "ec2_role" {

如果我从 ec2_role_policy 中删除 source_json = data.aws_iam_policy.amazon_ssm_managed_instance_core.policy,它将起作用。但是怎么和Doc A一起设置呢?

最佳答案

作为@hars34 mentioned in their answer ,一个实例配置文件只能包含一个角色,但该角色可以附加多个策略。但这不是您在那里所做的,也不是错误所提示的。

相反,您似乎对 assume_role_policy 感到困惑(也称为 "trust policy" ,这控制允许哪些 IAM 委托(delegate)人使用角色,例如其他 AWS 服务或不同的 AWS 账户等)角色和角色的 permissions policy允许该角色执行的操作(例如读取和写入 S3 存储桶)。

assume_role_policy/trust 策略文档中,您必须指定一个有效的信任策略,该策略必须包含 Principal block 并且不能包含 Resource block 这就是您的错误消息所提示的内容:

Error: Error creating IAM Role ec2-role: MalformedPolicyDocument: Has prohibited field Resource
status code: 400, request id: 1111111-3333-2222-4444-2131331312

因为您已将允许 EC2 实例承担角色的信任策略与如下所示的权限策略串联起来:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssm:DescribeAssociation",
"ssm:GetDeployablePatchSnapshotForInstance",
"ssm:GetDocument",
"ssm:DescribeDocument",
"ssm:GetManifest",
"ssm:GetParameter",
"ssm:GetParameters",
"ssm:ListAssociations",
"ssm:ListInstanceAssociations",
"ssm:PutInventory",
"ssm:PutComplianceItems",
"ssm:PutConfigurePackageResult",
"ssm:UpdateAssociationStatus",
"ssm:UpdateInstanceAssociationStatus",
"ssm:UpdateInstanceInformation"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ec2messages:AcknowledgeMessage",
"ec2messages:DeleteMessage",
"ec2messages:FailMessage",
"ec2messages:GetEndpoint",
"ec2messages:GetMessages",
"ec2messages:SendReply"
],
"Resource": "*"
}
]
}

其中包含 Resource block 。

如果您希望角色能够使用 arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore 策略,并且还能够承担 arn:aws:iam: :12313113231:role/s3-role 角色(尽管将权限直接授予角色而不是使用角色链接更为正常,如果这涉及跨账户访问,则使用 the S3 bucket policy 来允许该角色) 那么你应该这样做:

resource "aws_iam_role" "ec2_role" {
name = "ec2-role"
assume_role_policy = data.aws_iam_policy_document.ec2_assume_role_policy.json
}

resource "aws_iam_policy" "ec2_permission_policy" {
name = "ec2-policy"
policy = data.aws_iam_policy_document.ec2_permission_policy.json
}

resource "aws_iam_role_policy_attachment" "attach" {
role = aws_iam_role.ec2_role.name
policy_arn = aws_iam_policy.ec2_permission_policy.arn
}

data "aws_iam_policy" "amazon_ssm_managed_instance_core" {
arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}

data "aws_iam_policy_document" "ec2_assume_role_policy" {
statement {
effect = "Allow"
principals {
identifiers = ["ec2.amazonaws.com"]
type = "Service"
}
actions = ["sts:AssumeRole"]
}
}

data "aws_iam_policy_document" "ec2_permission_policy" {
source_json = data.aws_iam_policy.amazon_ssm_managed_instance_core.policy

statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
resources = ["arn:aws:iam::12313113231:role/s3-role"]
}
}

关于amazon-ec2 - 如何将多个 IAM 角色附加到 AWS 上的实例配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69192628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com