Skip to content

Commit ae3372f

Browse files
authored
fix: set architecture when publishing (#6)
1 parent 00de1b9 commit ae3372f

File tree

1 file changed

+128
-57
lines changed

1 file changed

+128
-57
lines changed

scripts/publish.ts

Lines changed: 128 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
import yargs from "https://deno.land/x/[email protected]/deno.ts";
22
import * as log from "https://deno.land/[email protected]/log/mod.ts";
33

4+
type Arch = "x86_64" | "arm64";
45
// Not all regions support multi architecture
56
// Best way I found to find them is to access
67
// https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/create/layer
78
// per region
8-
const regions = [
9-
{ region: "eu-north-1", arch: false },
10-
{ region: "ap-south-1", arch: true },
11-
{ region: "eu-west-3", arch: false },
12-
{ region: "eu-west-2", arch: true },
13-
{ region: "eu-west-1", arch: true },
14-
{ region: "ap-northeast-3", arch: true },
15-
{ region: "ap-northeast-2", arch: true },
16-
{ region: "ap-northeast-1", arch: true },
17-
{ region: "sa-east-1", arch: false },
18-
{ region: "ca-central-1", arch: false },
19-
{ region: "ap-southeast-1", arch: true },
20-
{ region: "ap-southeast-2", arch: false },
21-
{ region: "eu-central-1", arch: true },
22-
{ region: "us-east-1", arch: true },
23-
{ region: "us-east-2", arch: true },
24-
{ region: "us-west-1", arch: false },
25-
{ region: "us-west-2", arch: true },
9+
const regions: { region: string; archs: Arch[] }[] = [
10+
{ region: "eu-north-1", archs: [] },
11+
{ region: "ap-south-1", archs: ["x86_64", "arm64"] },
12+
{ region: "eu-west-3", archs: [] },
13+
{ region: "eu-west-2", archs: ["x86_64", "arm64"] },
14+
{ region: "eu-west-1", archs: ["x86_64", "arm64"] },
15+
{ region: "ap-northeast-3", archs: ["x86_64", "arm64"] },
16+
{ region: "ap-northeast-2", archs: ["x86_64", "arm64"] },
17+
{ region: "ap-northeast-1", archs: ["x86_64", "arm64"] },
18+
{ region: "sa-east-1", archs: [] },
19+
{ region: "ca-central-1", archs: [] },
20+
{ region: "ap-southeast-1", archs: ["x86_64", "arm64"] },
21+
{ region: "ap-southeast-2", archs: [] },
22+
{ region: "eu-central-1", archs: ["x86_64", "arm64"] },
23+
{ region: "us-east-1", archs: ["x86_64", "arm64"] },
24+
{ region: "us-east-2", archs: ["x86_64", "arm64"] },
25+
{ region: "us-west-1", archs: [] },
26+
{ region: "us-west-2", archs: ["x86_64", "arm64"] },
2627
];
2728

2829
const y = yargs(Deno.args)
@@ -68,16 +69,16 @@ await log.setup({
6869
},
6970
});
7071

71-
function getLayerName(name: string, type: "amd" | "arm") {
72-
switch (type) {
73-
case "amd": {
72+
function getLayerName(name: string, arch: Arch) {
73+
switch (arch) {
74+
case "x86_64": {
7475
return `${name}-x86_64`;
7576
}
76-
case "arm": {
77+
case "arm64": {
7778
return `${name}-arm64`;
7879
}
7980
default: {
80-
throw new Error(`Unsupported type: '${type}'`);
81+
throw new Error(`Unsupported arch: '${arch}'`);
8182
}
8283
}
8384
}
@@ -104,6 +105,7 @@ async function publishCmd(
104105
layerName: string,
105106
region: string,
106107
cwd: string,
108+
arch?: string,
107109
): Promise<{ version: string; layernArn: string; fullLayerName: string }> {
108110
if (y.dryRun) {
109111
return {
@@ -114,14 +116,20 @@ async function publishCmd(
114116
};
115117
}
116118

117-
const output = await runCommand([
119+
let cmd = [
118120
"aws",
119121
"lambda",
120122
"publish-layer-version",
121123
`--layer-name=${layerName}`,
122124
`--region=${region}`,
123125
`--zip-file=fileb://extension.zip`,
124-
], { cwd });
126+
];
127+
128+
if (arch) {
129+
cmd = cmd.concat([`--compatible-architectures=${arch}`]);
130+
}
131+
132+
const output = await runCommand(cmd, { cwd });
125133

126134
const parsed = JSON.parse(output);
127135
return {
@@ -134,6 +142,62 @@ async function publishCmd(
134142
};
135143
}
136144

145+
async function publishAmd(
146+
name: string,
147+
region: typeof regions[number]["region"],
148+
withArch?: boolean,
149+
): Promise<{
150+
layerName: string;
151+
version: string;
152+
layernArn: string;
153+
fullLayerName: string;
154+
}> {
155+
const cwd = "bin/x86_64";
156+
const layerName = getLayerName(name, "x86_64");
157+
if (y.dryRun) {
158+
return Promise.resolve({
159+
layerName,
160+
version: "999",
161+
layernArn: `arn:aws:lambda:${region}:myacc:layer:${layerName}`,
162+
fullLayerName: `arn:aws:lambda:${region}:myacc:layer:${layerName}:999`,
163+
});
164+
}
165+
166+
// For zones that only support x86_64, we don't pass an architecture
167+
return {
168+
...(await publishCmd(
169+
layerName,
170+
region,
171+
cwd,
172+
withArch ? "x86_64" : undefined,
173+
)),
174+
layerName,
175+
};
176+
}
177+
178+
async function publishArm(
179+
name: string,
180+
region: typeof regions[number]["region"],
181+
): Promise<{
182+
layerName: string;
183+
version: string;
184+
layernArn: string;
185+
fullLayerName: string;
186+
}> {
187+
const cwd = "bin/arm64";
188+
const layerName = getLayerName(name, "arm64");
189+
if (y.dryRun) {
190+
return Promise.resolve({
191+
layerName,
192+
version: "999",
193+
layernArn: `arn:aws:lambda:${region}:myacc:layer:${layerName}`,
194+
fullLayerName: `arn:aws:lambda:${region}:myacc:layer:${layerName}:999`,
195+
});
196+
}
197+
198+
return { ...(await publishCmd(layerName, region, cwd, "arm64")), layerName };
199+
}
200+
137201
async function makePublic(
138202
{ layerName, region, version }: {
139203
layerName: string;
@@ -170,41 +234,48 @@ async function makePublic(
170234

171235
export async function run() {
172236
log.info("Publishing extension...");
173-
const amd = await Promise.all(regions.map(async (r) => {
174-
const cwd = "bin/x86_64";
175-
const arch = "x86_64";
176-
const layerName = getLayerName(y.name, "amd");
177-
const region = r.region;
178-
179-
log.debug("Publishing", { layerName, region, arch });
180-
const output = await publishCmd(layerName, region, cwd);
181-
log.debug("Published", { ...output });
182-
183-
return { layerName, region, arch, ...output };
184-
}));
185-
const arm = await Promise.all(
186-
regions.filter((r) => r.arch).map(async (r) => {
187-
const cwd = "bin/arm64";
188-
const arch = "arm64";
189-
const layerName = getLayerName(y.name, "arm");
190-
const region = r.region;
191-
192-
log.debug("Publishing");
193-
log.debug({ layerName, region, arch });
194-
const output = await publishCmd(layerName, region, cwd);
195-
log.debug("Published");
196-
log.debug({ ...output });
197-
198-
return { layerName, region, arch, ...output };
199-
}),
200-
);
201237

202-
const out = [...amd, ...arm];
238+
const all = (await Promise.all(regions.map(async (r) => {
239+
log.debug(r);
240+
241+
// Since there's only a single architecture in this region
242+
// We don't ask to specificy a specific arch
243+
if (!r.archs.length) {
244+
log.debug("Region has no arch, defaulting to x86_64");
245+
const amd = await publishAmd(y.name, r.region, false);
246+
return [{ ...amd, region: r.region, arch: "x86_64" }];
247+
}
248+
249+
return await Promise.all(r.archs.map(async (arch) => {
250+
switch (arch) {
251+
case "x86_64": {
252+
log.debug("Publishing x86_64");
253+
return {
254+
...(await publishAmd(y.name, r.region, true)),
255+
region: r.region,
256+
arch,
257+
};
258+
}
259+
case "arm64": {
260+
log.debug("Publishing arm64");
261+
return {
262+
...(await publishArm(y.name, r.region)),
263+
region: r.region,
264+
arch,
265+
};
266+
}
267+
default: {
268+
throw new Error(`Invalid arch ${arch}`);
269+
}
270+
}
271+
}));
272+
}))).flat();
203273

204274
log.info("Making extensions public...");
205275
Promise.all(
206-
out.map(async ({ layerName, version, region }) => {
207-
log.debug("Making it public", { layerName, version, region });
276+
all.map(async ({ layerName, version, region }) => {
277+
log.debug("Making it public");
278+
log.debug({ layerName, version, region });
208279
const output = await makePublic({ layerName, version, region });
209280
log.debug("Done.");
210281
log.debug({ layerName, version, region });
@@ -213,7 +284,7 @@ export async function run() {
213284
}),
214285
);
215286

216-
return out.map(({ region, arch, fullLayerName }) => {
287+
return all.map(({ region, arch, fullLayerName }) => {
217288
return {
218289
region,
219290
arch,

0 commit comments

Comments
 (0)