Unable to write policy document in aws cdk using python

给你一囗甜甜゛ 提交于 2020-01-05 07:16:12

问题


Hi I am working on AWS CDK. I am trying to create policy. Below is my code.

MWSECSServiceRole = iam.Role(self, 'MWSECSServiceRole',
          assumed_by=iam.ServicePrincipal('ecs.amazonaws.com'))

        MWSECSServiceRole.add_to_policy(iam.PolicyStatement(
        effect=iam.Effect.ALLOW,
        resources=["arn:aws:elasticloadbalancing:*:{AccountId}:loadbalancer/app/mws-*","arn:aws:elasticloadbalancing:*:{AccountId}:listener-rule/app/mws-*","arn:aws:elasticloadbalancing:*:{AccountId}:listener/app/mws-*","arn:aws:elasticloadbalancing:*:{AccountId}:targetgroup/mws-*"],
        actions=["elasticloadbalancing:DeregisterInstancesFromLoadBalancer","elasticloadbalancing:DeregisterTargets","elasticloadbalancing:RegisterInstancesWithLoadBalancer","elasticloadbalancing:RegisterTargets"]
        ))

        MWSECSServiceRole.add_to_policy(iam.PolicyStatement(
        effect=iam.Effect.ALLOW,
        resources=["*"],
        actions=["ec2:AuthorizeSecurityGroupIngress","ec2:Describe*","elasticloadbalancing:Describe*"]
        ))

Which will generate below cloud formation template.

MWSECSServiceRoleDefaultPolicyD5E258B0:
    Type: AWS::IAM::Policy
    Properties:
      PolicyDocument:
        Statement:
          - Action:
              - elasticloadbalancing:DeregisterInstancesFromLoadBalancer
              - elasticloadbalancing:DeregisterTargets
              - elasticloadbalancing:RegisterInstancesWithLoadBalancer
              - elasticloadbalancing:RegisterTargets
            Effect: Allow
            Resource:
              - arn:aws:elasticloadbalancing:*:{AccountId}:loadbalancer/app/mws-*
              - arn:aws:elasticloadbalancing:*:{AccountId}:listener-rule/app/mws-*
              - arn:aws:elasticloadbalancing:*:{AccountId}:listener/app/mws-*
              - arn:aws:elasticloadbalancing:*:{AccountId}:targetgroup/mws-*
          - Action:
              - ec2:AuthorizeSecurityGroupIngress
              - ec2:Describe*
              - elasticloadbalancing:Describe*
            Effect: Allow
            Resource: "*"
        Version: "2012-10-17"
      PolicyName: MWSECSServiceRoleDefaultPolicyD5E258B0
      Roles:
        - Ref: MWSECSServiceRole966AC1F9
    Metadata:
      aws:cdk:path: LocationCdkStack-cdkstack/MWSECSServiceRole/DefaultPolicy/Resource

When I try to deploy It throws below error.

The policy failed legacy parsing (Service: AmazonIdentityManagement; Status Code: 400; Error Code: MalformedPolicyDocument; Request ID: e54462f7-f0bc-4a8c-9ec4-9530125113ec)

Can someone help me to identify this issue? Any help would be appreciated. Thanks


回答1:


I suggest you build your ARN using Stack.format_arn:

my_resource = core.Stack.of(self).format_arn(
  service="elasticloadbalancing",
  resource="loadbalancer",
  resource_name="app/mws-*"
)

See also ARN Manipulation.

Alternatively you can concatenate string and use core.Stack.of(self).account:

my_resource = "arn:aws:elasticloadbalancing:*:" + core.Stack.of(self).account + ":loadbalancer/app/mws-*"


来源:https://stackoverflow.com/questions/59261713/unable-to-write-policy-document-in-aws-cdk-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!