您的角色是为需要该策略才能使用其他资源的服务创建的,我不确定这是否有帮助。
您需要创建一个角色,该角色将能够承担,ec2、sagemaker、s3 等。ECR 不承担任何角色,因为它只是一个注册表。
例如,我有一个 Sagemaker 实例:
resource "aws_iam_role" "sagemaker_model" {
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "sagemaker.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
然后该实例需要使用其他资源 (ECR) 的权限:
resource "aws_iam_policy" "ecr" {
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ecr:*"
],
"Resource": [
"*"
]
}
]
}
EOF
}
然后我将该策略附加到前一个角色:
resource "aws_iam_role_policy_attachment" "model_attach_ecr" {
role = aws_iam_role.sagemaker_model.name
policy_arn = aws_iam_policy.ecr.arn
}
尽管 ECR 有一个特定的属性,它有自己的访问策略,但您需要允许先前创建的角色可以通过创建一个来访问特定的容器注册表aws_ecr_repository_policy
:
resource "aws_ecr_repository_policy" "policy" {
repository = <aws_ecr_repository.repo.name>
policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "new statement",
"Effect": "Allow",
"Principal": {
"AWS": "${aws_iam_role.sagemaker_model.arn}"
},
"Action": [
"ecr:*"
]
}
]
}
EOF
}
在这一个中,您需要用<aws_ecr_repository.repo.name>
实际的存储库名称替换。
我希望这有帮助。