-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
64 lines (54 loc) · 1.87 KB
/
index.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
/*!
* Copyright © 2023 United States Government as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
import { join } from 'path'
import { exists } from './paths.js'
export const deploy = {
async start({
cloudformation,
inventory: {
inv: { lambdasBySrcDir },
},
}: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
cloudformation: { Resources: Record<string, any> }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inventory: { inv: { lambdasBySrcDir: Record<string, any> } }
}) {
let hasTracing
const cfnBySrcDir = Object.fromEntries(
Object.values(cloudformation.Resources)
.filter(({ Type }) => Type === 'AWS::Serverless::Function')
.map(({ Properties }) => [Properties.CodeUri, Properties])
)
await Promise.all(
Object.entries(lambdasBySrcDir).map(async ([path, { config }]) => {
if (!config.tracing) return
hasTracing = true
const props = cfnBySrcDir[path]
const layers = props.Layers ?? (props.Layers = [])
props.Tracing = 'Active'
layers.push({
'Fn::Sub': `arn:aws:lambda:\${AWS::Region}:901920570463:layer:aws-otel-nodejs-${config.architecture}-ver-1-18-1:4`,
})
props.Environment.Variables.AWS_LAMBDA_EXEC_WRAPPER =
'/opt/otel-handler'
if (await exists(join(path, 'tracing.js'))) {
props.Environment.Variables.NODE_OPTIONS =
'--require /var/task/tracing.js'
}
})
)
if (hasTracing) {
const roleProps = cloudformation.Resources.Role.Properties
;(roleProps.ManagedPolicyArns ?? (roleProps.ManagedPolicyArns = [])).push(
'arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess'
)
}
return cloudformation
},
}