Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for DLQ configuration to Lambda L2 resource #663

Merged
merged 1 commit into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
SeekerWing marked this conversation as resolved.
Show resolved Hide resolved
"@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