Skip to content

Commit

Permalink
feat(aws-lambda): support dead letter queues (#663)
Browse files Browse the repository at this point in the history
Adds support for DLQ configuration in Lambda
  • Loading branch information
SeekerWing authored and Elad Ben-Israel committed Sep 6, 2018
1 parent 71e1226 commit 1b134a5
Show file tree
Hide file tree
Showing 5 changed files with 646 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- AWS::Glue::Classifier JsonClassifier (__added__)
- AWS::Glue::Classifier XMLClassifier (__added__)
- AWS::Glue::Crawler Configuration (__added__)
- AWS::Lambda::Lambda DLQConfigurationSupport (__added__)
- AWS::Neptune::DBInstance DBSubnetGroupName.UpdateType (__changed__)
- Old: Mutable
- New: Immutable
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,18 @@ new lambda.PipelineInvokeAction(this, 'Lambda', {

See [the AWS documentation](https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html)
on how to write a Lambda function invoked from CodePipeline.

### Lambda with DLQ

```ts
import lambda = require('@aws-cdk/aws-lambda');

const fn = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.NodeJS810,
handler: 'index.handler'
code: lambda.Code.inline('exports.handler = function(event, ctx, cb) { return cb(null, "hi"); }'),
deadLetterQueueEnabled: true
});
```
See [the AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/dlq.html)
to learn more about AWS Lambdas and DLQs.
41 changes: 41 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import iam = require('@aws-cdk/aws-iam');
import sqs = require('@aws-cdk/aws-sqs');
import cdk = require('@aws-cdk/cdk');
import { Code } from './code';
import { FunctionRef } from './lambda-ref';
Expand Down Expand Up @@ -89,6 +90,22 @@ export interface FunctionProps {
* Both supplied and generated roles can always be changed by calling `addToRolePolicy`.
*/
role?: iam.Role;

/**
* Enabled DLQ. If `deadLetterQueue` is undefined,
* an SQS queue with default options will be defined for your Function.
*
* @default false unless `deadLetterQueue` is set, which implies DLQ is enabled
*/
deadLetterQueueEnabled?: boolean;

/**
* The SQS queue to use if DLQ is enabled.
*
* @default SQS queue with 14 day retention period if `deadLetterQueueEnabled` is `true`
*/
deadLetterQueue?: sqs.QueueRef;

}

/**
Expand Down Expand Up @@ -166,6 +183,7 @@ export class Function extends FunctionRef {
role: this.role.roleArn,
environment: new cdk.Token(() => this.renderEnvironment()),
memorySize: props.memorySize,
deadLetterConfig: this.buildDeadLetterConfig(props),
});

resource.addDependency(this.role);
Expand Down Expand Up @@ -226,4 +244,27 @@ export class Function extends FunctionRef {
variables: this.environment
};
}

private buildDeadLetterConfig(props: FunctionProps) {
if (props.deadLetterQueue && props.deadLetterQueueEnabled === false) {
throw Error('deadLetterQueue defined but deadLetterQueueEnabled explicitly set to false');
}

if (!props.deadLetterQueue && !props.deadLetterQueueEnabled) {
return undefined;
}

const deadLetterQueue = props.deadLetterQueue || new sqs.Queue(this, 'DeadLetterQueue', {
retentionPeriodSec: 1209600
});

this.addToRolePolicy(new cdk.PolicyStatement()
.addAction('sqs:SendMessage')
.addResource(deadLetterQueue.queueArn));

return {
targetArn: deadLetterQueue.queueArn
};
}

}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@aws-cdk/aws-logs": "^0.8.2",
"@aws-cdk/aws-s3": "^0.8.2",
"@aws-cdk/aws-s3-notifications": "^0.8.2",
"@aws-cdk/aws-sqs": "^0.8.2",
"@aws-cdk/cdk": "^0.8.2",
"@aws-cdk/cx-api": "^0.8.2"
},
Expand Down
Loading

0 comments on commit 1b134a5

Please sign in to comment.