-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
120 lines (103 loc) · 2.94 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
console.log('Loading function');
const aws = require('aws-sdk');
const Ajv = require("ajv")
const fs = require("fs")
const YAML = require("yaml")
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
const cloudfront = new aws.CloudFront();
const schemaFiles = [
'./schemas/ObservedConfig.yaml',
'./schemas/PageConfig.yaml'
];
function buildValidator() {
const schemas = schemaFiles.map(file => YAML.parse(fs.readFileSync(file, 'utf-8')));
const ajv = new Ajv({schemas})
const schema = {
if: {
type: "object",
properties: {
type: {
const: "page"
}
},
required: [
"type"
]
},
then: {
$ref: "pageSchema"
},
else: {
$ref: "observedSchema"
}
}
return ajv.compile(schema);
}
function makeid(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for ( let i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
const {
DESTINATION_BUCKET_NAME,
DESTINATION_CONFIGS_ASSET_KEY,
CLOUDFRONT_DISTRIBUTION_ID,
CLOUDFRONT_INVALIDATION_PATH } = process.env;
exports.handler = async (event, context) => {
const bucket = event.Records[0].s3.bucket.name;
const params = {
Bucket: bucket,
};
const validate = buildValidator();
const { Contents } = await s3.listObjectsV2(params).promise();
const fileConfigs = await Promise.all(Contents.map(async (config, index) => {
const file = await s3.getObject({ Bucket: bucket, Key: config.Key }).promise();
let configFile;
try {
configFile = JSON.parse(file.Body.toString())
} catch (e) {
throw new Error('Config: ' + config.Key + ' is not a valid JSON');
}
if(!validate(configFile)) {
throw new Error('Config: ' + config.Key + ' is invalid\nSchema:' + JSON.stringify(validate.errors, null, 2))
}
return file.Body.toString();
}));
const allConfigs = fileConfigs.reduce((prev, next, index) => {
if (index !== fileConfigs.length - 1)
return prev.concat(next).concat(',\n');
return prev.concat(next);
}, '[\n').concat(']\n');
try {
JSON.parse(allConfigs)
} catch (e) {
throw new Error('Could not parse JSON');
}
console.log('All configs:', allConfigs);
await s3.putObject({
Bucket: DESTINATION_BUCKET_NAME,
Key: DESTINATION_CONFIGS_ASSET_KEY,
Body: allConfigs,
ContentType: 'text'
}).promise();
console.log('uploaded configs to:', DESTINATION_CONFIGS_ASSET_KEY);
const reference = makeid(16);
const couldFrontParams = {
DistributionId: CLOUDFRONT_DISTRIBUTION_ID,
InvalidationBatch: {
CallerReference: reference,
Paths: {
Quantity: 1,
Items: [
CLOUDFRONT_INVALIDATION_PATH,
]
}
}
};
await cloudfront.createInvalidation(couldFrontParams).promise();
};