shou2017.com
JP

Setting multiple IAM Resources with serverless framework (AWS)

Wed May 11, 2022
Sat Aug 10, 2024
AWS

I wanted to set multiple IAM Resource entries at once in the serverless framework for AWS.

Usually, you write this in yml, but I wasn’t sure how to do it, so I made a note. Sometimes I think Terraform would be more convenient for this kind of thing.

Data Source: aws_iam_policy_document

Ideally, you want to be able to write it like this:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "quicksight:GetDashboardEmbedUrl"
            ],
            "Resource": [
                "arn:aws:quicksight:ap-southeast-1:123456:dashboard/a",
                "arn:aws:quicksight:ap-southeast-1:654123:dashboard/b"
            ],
            "Effect": "Allow"
        }
    ]
}

So, how do you write this in YAML? Like this:

PolicyDocument:
  Version: "2012-10-17"
  Statement:
    - Effect: Allow
      Action:
        - quicksight:GetDashboardEmbedUrl
      Resource:
        [
          !Sub "arn:aws:quicksight:${AWS::Region}:${AWS::AccountId}:dashboard/${self:custom.a}",
          !Sub "arn:aws:quicksight:${AWS::Region}:${AWS::AccountId}:dashboard/${self:custom.a}"
        ]

The serverless framework allows you to use custom variables very flexibly, which makes this area quite convenient.

See Also