-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
145 lines (136 loc) · 4.26 KB
/
index.js
File metadata and controls
145 lines (136 loc) · 4.26 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
const core = require("@actions/core");
const fs = require("fs");
const {
LambdaClient,
UpdateFunctionCodeCommand,
UpdateFunctionConfigurationCommand,
} = require("@aws-sdk/client-lambda");
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
async function run() {
// Get all parameters
const ZIP = core.getInput("ZIP");
const FUNCTION_NAME = core.getInput("FUNCTION_NAME");
const AWS_REGION = core.getInput("AWS_REGION");
const AWS_SECRET_ID = core.getInput("AWS_SECRET_ID");
const AWS_SECRET_KEY = core.getInput("AWS_SECRET_KEY");
const RUNTIME = core.getInput("RUNTIME");
const ROLE = core.getInput("ROLE");
const HANDLER = core.getInput("HANDLER");
const DESCRIPTION = core.getInput("DESCRIPTION");
const TIMEOUT = core.getInput("TIMEOUT");
const MEMORY_SIZE = core.getInput("MEMORY_SIZE");
const ARCHITECTURES = core.getInput("ARCHITECTURES");
const ENVIRONMENT = core.getInput("ENVIRONMENT");
const S3_BUCKET = core.getInput("S3_BUCKET");
const S3_KEY = core.getInput("S3_KEY");
const IMAGE_URI = core.getInput("IMAGE_URI");
// Check mandatory params
if (!FUNCTION_NAME) {
throw "No FUNCTION_NAME provided!";
}
if (!AWS_REGION) {
throw "No AWS_REGION provided!";
}
if (!AWS_SECRET_ID) {
throw "No AWS_SECRET_ID provided!";
}
if (!AWS_SECRET_KEY) {
throw "No AWS_SECRET_KEY provided!";
}
const awsIdentityProvider = () =>
Promise.resolve({
accessKeyId: AWS_SECRET_ID,
secretAccessKey: AWS_SECRET_KEY,
});
const awsConfig = {
apiVersion: "2015-03-31",
region: AWS_REGION,
credentials: awsIdentityProvider,
maxRetries: 3,
sslEnabled: true,
logger: console,
};
console.log(`Update ${FUNCTION_NAME} in ${AWS_REGION}.`);
const updateParams = {
FunctionName: FUNCTION_NAME,
Publish: true,
};
const updateParamIfPresent = (paramName, paramValue) => {
if (paramValue) {
updateParams[paramName] = paramValue;
}
};
if (ZIP) {
updateParams["PackageType"] = "Zip";
const zipBuffer = readZip(`./${ZIP}`);
const uploadOverS3 = S3_BUCKET && S3_KEY;
if (uploadOverS3) {
console.log(`Upload to ${S3_BUCKET} as ${S3_KEY}.`);
updateParams["S3Bucket"] = S3_BUCKET;
updateParams["S3Key"] = S3_KEY;
const uploadCommand = new PutObjectCommand({
Bucket: S3_BUCKET,
Key: S3_KEY,
Body: zipBuffer,
});
const s3Client = new S3Client(awsConfig);
const response = await s3Client.send(uploadCommand);
console.log(response);
updateParamIfPresent("S3ObjectVersion", response.VersionId);
} else {
console.log(`Direct upload.`);
updateParams["ZipFile"] = zipBuffer;
}
}
if (IMAGE_URI) {
updateParams["PackageType"] = "Image";
updateParams["ImageUri"] = IMAGE_URI;
} else {
updateParamIfPresent("Runtime", RUNTIME);
updateParamIfPresent("Handler", HANDLER);
if (ENVIRONMENT) {
const Variables = JSON.parse(ENVIRONMENT);
updateParams["Environment"] = { Variables };
}
}
// add optional params
updateParamIfPresent("Role", ROLE);
updateParamIfPresent("Description", DESCRIPTION);
updateParamIfPresent("Timeout", convertOptionalToNumber(TIMEOUT));
updateParamIfPresent("MemorySize", convertOptionalToNumber(MEMORY_SIZE));
updateParamIfPresent(
"Architectures",
splitOptional(ARCHITECTURES) || ["x86_64"]
);
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/lambda/command/UpdateFunctionCodeCommand/
const updateCommand =
ZIP || IMAGE_URI
? new UpdateFunctionCodeCommand(updateParams)
: new UpdateFunctionConfigurationCommand(updateParams);
const lambdaClient = new LambdaClient(awsConfig);
const response = await lambdaClient.send(updateCommand);
console.log(response);
}
(async function () {
try {
await run();
} catch (error) {
console.log(error);
core.error(error.message);
core.setFailed(error.message);
}
})();
// HELPER FUNCTIONS
function readZip(path) {
const zipBuffer = fs.readFileSync(path);
if (zipBuffer) {
core.debug("ZIP read into memory.");
}
return zipBuffer;
}
function convertOptionalToNumber(it) {
return it ? Number(it) : undefined;
}
function splitOptional(it, separator = ",") {
return it ? it.split(separator) : undefined;
}