-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
integ.destinations.ts
108 lines (93 loc) · 4.01 KB
/
integ.destinations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import { App, Duration, Stack, StackProps } from 'aws-cdk-lib';
import { IntegTest, InvocationType, ExpectedResult } from '@aws-cdk/integ-tests-alpha';
import { Construct } from 'constructs';
import * as destinations from 'aws-cdk-lib/aws-lambda-destinations';
import { STANDARD_NODEJS_RUNTIME } from '../../config';
/*
* Stack verification steps:
* * aws lambda invoke --function-name <deployed fn name> --invocation-type Event --payload '"OK"' response.json
* * aws lambda invoke --function-name <deployed fn name> --invocation-type Event --payload '"NOT OK"' response.json
*/
class TestStack extends Stack {
public readonly fn: lambda.Function;
public readonly queue: sqs.Queue;
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const topic = new sns.Topic(this, 'Topic');
this.queue = new sqs.Queue(this, 'Queue');
this.fn = new lambda.Function(this, 'SnsSqs', {
runtime: STANDARD_NODEJS_RUNTIME,
handler: 'index.handler',
code: lambda.Code.fromInline(`exports.handler = async (event) => {
if (event.status === 'OK') return 'success';
throw new Error('failure');
};`),
onFailure: new destinations.SnsDestination(topic),
onSuccess: new destinations.SqsDestination(this.queue),
maxEventAge: Duration.hours(3),
retryAttempts: 1,
});
const onSuccessLambda = new lambda.Function(this, 'OnSucces', {
runtime: STANDARD_NODEJS_RUNTIME,
handler: 'index.handler',
code: lambda.Code.fromInline(`exports.handler = async (event) => {
console.log(event);
};`),
});
new lambda.Function(this, 'EventBusLambda', {
runtime: STANDARD_NODEJS_RUNTIME,
handler: 'index.handler',
code: lambda.Code.fromInline(`exports.handler = async (event) => {
if (event.status === 'OK') return 'success';
throw new Error('failure');
};`),
onFailure: new destinations.EventBridgeDestination(),
onSuccess: new destinations.LambdaDestination(onSuccessLambda),
});
const successBucket = new s3.Bucket(this, 'OnSuccessBucket');
const failureBucket = new s3.Bucket(this, 'OnFailureBucket');
new lambda.Function(this, 'S3', {
runtime: STANDARD_NODEJS_RUNTIME,
handler: 'index.handler',
code: lambda.Code.fromInline(`exports.handler = async (event) => {
if (event.status === 'OK') return 'success';
throw new Error('failure');
};`),
onFailure: new destinations.S3Destination(failureBucket),
onSuccess: new destinations.S3Destination(successBucket),
maxEventAge: Duration.hours(4),
retryAttempts: 2,
});
const version = this.fn.addVersion('MySpecialVersion');
new lambda.Alias(this, 'MySpecialAlias', {
aliasName: 'MySpecialAlias',
version,
onSuccess: new destinations.SqsDestination(this.queue),
onFailure: new destinations.SnsDestination(topic),
maxEventAge: Duration.hours(2),
retryAttempts: 0,
});
}
}
const app = new App();
const stack = new TestStack(app, 'aws-cdk-lambda-destinations');
const integ = new IntegTest(app, 'Destinations', {
testCases: [stack],
});
integ.assertions.invokeFunction({
functionName: stack.fn.functionName,
invocationType: InvocationType.EVENT,
payload: JSON.stringify({ status: 'OK' }),
});
const message = integ.assertions.awsApiCall('SQS', 'receiveMessage', {
QueueUrl: stack.queue.queueUrl,
WaitTimeSeconds: 20,
});
message.assertAtPath('Messages.0.Body.requestContext.condition', ExpectedResult.stringLikeRegexp('Success'));
message.assertAtPath('Messages.0.Body.requestPayload.status', ExpectedResult.stringLikeRegexp('OK'));
message.assertAtPath('Messages.0.Body.responseContext.statusCode', ExpectedResult.stringLikeRegexp('200'));
message.assertAtPath('Messages.0.Body.responsePayload', ExpectedResult.stringLikeRegexp('success'));