agiledigital / serverless-sns-sqs-lambda

serverless plugin to make serverless-sns-sqs-lambda events
Apache License 2.0
81 stars 18 forks source link

Reduce the generated IAM role size by not creating separate policy statements for SQS and KMS resources #832

Open peter-kasa opened 7 months ago

peter-kasa commented 7 months ago

We ran into the issue of having a too large IAM role generated when using multiple snsSqs events in one serverless.yml. (An other PR also related to this issue: https://github.com/agiledigital/serverless-sns-sqs-lambda/pull/715)

I noticed that the generated IAM role's size can be reduced by merging multiple policy statements into one. The scope of this PR is to do this resource merge. See the before/after example on what to expect.

Example

Before

{
    "Version": "2012-10-17",
    "Statement": [
        ...
        {
            "Action": [
                "sqs:DeleteMessage",
                "sqs:GetQueueAttributes",
                "sqs:ReceiveMessage"
            ],
            "Resource": [
                "arn:aws:sqs:us-west-2:123456789000:test-dev-testQueue",
                "arn:aws:sqs:us-west-2:123456789000:test-dev-testDeadLetterQueue"
            ],
            "Effect": "Allow"
        },
        {
            "Action": [
                "sqs:DeleteMessage",
                "sqs:GetQueueAttributes",
                "sqs:ReceiveMessage"
            ],
            "Resource": [
                "arn:aws:sqs:us-west-2:123456789000:test-dev-test2Queue",
                "arn:aws:sqs:us-west-2:123456789000:test-dev-test2DeadLetterQueue"
            ],
            "Effect": "Allow"
        }
        ...
    ]
}

After

{
    "Version": "2012-10-17",
    "Statement": [
        ...
        {
            "Action": [
                "sqs:DeleteMessage",
                "sqs:GetQueueAttributes",
                "sqs:ReceiveMessage"
            ],
            "Resource": [
                "arn:aws:sqs:us-west-2:123456789000:test-dev-testQueue",
                "arn:aws:sqs:us-west-2:123456789000:test-dev-testDeadLetterQueue",
                "arn:aws:sqs:us-west-2:123456789000:test-dev-test2Queue",
                "arn:aws:sqs:us-west-2:123456789000:test-dev-test2DeadLetterQueue"
            ],
            "Effect": "Allow"
        }
        ...
    ]
}