-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lambda-event-sources): failure handling for stream event sources (…
- Loading branch information
Showing
13 changed files
with
921 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { DlqDestinationConfig, IEventSourceDlq, IEventSourceMapping, IFunction } from "@aws-cdk/aws-lambda"; | ||
import * as sns from '@aws-cdk/aws-sns'; | ||
|
||
/** | ||
* An SNS dead letter queue destination configuration for a Lambda event source | ||
*/ | ||
export class SnsDlq implements IEventSourceDlq { | ||
constructor(private readonly topic: sns.ITopic) { | ||
} | ||
|
||
/** | ||
* Returns a destination configuration for the DLQ | ||
*/ | ||
public bind(_target: IEventSourceMapping, targetHandler: IFunction): DlqDestinationConfig { | ||
this.topic.grantPublish(targetHandler); | ||
|
||
return { | ||
destination: this.topic.topicArn | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { DlqDestinationConfig, IEventSourceDlq, IEventSourceMapping, IFunction } from "@aws-cdk/aws-lambda"; | ||
import * as sqs from '@aws-cdk/aws-sqs'; | ||
|
||
/** | ||
* An SQS dead letter queue destination configuration for a Lambda event source | ||
*/ | ||
export class SqsDlq implements IEventSourceDlq { | ||
constructor(private readonly queue: sqs.IQueue) { | ||
} | ||
|
||
/** | ||
* Returns a destination configuration for the DLQ | ||
*/ | ||
public bind(_target: IEventSourceMapping, targetHandler: IFunction): DlqDestinationConfig { | ||
this.queue.grantSendMessages(targetHandler); | ||
|
||
return { | ||
destination: this.queue.queueArn | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
packages/@aws-cdk/aws-lambda-event-sources/test/integ.kinesiswithdlq.expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
{ | ||
"Resources": { | ||
"FServiceRole3AC82EE1": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "lambda.amazonaws.com" | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"ManagedPolicyArns": [ | ||
{ | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
] | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
"FServiceRoleDefaultPolicy17A19BFA": { | ||
"Type": "AWS::IAM::Policy", | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"sqs:SendMessage", | ||
"sqs:GetQueueAttributes", | ||
"sqs:GetQueueUrl" | ||
], | ||
"Effect": "Allow", | ||
"Resource": { | ||
"Fn::GetAtt": [ | ||
"Q63C6E3AB", | ||
"Arn" | ||
] | ||
} | ||
}, | ||
{ | ||
"Action": [ | ||
"kinesis:DescribeStream", | ||
"kinesis:GetRecords", | ||
"kinesis:GetShardIterator" | ||
], | ||
"Effect": "Allow", | ||
"Resource": { | ||
"Fn::GetAtt": [ | ||
"S509448A1", | ||
"Arn" | ||
] | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "FServiceRoleDefaultPolicy17A19BFA", | ||
"Roles": [ | ||
{ | ||
"Ref": "FServiceRole3AC82EE1" | ||
} | ||
] | ||
} | ||
}, | ||
"FC4345940": { | ||
"Type": "AWS::Lambda::Function", | ||
"Properties": { | ||
"Code": { | ||
"ZipFile": "exports.handler = async function handler(event) {\n // tslint:disable-next-line:no-console\n console.log('event:', JSON.stringify(event, undefined, 2));\n throw new Error();\n}" | ||
}, | ||
"Handler": "index.handler", | ||
"Role": { | ||
"Fn::GetAtt": [ | ||
"FServiceRole3AC82EE1", | ||
"Arn" | ||
] | ||
}, | ||
"Runtime": "nodejs10.x" | ||
}, | ||
"DependsOn": [ | ||
"FServiceRoleDefaultPolicy17A19BFA", | ||
"FServiceRole3AC82EE1" | ||
] | ||
}, | ||
"FKinesisEventSourcelambdaeventsourcekinesiswithdlqSD357FCB87EEA8CB4": { | ||
"Type": "AWS::Lambda::EventSourceMapping", | ||
"Properties": { | ||
"EventSourceArn": { | ||
"Fn::GetAtt": [ | ||
"S509448A1", | ||
"Arn" | ||
] | ||
}, | ||
"FunctionName": { | ||
"Ref": "FC4345940" | ||
}, | ||
"BatchSize": 100, | ||
"DestinationConfig": { | ||
"OnFailure": { | ||
"Destination": { | ||
"Fn::GetAtt": [ | ||
"Q63C6E3AB", | ||
"Arn" | ||
] | ||
} | ||
} | ||
}, | ||
"MaximumRetryAttempts": 0, | ||
"StartingPosition": "TRIM_HORIZON" | ||
} | ||
}, | ||
"S509448A1": { | ||
"Type": "AWS::Kinesis::Stream", | ||
"Properties": { | ||
"ShardCount": 1, | ||
"RetentionPeriodHours": 24 | ||
} | ||
}, | ||
"Q63C6E3AB": { | ||
"Type": "AWS::SQS::Queue" | ||
} | ||
}, | ||
"Outputs": { | ||
"InputKinesisStreamName": { | ||
"Value": { | ||
"Ref": "S509448A1" | ||
} | ||
}, | ||
"DlqSqsQueueUrl": { | ||
"Value": { | ||
"Ref": "Q63C6E3AB" | ||
} | ||
}, | ||
"FunctionArn": { | ||
"Value":{ | ||
"Fn::GetAtt":["FC4345940","Arn"] | ||
} | ||
} | ||
}, | ||
"Parameters": { | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
packages/@aws-cdk/aws-lambda-event-sources/test/integ.kinesiswithdlq.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as kinesis from '@aws-cdk/aws-kinesis'; | ||
import * as lambda from '@aws-cdk/aws-lambda'; | ||
import * as sqs from '@aws-cdk/aws-sqs'; | ||
import { App, CfnOutput, Stack } from "@aws-cdk/core"; | ||
import { KinesisEventSource, SqsDlq } from '../lib'; | ||
|
||
/* | ||
* Stack verification steps: | ||
* * aws kinesis put-record --stream-name <value of stack output: InputKinesisStreamName> --partition-key 123 --data testdata | ||
* * aws sqs receive-message --queue-url <value of stack output: DlqSqsQueueUrl> --max-number-of-messages 1 --query 'Messages[0].Body' | ||
* The last command should return a string that contains the Lambda function ARN in it. | ||
*/ | ||
|
||
async function handler(event: any) { | ||
// tslint:disable-next-line:no-console | ||
console.log('event:', JSON.stringify(event, undefined, 2)); | ||
throw new Error(); | ||
} | ||
|
||
class KinesisWithDLQTest extends Stack { | ||
constructor(scope: App, id: string) { | ||
super(scope, id); | ||
|
||
const fn = new lambda.Function(this, 'F', { | ||
runtime: lambda.Runtime.NODEJS_10_X, | ||
handler: 'index.handler', | ||
code: lambda.Code.fromInline(`exports.handler = ${handler.toString()}`) | ||
}); | ||
new CfnOutput(this, 'FunctionArn', { value: fn.functionArn }); | ||
|
||
const stream = new kinesis.Stream(this, 'S'); | ||
new CfnOutput(this, 'InputKinesisStreamName', { value: stream.streamName }); | ||
|
||
const dlq = new sqs.Queue(this, 'Q'); | ||
new CfnOutput(this, 'DlqSqsQueueUrl', { value: dlq.queueUrl }); | ||
|
||
fn.addEventSource(new KinesisEventSource(stream, { | ||
startingPosition: lambda.StartingPosition.TRIM_HORIZON, | ||
onFailure: new SqsDlq(dlq), | ||
retryAttempts: 0, | ||
})); | ||
} | ||
} | ||
|
||
const app = new App(); | ||
new KinesisWithDLQTest(app, 'lambda-event-source-kinesis-with-dlq'); | ||
app.synth(); |
Oops, something went wrong.