将自动创建的角色的名称设置为CDK中的名称。

huangapple go评论58阅读模式
英文:

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(&quot;The Task Role is undefined&quot;);
}

const cfnTaskRole = taskDefinition.taskRole.node.defaultChild as iam.CfnRole;

cfnTaskRole.addPropertyOverride(&quot;RoleName&quot;, &quot;my-task-role&quot;);

[1] See Difference between AWS Elastic Container Service's (ECS) ExecutionRole and TaskRole

huangapple
  • 本文由 发表于 2023年5月14日 13:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定