|
| 1 | +import { CustomResource, Duration, Stack, StackProps } from 'aws-cdk-lib'; |
| 2 | +import { Construct } from 'constructs'; |
| 3 | +import { LayerVersion, Runtime } from 'aws-cdk-lib/aws-lambda'; |
| 4 | +import { RetentionDays } from 'aws-cdk-lib/aws-logs'; |
| 5 | +import { v4 } from 'uuid'; |
| 6 | +import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam'; |
| 7 | +import { Provider } from 'aws-cdk-lib/custom-resources'; |
| 8 | +import { StringParameter } from 'aws-cdk-lib/aws-ssm'; |
| 9 | +import path from 'path'; |
| 10 | +import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'; |
| 11 | + |
| 12 | +export interface CanaryStackProps extends StackProps { |
| 13 | + readonly layerName: string; |
| 14 | + readonly powertoolsPackageVersion: string; |
| 15 | + readonly ssmParameterLayerArn: string; |
| 16 | +} |
| 17 | + |
| 18 | +export class CanaryStack extends Stack { |
| 19 | + public constructor(scope: Construct, id: string, props: CanaryStackProps) { |
| 20 | + super(scope, id, props); |
| 21 | + const { layerName, powertoolsPackageVersion } = props; |
| 22 | + |
| 23 | + const suffix = v4().substring(0, 5); |
| 24 | + |
| 25 | + const layerArn = StringParameter.fromStringParameterAttributes( |
| 26 | + this, |
| 27 | + 'LayerArn', |
| 28 | + { |
| 29 | + parameterName: props.ssmParameterLayerArn, |
| 30 | + } |
| 31 | + ).stringValue; |
| 32 | + |
| 33 | + // lambda function |
| 34 | + const layer = [ |
| 35 | + LayerVersion.fromLayerVersionArn(this, 'powertools-layer', layerArn), |
| 36 | + ]; |
| 37 | + |
| 38 | + const canaryFunction = new NodejsFunction(this, 'CanaryFunction', { |
| 39 | + entry: path.join( |
| 40 | + __dirname, |
| 41 | + '../tests/e2e/layerPublisher.class.test.functionCode.ts' |
| 42 | + ), |
| 43 | + handler: 'handler', |
| 44 | + runtime: Runtime.NODEJS_18_X, |
| 45 | + functionName: `canary-${suffix}`, |
| 46 | + timeout: Duration.seconds(30), |
| 47 | + bundling: { |
| 48 | + externalModules: [ |
| 49 | + // don't package these modules, we want to pull them from the layer |
| 50 | + 'aws-sdk', |
| 51 | + '@aws-lambda-powertools/logger', |
| 52 | + '@aws-lambda-powertools/metrics', |
| 53 | + '@aws-lambda-powertools/tracer', |
| 54 | + '@aws-lambda-powertools/parameters', |
| 55 | + '@aws-lambda-powertools/commons', |
| 56 | + ], |
| 57 | + }, |
| 58 | + environment: { |
| 59 | + POWERTOOLS_SERVICE_NAME: 'canary', |
| 60 | + POWERTOOLS_PACKAGE_VERSION: powertoolsPackageVersion, |
| 61 | + POWERTOOLS_LAYER_NAME: layerName, |
| 62 | + SSM_PARAMETER_LAYER_ARN: props.ssmParameterLayerArn, |
| 63 | + }, |
| 64 | + layers: layer, |
| 65 | + logRetention: RetentionDays.ONE_DAY, |
| 66 | + }); |
| 67 | + |
| 68 | + canaryFunction.addToRolePolicy( |
| 69 | + new PolicyStatement({ |
| 70 | + actions: ['ssm:GetParameter'], |
| 71 | + resources: ['*'], |
| 72 | + effect: Effect.ALLOW, |
| 73 | + }) |
| 74 | + ); |
| 75 | + |
| 76 | + // use custom resource to trigger the lambda function during the CFN deployment |
| 77 | + const provider = new Provider(this, 'CanaryCustomResourceProvider', { |
| 78 | + onEventHandler: canaryFunction, |
| 79 | + logRetention: RetentionDays.ONE_DAY, |
| 80 | + }); |
| 81 | + |
| 82 | + // random suffix forces recreation of the custom resource otherwise the custom resource will be reused from prevous deployment |
| 83 | + new CustomResource(this, `CanaryCustomResource${suffix}`, { |
| 84 | + serviceToken: provider.serviceToken, |
| 85 | + }); |
| 86 | + } |
| 87 | +} |
0 commit comments