Skip to content

Commit b233582

Browse files
authored
feat: Allow Lambda provider to use any version of Linux (#584)
Previously we required Amazon Linux 2023 (and the error message erroneously said Amazon Linux 2). With this change, we will still be using Amazon Linux 2023 by default, but Ubuntu and Amazon Linux 2 will also be supported. Note that this changes the default base image from `public.ecr.aws/lambda/nodejs:20-x86_64` to `public.ecr.aws/amazonlinux/amazonlinux:2023`. The new image contains all the packages in the old one plus a few more Python packages. The new image is a bit smaller so might load faster. The only difference should be nodejs 20 in `/var/lang` which also includes the Lambda runtime. Resolves #510 Resolves #423 BREAKING CHANGE: Node.js is no longer included by default with Lambda runner images
1 parent 4474a51 commit b233582

File tree

10 files changed

+96
-68
lines changed

10 files changed

+96
-68
lines changed

.projen/tasks.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projenrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ releaseWorkflow.file.addDeletionOverride('on.push');
119119

120120
// bundle docker images
121121
project.bundler.bundleTask.exec('cp -r src/providers/docker-images assets');
122+
project.bundler.bundleTask.exec('cp -r src/providers/lambda-*.sh assets/providers');
122123

123124
// set proper line endings
124125
project.gitattributes.addAttributes('*.js', 'eol=lf');

API.md

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/image-builders/components.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ export abstract class RunnerImageComponent {
509509
name = 'Lambda-Entrypoint';
510510

511511
getCommands(os: Os, _architecture: Architecture) {
512-
if (!os.is(Os.LINUX_AMAZON_2) && !os.is(Os.LINUX_AMAZON_2023) && !os.is(Os.LINUX_UBUNTU)) {
512+
if (!os.isIn(Os._ALL_LINUX_VERSIONS)) {
513513
throw new Error(`Unsupported OS for Lambda entrypoint: ${os.name}`);
514514
}
515515

@@ -519,20 +519,19 @@ export abstract class RunnerImageComponent {
519519
getAssets(_os: Os, _architecture: Architecture): RunnerImageAsset[] {
520520
return [
521521
{
522-
source: path.join(__dirname, '..', '..', 'assets', 'docker-images', 'lambda', 'linux-x64', 'runner.js'),
523-
target: '${LAMBDA_TASK_ROOT}/runner.js',
522+
source: path.join(__dirname, '..', '..', 'assets', 'providers', 'lambda-bootstrap.sh'),
523+
target: '/bootstrap.sh',
524524
},
525525
{
526-
source: path.join(__dirname, '..', '..', 'assets', 'docker-images', 'lambda', 'linux-x64', 'runner.sh'),
527-
target: '${LAMBDA_TASK_ROOT}/runner.sh',
526+
source: path.join(__dirname, '..', '..', 'assets', 'providers', 'lambda-runner.sh'),
527+
target: '/runner.sh',
528528
},
529529
];
530530
}
531531

532532
getDockerCommands(_os: Os, _architecture: Architecture): string[] {
533533
return [
534-
'WORKDIR ${LAMBDA_TASK_ROOT}',
535-
'CMD ["runner.handler"]',
534+
'ENTRYPOINT ["bash", "/bootstrap.sh"]',
536535
];
537536
}
538537
};

src/providers/lambda-bootstrap.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
while true
6+
do
7+
# get data from lambda
8+
HEADERS="$(mktemp)"
9+
EVENT_DATA=$(curl -sS -LD "$HEADERS" "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next")
10+
REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)
11+
12+
# execute runner and respond
13+
if bash /runner.sh "$EVENT_DATA"; then
14+
curl "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/response" -d ""
15+
else
16+
curl "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/error" -d "{\"errorMessage\": \"Runner failed with exit code $?\", \"errorType\": \"Error\", \"stackTrace\": []}"
17+
fi
18+
19+
# cleanup
20+
find /tmp -mindepth 1 -maxdepth 1 -exec rm -rf '{}' \;
21+
done

src/providers/lambda-runner.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# workaround for "Cannot get required symbol EVP_rc2_cbc from libssl"
6+
# lambda docker image for node.js comes with stripped down libssl.so pushed in LD_LIBRARY_PATH
7+
if [ -f /var/lang/lib/libssl.so ]; then
8+
export LD_LIBRARY_PATH=/usr/lib64:$LD_LIBRARY_PATH
9+
fi
10+
11+
# extract parameters
12+
OWNER=$(echo "$1" | jq -r .owner)
13+
REPO=$(echo "$1" | jq -r .repo)
14+
GITHUB_DOMAIN=$(echo "$1" | jq -r .githubDomain)
15+
RUNNER_TOKEN=$(echo "$1" | jq -r .token)
16+
RUNNER_NAME=$(echo "$1" | jq -r .runnerName)
17+
RUNNER_LABEL=$(echo "$1" | jq -r .label)
18+
REGISTRATION_URL=$(echo "$1" | jq -r .registrationUrl)
19+
20+
# copy runner code (it needs a writable directory)
21+
cp -r /home/runner /tmp/
22+
cd /tmp/runner
23+
24+
# setup home directory
25+
mkdir /tmp/home
26+
export HOME=/tmp/home
27+
28+
# start runner
29+
if [ "${RUNNER_VERSION}" = "latest" ]; then RUNNER_FLAGS=""; else RUNNER_FLAGS="--disableupdate"; fi
30+
./config.sh --unattended --url "${REGISTRATION_URL}" --token "${RUNNER_TOKEN}" --ephemeral --work _work --labels "${RUNNER_LABEL},cdkghr:started:`date +%s`" --name "${RUNNER_NAME}" ${RUNNER_FLAGS}
31+
echo Config done
32+
./run.sh
33+
echo Run done
34+
35+
# print status for metrics
36+
STATUS=$(grep -Phors "finish job request for job [0-9a-f\-]+ with result: \K.*" _diag/ | tail -n1)
37+
[ -n "$STATUS" ] && echo CDKGHA JOB DONE "$RUNNER_LABEL" "$STATUS"

src/providers/lambda.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export class LambdaRunnerProvider extends BaseProvider implements IRunnerProvide
153153
*
154154
* You can add components to the image builder by calling `imageBuilder.addComponent()`.
155155
*
156-
* The default OS is Amazon Linux 2 running on x64 architecture.
156+
* The default OS is Amazon Linux 2023 running on x64 architecture.
157157
*
158158
* Included components:
159159
* * `RunnerImageComponent.requiredPackages()`
@@ -163,24 +163,11 @@ export class LambdaRunnerProvider extends BaseProvider implements IRunnerProvide
163163
* * `RunnerImageComponent.awsCli()`
164164
* * `RunnerImageComponent.githubRunner()`
165165
* * `RunnerImageComponent.lambdaEntrypoint()`
166-
*
167-
* Base Docker image: `public.ecr.aws/lambda/nodejs:20-x86_64` or `public.ecr.aws/lambda/nodejs:20-arm64`
168166
*/
169167
public static imageBuilder(scope: Construct, id: string, props?: RunnerImageBuilderProps) {
170-
if (props?.os && !Os.LINUX_AMAZON_2.is(props.os) && !props?.baseDockerImage) {
171-
// TODO we can support Ubuntu by building our own image https://docs.aws.amazon.com/lambda/latest/dg/nodejs-image.html#nodejs-image-clients
172-
throw new Error('Lambda runner provider only supports Amazon Linux 2. Use a different provider or specify a custom `baseDockerImage` that supports your desired OS.');
173-
}
174-
175-
let baseDockerImage = 'public.ecr.aws/lambda/nodejs:20-x86_64';
176-
if (props?.architecture === Architecture.ARM64) {
177-
baseDockerImage = 'public.ecr.aws/lambda/nodejs:20-arm64';
178-
}
179-
180168
return RunnerImageBuilder.new(scope, id, {
181169
os: Os.LINUX_AMAZON_2023,
182-
architecture: props?.architecture ?? Architecture.X86_64,
183-
baseDockerImage,
170+
architecture: Architecture.X86_64,
184171
components: [
185172
RunnerImageComponent.requiredPackages(),
186173
RunnerImageComponent.runnerUser(),

test/default.integ.snapshot/github-runners-test.assets.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,28 @@
4040
}
4141
}
4242
},
43-
"68628bab8c925c01632702a86513d4b22840e7fbb138f290bab11c2c2d54c489": {
43+
"2fc3b84da69dcc5adb6dc4721b50c1166474fa7e5fd5f242e833d12ac28e09d9": {
4444
"source": {
45-
"path": "asset.68628bab8c925c01632702a86513d4b22840e7fbb138f290bab11c2c2d54c489.js",
45+
"path": "asset.2fc3b84da69dcc5adb6dc4721b50c1166474fa7e5fd5f242e833d12ac28e09d9.sh",
4646
"packaging": "file"
4747
},
4848
"destinations": {
4949
"current_account-current_region": {
5050
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
51-
"objectKey": "68628bab8c925c01632702a86513d4b22840e7fbb138f290bab11c2c2d54c489.js",
51+
"objectKey": "2fc3b84da69dcc5adb6dc4721b50c1166474fa7e5fd5f242e833d12ac28e09d9.sh",
5252
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
5353
}
5454
}
5555
},
56-
"66540a3450c33faeefad87df9de8684f624030603c76336933c519972d85a072": {
56+
"7be6b27ef13a5bba7f44b1d9c6e50fc2c68fdb40d51cef42cee01f27c38842a9": {
5757
"source": {
58-
"path": "asset.66540a3450c33faeefad87df9de8684f624030603c76336933c519972d85a072.sh",
58+
"path": "asset.7be6b27ef13a5bba7f44b1d9c6e50fc2c68fdb40d51cef42cee01f27c38842a9.sh",
5959
"packaging": "file"
6060
},
6161
"destinations": {
6262
"current_account-current_region": {
6363
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
64-
"objectKey": "66540a3450c33faeefad87df9de8684f624030603c76336933c519972d85a072.sh",
64+
"objectKey": "7be6b27ef13a5bba7f44b1d9c6e50fc2c68fdb40d51cef42cee01f27c38842a9.sh",
6565
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
6666
}
6767
}
@@ -209,15 +209,15 @@
209209
}
210210
}
211211
},
212-
"54ea0c28666f3fa077b83dbc9a02d3d6d3304dbd52deb343fc69e6a7ca821b0e": {
212+
"8368627222f528d1ca1bdcdde41640c2fe68400d3d98ed2e5699e5ff524c02f3": {
213213
"source": {
214214
"path": "github-runners-test.template.json",
215215
"packaging": "file"
216216
},
217217
"destinations": {
218218
"current_account-current_region": {
219219
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
220-
"objectKey": "54ea0c28666f3fa077b83dbc9a02d3d6d3304dbd52deb343fc69e6a7ca821b0e.json",
220+
"objectKey": "8368627222f528d1ca1bdcdde41640c2fe68400d3d98ed2e5699e5ff524c02f3.json",
221221
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
222222
}
223223
}

0 commit comments

Comments
 (0)