简体   繁体   English

使用 CDK 创建自定义 AWS IAM 策略

[英]create custom AWS IAM policy using CDK

As per doc : https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-iam.Policy.html根据文档: https : //docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-iam.Policy.html

I could create my own policy and attach to role but it is not creating a new policy rather attached as inline policy to the role.我可以创建自己的策略并附加到角色,但它不会创建新策略,而是作为内联策略附加到角色。 I want to create a custom policy with an arn so that I can attach to other roles I want我想使用 arn 创建自定义策略,以便我可以附加到我想要的其他角色

Things I tried我尝试过的事情

new Policy(..)
new iam.PolicyStatement()
addStatements()
attachToRole()

Please let me know how can I create custom managed policy请让我知道如何创建自定义托管策略

I should have been more specific in the previous question and should have said to use ManagedPolicy .我应该在上一个问题中更具体,应该说使用ManagedPolicy Here is a the solution you are looking for :这是您正在寻找的解决方案:

 const role = new Role(this, 'MyRole', {
   assumedBy: new ServicePrincipal('ec2.amazonaws.com'),
 });

 const policy = new ManagedPolicy(this, "MyManagedPolicy", {
   statements: [
     new PolicyStatement({
       effect: Effect.ALLOW,
       actions: ["s3:*"],
       resources: ["*"]
     })
   ],
   roles: [role]
 });

 // Either use roles property inside ManagedPolicy or use attachToRole below,
 // Both will yield the same result
 // Creates a managed policy and then attaches the policy to role

 // policy.attachToRole(role);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM