-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (140 loc) · 5.72 KB
/
index.js
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
const S3rver = require('s3rver');
const path = require('path');
let invokeLambda = require('@architect/sandbox/invokeLambda');
const createLambdaJSON = require('@architect/package/createLambdaJSON');
const { toLogicalID } = require('@architect/utils');
const fs = require('fs-extra');
const camelcase = require('camelcase');
const createClients = require('./client');
const defaultLocalOptions = {
port: 4569,
address: 'localhost',
directory: './buckets',
accessKeyId: 'S3RVER',
secretAccessKey: 'S3RVER',
resetOnClose: true
};
let s3Instance;
function parseBuckets (arc, stage) {
const [ stackName ] = arc.app;
return arc.buckets.map((bucket) => {
if (typeof bucket === 'string') {
return {
resource: `PluginBucket${toLogicalID(bucket)}`,
name: `${stackName}-${bucket}-${stage}`,
client: camelcase(bucket)
};
}
const [ [ name, settings ] ] = Object.entries(bucket);
return {
resource: `PluginBucket${toLogicalID(name)}`,
name: `${stackName}-${name}-${stage}`,
lambda: settings.triggers && `${name}-${settings.triggers}`,
client: camelcase(name),
settings
};
});
}
module.exports = {
package ({ arc, cloudformation, stage = 'staging', inventory }) {
const cwd = inventory.inv._project.src;
if (!arc.buckets) {
return cloudformation;
}
const buckets = parseBuckets(arc, stage);
for (let bucket of buckets) {
cloudformation.Resources[bucket.resource] = {
Type: 'AWS::S3::Bucket',
Properties: {
BucketName: bucket.name
}
};
}
cloudformation.Resources.Role.Properties.Policies.push({
PolicyName: 'ArcBucketsPlugin',
PolicyDocument: {
Statement: [
{
Effect: 'Allow',
Action: [
's3:GetObject',
's3:PutObject'
],
Resource: buckets.reduce((resources, bucket) => [ ...resources, `arn:aws:s3:::${bucket.name}`, `arn:aws:s3:::${bucket.name}/*` ], [])
}
]
}
});
for (let bucket of buckets.filter(b => b.settings && b.settings.triggers)) {
/** Add Lambda to be invoked */
const src = path.join(cwd, 'src', 'buckets', bucket.lambda);
const [ functionName, functionDef ] = createLambdaJSON({ inventory, src });
cloudformation.Resources[functionName] = functionDef;
/** Add Lambda permission so s3 can invoke it */
cloudformation.Resources[`${functionName}InvokePermission`] = {
Type: 'AWS::Lambda::Permission',
DependsOn: functionName,
Properties: {
FunctionName: { 'Fn::GetAtt': [ functionName, 'Arn' ] },
Action: 'lambda:InvokeFunction',
Principal: 's3.amazonaws.com',
SourceAccount: { Ref: 'AWS::AccountId' },
SourceArn: `arn:aws:s3:::${bucket.name}`
}
};
/** Add s3 Notification Configuration so it knows when to invoke the lambda */
cloudformation.Resources[bucket.resource].Properties.NotificationConfiguration = {
LambdaConfigurations: [ {
Event: bucket.settings.triggers === 'create' ? 's3:ObjectCreated:*' : 's3:ObjectRemoved:*',
Function: { 'Fn::GetAtt': [ functionName, 'Arn' ] }
} ]
};
}
return cloudformation;
},
pluginFunctions ({ arc, inventory }) {
if (arc.buckets) {
const buckets = parseBuckets(arc, "' + process.env.NODE_ENV");
const cwd = inventory.inv._project.src;
const { code, types } = createClients(buckets);
fs.outputFileSync(path.join(cwd, './src/shared/buckets.js'), code);
fs.outputFileSync(path.join(cwd, './src/shared/buckets.d.ts'), types);
for (let bucket of buckets.filter(b => b.settings && b.settings.triggers)) {
const src = path.join(cwd, 'src', 'buckets', bucket.lambda);
return [ {
src,
body: 'exports.handler = async function (event) { console.log(event); }'
} ];
}
}
return [];
},
sandbox: {
async start ({ arc, inventory }) {
const cwd = inventory.inv._project.src;
if (arc.buckets) {
const buckets = parseBuckets(arc, 'testing');
s3Instance = new S3rver({ configureBuckets: buckets, ...defaultLocalOptions });
await s3Instance.run();
s3Instance.on('event', (e) => {
const { eventName, s3: { bucket: { name } } } = e.Records[0];
const bucket = buckets.find(bucket => bucket.name === name);
if (bucket.settings && bucket.settings.triggers && eventName.toLowerCase().includes(bucket.settings.triggers)) {
const src = path.join(cwd, 'src', 'buckets', bucket.lambda);
invokeLambda({ inventory, src, payload: e }, (e) => {
if (e) console.log(e);
});
}
});
}
},
end: async function endS3rver () {
try {
return await s3Instance.close();
}
catch (e) {
console.log(e);
}
}
}
};