aws / aws/aws-cdk

aws-lambda: Defining a `deadLetterQueue` or `deadLetterTopic` will *always* add a policy to the function's execution role

Open
#29,879 5 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-lambda bug effort/small p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

As indicated in the title, if you define a `deadLetterQueue` or `deadLetterTopic` on a `Function`, it will **always** append an inline policy to the execution role which is associated with the function.

I would suggest that this is expected behaviour if the `Function` creates the IAM role, but it may be unexpected if the IAM role is defined elsewhere. It feels a lot like a side-effect. This feels like a problem within the same CloudFormation stack, but would probably be even more unexpected if the IAM role being referenced originated outside the stack.

### Expected Behavior

I created an IAM role and SQS queue adjacent to a Lambda Function. I would have expected that if my IAM role were lacking a permission, it would:

* Ideally, throw an error due to a missing permission (although this may be hard to implement, especially for non-stack IAM roles); or
* Simply fail to drop failing events to the DLQ due to a lacking permission (this is less ideal because it may go unnoticed)

### Current Behavior

IAM policies for SQS permission will indiscriminately be added to IAM roles no matter where that role originates from

### Reproduction Steps

#### Relevant CDK Source Code

https://github.com/aws/aws-cdk/blob/5347369fa11f4f11ab3893b9ac4c8467c5d514c3/packages/aws-cdk-lib/aws-lambda/lib/function.ts#L1596-L1599

https://github.com/aws/aws-cdk/blob/5347369fa11f4f11ab3893b9ac4c8467c5d514c3/packages/aws-cdk-lib/aws-lambda/lib/function-base.ts#L375-L381

#### Partial Code to Reproduce Behaviour

```python
queue = aws_sqs.Queue(self, "queue")
role = aws_iam.Role(
self,
"role",
managed_policies=[
aws_iam.ManagedPolicy.from_aws_managed_policy_name(
"service-role/AWSLambdaVPCAccessExecutionRole"
),
],
assumed_by=aws_iam.ServicePrincipal("lambda.amazonaws.com"),
)
function = aws_lambda.Function(
...
role=role,
dead_letter_queue=queue,
...
)
```

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.114.1

### Framework Version

2.114.1

### Node.js Version

20.8.1

### OS

macOS 14.2.1

### Language

Python

### Language Version

3.11.6

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reading the referenced sections of packages/aws-cdk-lib/aws-lambda/lib/function.ts and function-base.ts, then synthesize the Python reproduction with an externally defined IAM role, SQS queue, and Lambda dead-letter queue. Done means the behavior for supplied roles and function-created roles is explicitly resolved without indiscriminately adding SQS permissions.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python, typescript
Domain
cloud, infrastructure, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.