-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathlayer-publisher-stack.ts
66 lines (57 loc) · 2.24 KB
/
layer-publisher-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
import { CfnOutput, RemovalPolicy, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import {
LayerVersion,
Code,
Runtime,
CfnLayerVersionPermission,
} from 'aws-cdk-lib/aws-lambda';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
import { resolve } from 'node:path';
export interface LayerPublisherStackProps extends StackProps {
readonly layerName?: string;
readonly powertoolsPackageVersion?: string;
readonly ssmParameterLayerArn: string;
}
export class LayerPublisherStack extends Stack {
public readonly lambdaLayerVersion: LayerVersion;
public constructor(
scope: Construct,
id: string,
props: LayerPublisherStackProps
) {
super(scope, id, props);
const { layerName, powertoolsPackageVersion } = props;
console.log(
`publishing layer ${layerName} version : ${powertoolsPackageVersion}`
);
this.lambdaLayerVersion = new LayerVersion(this, 'LambdaPowertoolsLayer', {
layerVersionName: props?.layerName,
description: `Powertools for AWS Lambda (TypeScript) version ${powertoolsPackageVersion}`,
compatibleRuntimes: [Runtime.NODEJS_16_X, Runtime.NODEJS_18_X],
license: 'MIT-0',
// This is needed because the following regions do not support the compatibleArchitectures property #1400
// ...(![ 'eu-south-2', 'eu-central-2', 'ap-southeast-4' ].includes(Stack.of(this).region) ? { compatibleArchitectures: [Architecture.X86_64] } : {}),
code: Code.fromAsset(resolve(__dirname, '..', '..', 'tmp')),
});
const layerPermission = new CfnLayerVersionPermission(
this,
'PublicLayerAccess',
{
action: 'lambda:GetLayerVersion',
layerVersionArn: this.lambdaLayerVersion.layerVersionArn,
principal: '*',
}
);
layerPermission.applyRemovalPolicy(RemovalPolicy.RETAIN);
this.lambdaLayerVersion.applyRemovalPolicy(RemovalPolicy.RETAIN);
new StringParameter(this, 'VersionArn', {
parameterName: props.ssmParameterLayerArn,
stringValue: this.lambdaLayerVersion.layerVersionArn,
});
new CfnOutput(this, 'LatestLayerArn', {
value: this.lambdaLayerVersion.layerVersionArn,
exportName: props?.layerName ?? `LambdaPowerToolsForTypeScriptLayerARN`,
});
}
}