AWS CDK - 角色和策略创建
AWS CDK - role and policy creation
如何将此 CloudFormation 转换为 CDK(Java脚本或 Java)?我正在尝试这样做,但这是我第一次使用 CDK,我不确定该怎么做。
FargateTaskExecutionServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ecs-tasks.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: AmazonECSTaskExecutionRolePolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- 'ecr:GetAuthorizationToken'
- 'ecr:BatchCheckLayerAvailability'
- 'ecr:GetDownloadUrlForLayer'
- 'ecr:BatchGetImage'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: '*'
你应该参考API reference document以获得清晰的图片。有此类用例的示例。
然而,既然你已经在这里问过,而我的手很痒,无法为你提供答案,所以这里只是 IAM 部分的 TypeScript 实现:
import {
ManagedPolicy,
Role,
ServicePrincipal,
PolicyStatement,
Effect
} from '@aws-cdk/aws-iam';
....
....
const ecsFargateServiceRole = new Role(this, 'FargateTaskExecutionServiceRole', {
assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com')
});
// Add a policy to a Role
ecsFargateServiceRole.addToPolicy(
new PolicyStatement({
effect: Effect.ALLOW,
resources: ['*'],
actions: [
'ecr:GetAuthorizationToken',
'ecr:BatchCheckLayerAvailability',
'ecr:GetDownloadUrlForLayer',
'ecr:BatchGetImage',
'logs:CreateLogStream',
'logs:PutLogEvents'
]
})
);
// Add a managed policy to a role you can use
ecsFargateServiceRole.addManagedPolicy(
ManagedPolicy.fromAwsManagedPolicyName('AmazonECSTaskExecutionRolePolicy')
);
....
....
更新:
当您将 AWS 托管策略添加到角色时,您可以通过 its name or by its ARN 获取托管策略作为参考。重要的部分是,如果 AWS 托管策略按其名称或 ARN 如上所述使用,则您将不需要显式使用策略语句。根据我上面的回答,您可以使用托管策略方法而不是使用策略声明。
现在定义角色的一种简单方法是:
const ecsFargateServiceRole = new Role(this, 'FargateTaskExecutionServiceRole', {
assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com'),
managedPolicies: [
ManagedPolicy.fromAwsManagedPolicyName('AmazonECSTaskExecutionRolePolicy')
]
});
请注意,为了简洁起见,我排除了 Construct 的构造函数。
如何将此 CloudFormation 转换为 CDK(Java脚本或 Java)?我正在尝试这样做,但这是我第一次使用 CDK,我不确定该怎么做。
FargateTaskExecutionServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ecs-tasks.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: AmazonECSTaskExecutionRolePolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- 'ecr:GetAuthorizationToken'
- 'ecr:BatchCheckLayerAvailability'
- 'ecr:GetDownloadUrlForLayer'
- 'ecr:BatchGetImage'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: '*'
你应该参考API reference document以获得清晰的图片。有此类用例的示例。
然而,既然你已经在这里问过,而我的手很痒,无法为你提供答案,所以这里只是 IAM 部分的 TypeScript 实现:
import {
ManagedPolicy,
Role,
ServicePrincipal,
PolicyStatement,
Effect
} from '@aws-cdk/aws-iam';
....
....
const ecsFargateServiceRole = new Role(this, 'FargateTaskExecutionServiceRole', {
assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com')
});
// Add a policy to a Role
ecsFargateServiceRole.addToPolicy(
new PolicyStatement({
effect: Effect.ALLOW,
resources: ['*'],
actions: [
'ecr:GetAuthorizationToken',
'ecr:BatchCheckLayerAvailability',
'ecr:GetDownloadUrlForLayer',
'ecr:BatchGetImage',
'logs:CreateLogStream',
'logs:PutLogEvents'
]
})
);
// Add a managed policy to a role you can use
ecsFargateServiceRole.addManagedPolicy(
ManagedPolicy.fromAwsManagedPolicyName('AmazonECSTaskExecutionRolePolicy')
);
....
....
更新:
当您将 AWS 托管策略添加到角色时,您可以通过 its name or by its ARN 获取托管策略作为参考。重要的部分是,如果 AWS 托管策略按其名称或 ARN 如上所述使用,则您将不需要显式使用策略语句。根据我上面的回答,您可以使用托管策略方法而不是使用策略声明。
现在定义角色的一种简单方法是:
const ecsFargateServiceRole = new Role(this, 'FargateTaskExecutionServiceRole', {
assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com'),
managedPolicies: [
ManagedPolicy.fromAwsManagedPolicyName('AmazonECSTaskExecutionRolePolicy')
]
});
请注意,为了简洁起见,我排除了 Construct 的构造函数。