英文:
Set the name of an automatically created Role in the CDK
问题
以下是您要翻译的内容:
例如,在创建任务定义时:
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDefinition', {
networkMode: ecs.NetworkMode.AWS_VPC,
})
这会自动创建并命名角色。
从文档中可以看到,有一个属性用于设置角色,但它接受IRole
而不是名称。
是否有一种方法可以为自动创建的角色命名?
或者,我想我应该提前使用class Role创建角色并将其附加到Ec2TaskDefinition
,但是如何知道角色属性是什么?
英文:
For example, when making a task definition:
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDefinition', {
networkMode: ecs.NetworkMode.AWS_VPC,
})
This makes the role automatically, and named automatically.
From the document, there is property to set the role, but it accepts IRole
not name.
Is there any method to name the role which is automatically created?
Alternatively, I suppose I should use class Role to make role in advance and attache this to the Ec2TaskDefinition
?, however how can I know the role property?
答案1
得分: 1
An ECS TaskDefinition has two role props, the executionRole and the taskRole.
如果您将一个角色构造传递给这些属性,您可以直接设置角色名称。如果您不设置这些属性,您可以使用逃生口语法来修改自动创建的默认角色的名称:
if (!taskDefinition.taskRole) {
throw new Error("The Task Role is undefined");
}
const cfnTaskRole = taskDefinition.taskRole.node.defaultChild as iam.CfnRole;
cfnTaskRole.addPropertyOverride("RoleName", "my-task-role");
1 参见AWS Elastic Container Service(ECS)执行角色和任务角色的区别
英文:
An ECS TaskDefinition has two role props, the executionRole and the taskRole.<sup>1</sup> If you pass a role construct to these props, you can set the role name directly. If you don't set the props, you can use escape hatch syntax to modify the name of the automatically created default roles:
if (!taskDefinition.taskRole) {
throw new Error("The Task Role is undefined");
}
const cfnTaskRole = taskDefinition.taskRole.node.defaultChild as iam.CfnRole;
cfnTaskRole.addPropertyOverride("RoleName", "my-task-role");
[1] See Difference between AWS Elastic Container Service's (ECS) ExecutionRole and TaskRole
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论