-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathcanary-stack.ts
87 lines (78 loc) · 2.89 KB
/
canary-stack.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
import { CustomResource, Duration, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { LayerVersion, Runtime } from 'aws-cdk-lib/aws-lambda';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { v4 } from 'uuid';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Provider } from 'aws-cdk-lib/custom-resources';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
import path from 'path';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
export interface CanaryStackProps extends StackProps {
readonly layerName: string;
readonly powertoolsPackageVersion: string;
readonly ssmParameterLayerArn: string;
}
export class CanaryStack extends Stack {
public constructor(scope: Construct, id: string, props: CanaryStackProps) {
super(scope, id, props);
const { layerName, powertoolsPackageVersion } = props;
const suffix = v4().substring(0, 5);
const layerArn = StringParameter.fromStringParameterAttributes(
this,
'LayerArn',
{
parameterName: props.ssmParameterLayerArn,
}
).stringValue;
// lambda function
const layer = [
LayerVersion.fromLayerVersionArn(this, 'powertools-layer', layerArn),
];
const canaryFunction = new NodejsFunction(this, 'CanaryFunction', {
entry: path.join(
__dirname,
'../tests/e2e/layerPublisher.class.test.functionCode.ts'
),
handler: 'handler',
runtime: Runtime.NODEJS_18_X,
functionName: `canary-${suffix}`,
timeout: Duration.seconds(30),
bundling: {
externalModules: [
// don't package these modules, we want to pull them from the layer
'aws-sdk',
'@aws-lambda-powertools/logger',
'@aws-lambda-powertools/metrics',
'@aws-lambda-powertools/tracer',
'@aws-lambda-powertools/parameters',
'@aws-lambda-powertools/commons',
],
},
environment: {
POWERTOOLS_SERVICE_NAME: 'canary',
POWERTOOLS_PACKAGE_VERSION: powertoolsPackageVersion,
POWERTOOLS_LAYER_NAME: layerName,
SSM_PARAMETER_LAYER_ARN: props.ssmParameterLayerArn,
},
layers: layer,
logRetention: RetentionDays.ONE_DAY,
});
canaryFunction.addToRolePolicy(
new PolicyStatement({
actions: ['ssm:GetParameter'],
resources: ['*'],
effect: Effect.ALLOW,
})
);
// use custom resource to trigger the lambda function during the CFN deployment
const provider = new Provider(this, 'CanaryCustomResourceProvider', {
onEventHandler: canaryFunction,
logRetention: RetentionDays.ONE_DAY,
});
// random suffix forces recreation of the custom resource otherwise the custom resource will be reused from prevous deployment
new CustomResource(this, `CanaryCustomResource${suffix}`, {
serviceToken: provider.serviceToken,
});
}
}