-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambdas.mjs
123 lines (117 loc) · 3.65 KB
/
lambdas.mjs
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
import { join } from 'node:path'
import { build } from 'esbuild'
const cwd = process.cwd()
const entryFileFolder = join(cwd, 'src', 'entry-files')
const n = (stage, name) => `${name}${stage === 'production' ? '' : '-staging'}`
const names = [
'control',
'aws-lite-raw',
'aws-lite-bundled',
'aws-sdk-v2-raw', // Run on nodejs16.x
'aws-sdk-v2-bundled', // Run on nodejs20.x
'aws-sdk-v3-raw',
'aws-sdk-v3-bundled',
]
const services = [
'dynamodb',
's3',
'iam',
'cloudformation',
'lambda',
'sts',
]
const lambdae = names.concat('invoker')
const plugin = {
set: {
customLambdas: () => {
const executing = lambdae.map(name => ({ name, src: `src/lambdas/${name}` }))
// Just point dummies to control src because it's super simple
const dummies = names
.filter(name => name !== 'control')
.map(name => ({ name: `dummy-${name}`, src: `src/lambdas/control` }))
return executing.concat(dummies)
},
},
deploy: {
start: async ({ cloudformation, inventory }) => {
const resources = Object.entries(cloudformation.Resources)
for (const [ name, item ] of resources) {
if (item.Type === 'AWS::Serverless::Function') {
const stage = inventory.inv._arc.deployStage
cloudformation.Resources[name].Properties.FunctionName = n(stage, item.ArcMetadata.name)
cloudformation.Resources[name]
.Properties.Environment.Variables.DUMMY_TABLE_NAME = {
Ref: 'DummyDataTable',
}
cloudformation.Resources[name]
.Properties.Environment.Variables.DUMMY_BUCKET_NAME = {
Ref: 'DummyAssetsBucket',
}
if (name.startsWith('Aws')) {
cloudformation.Resources[name]
.Properties.Environment.Variables.DUMMY_LAMBDA_NAME = {
Ref: `Dummy${name}`,
}
}
}
}
// Enable access to dummy resources
cloudformation.Resources.Role.Properties.Policies.push({
PolicyName: 'DummyResourcePolicy',
PolicyDocument: {
Statement: [
{
Effect: 'Allow',
Action: [
'cloudformation:ListStackResources',
'cloudformation:UpdateTerminationProtection',
'iam:GetRole',
'iam:UpdateRole',
'lambda:GetFunctionConfiguration',
'lambda:UpdateFunctionConfiguration',
],
Resource: [
'arn:aws:cloudformation:*:*:*',
'arn:aws:lambda:*:*:*:*',
'arn:aws:iam::*:role/aws-lite-dummy-iam-role',
],
},
],
},
})
},
},
hydrate: {
copy: async ({ inventory }) => {
for (const lambda of inventory.inv.customLambdas) {
const { name } = lambda
if (name.includes('bundled') && !name.includes('dummy')) {
const version = name.split('-bundled')[0]
const outDir = join(cwd, 'src', 'lambdas', name)
if (version === 'aws-lite') {
await esbuild(
[ join(entryFileFolder, `${version}-client.js`) ],
join(outDir, `${version}-client-bundle.js`),
)
}
for (const service of services) {
await esbuild(
[ join(entryFileFolder, `${version}-${service}.js`) ],
join(outDir, `${version}-${service}-bundle.js`),
)
}
}
}
},
},
}
async function esbuild (entryPoints, outfile) {
await build({
entryPoints,
bundle: true,
platform: 'node',
format: 'cjs',
outfile,
})
}
export { plugin as default, names }