-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlayer.ts
264 lines (224 loc) · 9.42 KB
/
layer.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { FunctionProperties, Resources, Parameters, LambdaLayersProperty, CFN_IF_FUNCTION_STRING } from "./index";
import log from "loglevel";
const LAMBDA_FUNCTION_RESOURCE_TYPE = "AWS::Lambda::Function";
export const DD_ACCOUNT_ID = "464622532012";
export const DD_GOV_ACCOUNT_ID = "002406178527";
export enum RuntimeType {
NODE,
PYTHON,
UNSUPPORTED,
}
export enum ArchitectureType {
ARM64,
x86_64,
}
// Self defined interface that only applies to the macro - the FunctionProperties interface
// defined in index.ts matches the CloudFormation AWS::Lambda::Function Properties interface.
export interface LambdaFunction {
properties: FunctionProperties;
key: string;
runtimeType: RuntimeType;
runtime: string;
architectureType: ArchitectureType;
architecture: string;
}
const architectureLookup: { [key: string]: ArchitectureType } = {
x86_64: ArchitectureType.x86_64,
arm64: ArchitectureType.ARM64,
};
const architectureToExtensionLayerName: { [key: string]: string } = {
x86_64: "Datadog-Extension",
arm64: "Datadog-Extension-ARM",
};
export const runtimeLookup: { [key: string]: RuntimeType } = {
"nodejs12.x": RuntimeType.NODE,
"nodejs14.x": RuntimeType.NODE,
"nodejs16.x": RuntimeType.NODE,
"nodejs18.x": RuntimeType.NODE,
"python2.7": RuntimeType.PYTHON,
"python3.6": RuntimeType.PYTHON,
"python3.7": RuntimeType.PYTHON,
"python3.8": RuntimeType.PYTHON,
"python3.9": RuntimeType.PYTHON,
"python3.10": RuntimeType.PYTHON,
"python3.11": RuntimeType.PYTHON,
};
function runtimeToLayerName(runtime: string, architecture: string): string {
const nodeLookup: { [key: string]: string } = {
"nodejs12.x": "Datadog-Node12-x",
"nodejs14.x": "Datadog-Node14-x",
"nodejs16.x": "Datadog-Node16-x",
"nodejs18.x": "Datadog-Node18-x",
};
const pythonLookup: { [key: string]: string } = {
"python2.7": "Datadog-Python27",
"python3.6": "Datadog-Python36",
"python3.7": "Datadog-Python37",
"python3.8": "Datadog-Python38",
"python3.9": "Datadog-Python39",
"python3.10": "Datadog-Python310",
"python3.11": "Datadog-Python311",
};
const pythonArmLookup: { [key: string]: string } = {
"python3.8": "Datadog-Python38-ARM",
"python3.9": "Datadog-Python39-ARM",
"python3.10": "Datadog-Python310-ARM",
"python3.11": "Datadog-Python311-ARM",
};
if (runtimeLookup[runtime] === RuntimeType.NODE) {
return nodeLookup[runtime];
}
if (runtimeLookup[runtime] === RuntimeType.PYTHON && architectureLookup[architecture] === ArchitectureType.ARM64) {
return pythonArmLookup[runtime];
}
return pythonLookup[runtime];
}
/**
* Parse through the Resources section of the provided CloudFormation template to find all lambda
* function resources. Several modifications will be made later on to these resources, and
* storing them with a clearly defined interface will make it easier to implement changes.
*
* Also assigns a general runtime type to the output lambdas. This helps to determine which lambda
* layers to add & which handler to redirect to later on in the macro.
*/
export function findLambdas(resources: Resources, templateParameterValues: Parameters) {
return Object.entries(resources)
.map(([key, resource]) => {
if (resource.Type !== LAMBDA_FUNCTION_RESOURCE_TYPE) {
log.debug(`Resource ${key} is not a Lambda function, skipping...`);
return;
}
const properties: FunctionProperties = resource.Properties;
const runtime = useOrRef(properties.Runtime, templateParameterValues);
const architecture = useOrRef(properties.Architectures?.[0], templateParameterValues) ?? "x86_64";
let runtimeType = RuntimeType.UNSUPPORTED;
let architectureType = ArchitectureType.x86_64;
if (runtime !== undefined && runtime in runtimeLookup) {
runtimeType = runtimeLookup[runtime];
}
if (architecture !== undefined && architecture in architectureLookup) {
architectureType = architectureLookup[architecture];
}
return {
properties,
key,
runtimeType,
runtime,
architecture,
architectureType,
} as LambdaFunction;
})
.filter((lambda) => lambda !== undefined) as LambdaFunction[];
}
function useOrRef(value: undefined | string | { Ref: any }, templateParameterValues: Parameters): undefined | string {
if (!value) return undefined;
if (typeof value === "string") return value;
return templateParameterValues[value.Ref] ?? value;
}
/**
* Apply the provided Lambda layer that corresponds to each Lambda's runtime.
* If a Lambda layer for a given runtime is required but not provided, store an error message with
* that Lambda function's logical id. Return all errors, so that customer can see if they are
* missing more than one required Lambda layer.
*/
export function applyLayers(
region: string,
lambdas: LambdaFunction[],
pythonLayerVersion?: number,
nodeLayerVersion?: number,
extensionLayerVersion?: number,
) {
const errors: string[] = [];
lambdas.forEach((lambda) => {
if (lambda.runtimeType === RuntimeType.UNSUPPORTED) {
log.debug(`No Lambda layer available for runtime: ${lambda.runtime}`);
return;
}
let lambdaLibraryLayerArn;
let lambdaExtensionLayerArn;
if (lambda.runtimeType === RuntimeType.PYTHON) {
if (pythonLayerVersion === undefined) {
errors.push(getMissingLayerVersionErrorMsg(lambda.key, "Python", "python"));
return;
}
log.debug(`Setting Python Lambda layer for ${lambda.key}`);
lambdaLibraryLayerArn = getLambdaLibraryLayerArn(region, pythonLayerVersion, lambda.runtime, lambda.architecture);
addLayer(lambdaLibraryLayerArn, lambda);
}
if (lambda.runtimeType === RuntimeType.NODE) {
if (nodeLayerVersion === undefined) {
errors.push(getMissingLayerVersionErrorMsg(lambda.key, "Node.js", "node"));
return;
}
log.debug(`Setting Node Lambda layer for ${lambda.key}`);
lambdaLibraryLayerArn = getLambdaLibraryLayerArn(region, nodeLayerVersion, lambda.runtime, lambda.architecture);
addLayer(lambdaLibraryLayerArn, lambda);
}
if (extensionLayerVersion !== undefined) {
log.debug(`Setting Lambda Extension layer for ${lambda.key}`);
lambdaExtensionLayerArn = getExtensionLayerArn(region, extensionLayerVersion, lambda.architecture);
addLayer(lambdaExtensionLayerArn, lambda);
}
});
return errors;
}
function addLayer(layerArn: string, lambda: LambdaFunction) {
if (layerArn === undefined) {
return;
}
const currentLayers = lambda.properties.Layers ?? [];
const newLayers = getNewLayers(layerArn, currentLayers);
lambda.properties.Layers = newLayers;
}
// Return the layers arr or object with layerArn added
export function getNewLayers(layerArn: string, currentLayers: LambdaLayersProperty): LambdaLayersProperty {
if (Array.isArray(currentLayers)) {
if (currentLayers.includes(layerArn)) {
// Don't change layers if the layerArn is already present
return currentLayers;
}
return [...currentLayers, layerArn];
}
// CFN Fn::If conditional values are arrays with three items:
// 1. condition, 2. output if condition is true, 3. output if false
const conditionalValues = currentLayers[CFN_IF_FUNCTION_STRING];
// If this is not an if statement, log a warning and do not add layer
if (conditionalValues === undefined) {
console.warn("Unrecognized object in Layers definition. Cannot " + `add layer ${layerArn}`);
return currentLayers;
}
if (conditionalValues.length !== 3) {
console.warn("Conditional in Layers definition does not have 3 items. Cannot " + `add layer ${layerArn}`);
return currentLayers;
}
const [conditionalName, layersIfTrue, layersIfFalse] = conditionalValues;
const newLayersIfTrue = getNewLayers(layerArn, layersIfTrue);
const newLayersIfFalse = getNewLayers(layerArn, layersIfFalse);
return { [CFN_IF_FUNCTION_STRING]: [conditionalName, newLayersIfTrue, newLayersIfFalse] };
}
export function getLambdaLibraryLayerArn(region: string, version: number, runtime: string, architecture: string) {
const layerName = runtimeToLayerName(runtime, architecture);
const isGovCloud = region === "us-gov-east-1" || region === "us-gov-west-1";
// if this is a GovCloud region, use the GovCloud lambda layer
if (isGovCloud) {
log.debug("GovCloud region detected, using GovCloud Lambda layer");
return `arn:aws-us-gov:lambda:${region}:${DD_GOV_ACCOUNT_ID}:layer:${layerName}:${version}`;
}
return `arn:aws:lambda:${region}:${DD_ACCOUNT_ID}:layer:${layerName}:${version}`;
}
export function getExtensionLayerArn(region: string, version: number, architecture: string) {
const layerName = architectureToExtensionLayerName[architecture];
const isGovCloud = region === "us-gov-east-1" || region === "us-gov-west-1";
// if this is a GovCloud region, use the GovCloud lambda layer
if (isGovCloud) {
log.debug("GovCloud region detected, using GovCloud Lambda layer");
return `arn:aws-us-gov:lambda:${region}:${DD_GOV_ACCOUNT_ID}:layer:${layerName}:${version}`;
}
return `arn:aws:lambda:${region}:${DD_ACCOUNT_ID}:layer:${layerName}:${version}`;
}
export function getMissingLayerVersionErrorMsg(functionKey: string, formalRuntime: string, paramRuntime: string) {
return (
`Resource ${functionKey} has a ${formalRuntime} runtime, but no ${formalRuntime} Lambda Library version was provided. ` +
`Please add the '${paramRuntime}LayerVersion' parameter for the Datadog serverless macro.`
);
}