-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyToS3.ts
More file actions
169 lines (143 loc) · 4.22 KB
/
Copy pathcopyToS3.ts
File metadata and controls
169 lines (143 loc) · 4.22 KB
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
import * as fs from 'fs';
import * as path from 'path';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import * as archiver from 'archiver';
import * as tmp from 'tmp';
import YAML from 'yaml';
const bucketName = 'lambda-error-sns-sender';
const region = 'eu-west-1';
const cloudFromationOutputYamlFileName = 'lambda-error-sns-sender.yaml';
interface Assets {
files: Record<
string,
{
source: {
path: string;
packaging: string;
};
destinations: {
'current_account-current_region': {
bucketName: string;
objectKey: string;
};
};
}
>;
}
const s3Client = new S3Client({ region });
async function run() {
const jsonFile = fs.readFileSync(
'cdk.out/lambda-error-sns-sender-cf.assets.json',
'utf-8',
);
const assets: Assets = JSON.parse(jsonFile);
const assetsFiles: string[] = [];
let cloudFromationTemplateFile: string | undefined;
for (const key of Object.keys(assets.files)) {
const file = assets.files[key];
const packaging = file.source.packaging;
const destination = file.destinations['current_account-current_region'];
const objectKey = destination.objectKey;
const sourcePath = file.source.path;
const fullPath = path.join('cdk.out', sourcePath);
if (packaging === 'zip') {
// zip folder with library
await uploadZipToS3(fullPath, objectKey);
assetsFiles.push(objectKey);
} else if (sourcePath === 'lambda-error-sns-sender-cf.template.json') {
cloudFromationTemplateFile = fullPath;
} else {
await uploadFileToS3(fullPath, objectKey);
}
}
if (!cloudFromationTemplateFile) {
throw new Error('cloudFromationTemplateFile not found');
}
await convertToYamlAndUploadZipToS3(
cloudFromationTemplateFile,
cloudFromationOutputYamlFileName,
assetsFiles,
);
}
async function convertToYamlAndUploadZipToS3(
fullPath: string,
objectKey: string,
assetsFiles: string[],
) {
console.log(`Converting to yaml and uploading ${fullPath} to ${objectKey}`);
// read json
const jsonString = fs.readFileSync(fullPath, 'utf-8');
const json = JSON.parse(jsonString);
for (const key in json.Resources) {
const resource = json.Resources[key];
const s3Key = resource.Properties?.Code?.S3Key;
if (s3Key && assetsFiles.includes(s3Key)) {
console.log(
`Replacing ${JSON.stringify(
json.Resources[key].Properties.Code.S3Bucket,
)} with ${bucketName} for resource ${s3Key}`,
);
json.Resources[key].Properties.Code.S3Bucket = bucketName;
}
}
const yaml = YAML.stringify(json);
await uploadToS3(yaml, objectKey);
}
async function uploadZipToS3(fullPath: string, objectKey: string) {
console.log(`Ziping and uploading ${fullPath} to ${objectKey}`);
await new Promise((resolve, reject) => {
try {
tmp.file(async (err, zipFilePath, _fd, cleanupCallback) => {
if (err) {
reject(err);
return;
}
try {
await zipFolder(fullPath, zipFilePath);
await uploadFileToS3(zipFilePath, objectKey);
cleanupCallback();
resolve(undefined);
} catch (err2) {
reject(err2);
}
});
} catch (err) {
reject(err);
}
});
}
async function uploadFileToS3(filePath: string, objectKey: string) {
console.log(`Uploading ${filePath} to ${objectKey}`);
const fileStream = fs.createReadStream(filePath);
await uploadToS3(fileStream, objectKey);
}
async function uploadToS3(body: fs.ReadStream | string, objectKey: string) {
const params = {
Bucket: bucketName,
Key: objectKey,
Body: body,
};
await s3Client.send(new PutObjectCommand(params));
}
async function zipFolder(
sourceFolder: string,
zipFilePath: string,
): Promise<void> {
const output = fs.createWriteStream(zipFilePath);
const archive = archiver.create('zip', {
zlib: { level: 9 },
});
await new Promise(async (resolve, reject) => {
try {
archive
.directory(sourceFolder, false)
.on('error', (err) => reject(err))
.pipe(output);
output.on('close', () => resolve(undefined));
await archive.finalize();
} catch (err) {
reject(err);
}
});
}
void run();