Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit a62cfff

Browse files
authored
refactor: structure of lambda code (#3224)
1 parent 43acb08 commit a62cfff

File tree

109 files changed

+9388
-14926
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+9388
-14926
lines changed

Diff for: .ci/Dockerfile

+8-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
#syntax=docker/dockerfile:1.2
2-
FROM node:16 as build
3-
WORKDIR /lambda
2+
FROM node:18 as build
3+
WORKDIR /lambdas
44
RUN apt-get update \
55
&& apt-get install -y zip \
66
&& rm -rf /var/lib/apt/lists/*
77

8-
FROM build as runner-binaries-syncer
9-
COPY modules/runner-binaries-syncer/lambdas/runner-binaries-syncer /lambda
10-
RUN --mount=type=cache,target=/lambda/node_modules,id=runner-binaries-syncer \
11-
yarn install && yarn dist
12-
13-
FROM build as runners
14-
COPY modules/runners/lambdas/runners /lambda
15-
RUN --mount=type=cache,target=/lambda/node_modules,id=runners \
16-
yarn install && yarn dist
17-
18-
FROM build as webhook
19-
COPY modules/webhook/lambdas/webhook /lambda
20-
RUN --mount=type=cache,target=/lambda/node_modules,id=webhook \
21-
yarn install && yarn dist
8+
FROM build as lambdas
9+
COPY lambdas /lambdas
10+
RUN --mount=type=cache,target=/work/node_modules,id=lambdas \
11+
yarn install --frozen-lockfile && yarn dist && \
12+
find . -name "*.zip" -exec cp {} . \;
2213

2314
FROM scratch as final
24-
COPY --from=runner-binaries-syncer /lambda/runner-binaries-syncer.zip /runner-binaries-syncer.zip
25-
COPY --from=runners /lambda/runners.zip /runners.zip
26-
COPY --from=webhook /lambda/webhook.zip /webhook.zip
15+
COPY --from=lambdas /lambdas/*.zip /

Diff for: .ci/build-yarn.sh

-11
This file was deleted.

Diff for: .github/workflows/lambda-runner-binaries-syncer.yml

-14
This file was deleted.

Diff for: .github/workflows/lambda-runners.yml

-14
This file was deleted.

Diff for: .github/workflows/lambda-webhook.yml

-14
This file was deleted.

Diff for: .github/workflows/lambda-template.yml renamed to .github/workflows/lambda.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
name: Lambda Syncer
1+
name: Build lambdas
22
on:
3-
workflow_call:
4-
inputs:
5-
working-directory:
6-
required: true
7-
type: string
3+
pull_request:
4+
branches:
5+
- main
6+
paths:
7+
- 'lambdas/**'
88

99
jobs:
1010
build:
@@ -16,12 +16,12 @@ jobs:
1616
image: node:${{ matrix.node }}
1717
defaults:
1818
run:
19-
working-directory: ${{ inputs.working-directory }}/${{ inputs.image }}
19+
working-directory: ./lambdas
2020

2121
steps:
2222
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
2323
- name: Install dependencies
24-
run: yarn install
24+
run: yarn install --frozen-lockfile
2525
- name: Run prettier
2626
run: yarn format-check
2727
- name: Run linter

Diff for: .github/workflows/release.yml

+10-37
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ on:
55
- main
66
- v1
77
workflow_dispatch:
8+
89
jobs:
9-
prepare:
10-
name: Create dist
10+
release:
11+
name: Release
1112
runs-on: ubuntu-latest
1213
container: node:18
13-
strategy:
14-
matrix:
15-
lambda: ["modules/webhook/lambdas/webhook", "modules/runner-binaries-syncer/lambdas/runner-binaries-syncer", "modules/runners/lambdas/runners"]
1614
permissions:
17-
contents: read
15+
contents: write
1816
actions: write
1917
steps:
2018
- name: Extract lambda name
@@ -26,21 +24,8 @@ jobs:
2624
- name: Add zip
2725
run: apt update && apt install zip
2826
- name: Build dist
29-
working-directory: ${{ matrix.lambda }}
27+
working-directory: lambdas
3028
run: yarn install && yarn run test && yarn dist
31-
- uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # ratchet:actions/upload-artifact@v3
32-
with:
33-
name: ${{ steps.lambda.outputs.name }}
34-
path: ${{ matrix.lambda }}/${{ steps.lambda.outputs.name }}.zip
35-
retention-days: 1
36-
release:
37-
name: release
38-
runs-on: ubuntu-latest
39-
needs: prepare
40-
outputs:
41-
releases_created: ${{ steps.release.outputs.releases_created }}
42-
tag_name: ${{ steps.release.outputs.tag_name }}
43-
steps:
4429
- name: Get installation token
4530
uses: philips-software/app-token-action@a37926571e4cec6f219e06727136efdd073d8657 # ratchet:philips-software/[email protected]
4631
id: token
@@ -59,23 +44,11 @@ jobs:
5944
default-branch: ${{ steps.branch.outputs.name }}
6045
release-type: terraform-module
6146
token: ${{ steps.token.outputs.token }}
62-
assets:
63-
name: upload assets
64-
if: ${{ needs.release.outputs.releases_created }}
65-
needs: release
66-
runs-on: ubuntu-latest
67-
strategy:
68-
matrix:
69-
asset: ["webhook", "runner-binaries-syncer", "runners"]
70-
permissions:
71-
contents: write
72-
actions: read
73-
steps:
74-
- uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # ratchet:actions/checkout@v3
75-
- uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # ratchet:actions/download-artifact@v3
76-
with:
77-
name: ${{ matrix.asset }}
7847
- name: Upload Release Asset
48+
if: ${{ steps.release.outputs.releases_created }}
7949
env:
8050
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
81-
run: gh release upload ${{ needs.release.outputs.tag_name }} ${{ matrix.asset }}.zip
51+
run: |
52+
for f in $(find . -name '*.zip'); do
53+
gh release upload ${{ needs.release.outputs.tag_name }} $f
54+
done

Diff for: .github/workflows/terraform.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ jobs:
2727
uses: actions/checkout@v3
2828
- name: "Fake zip files" # Validate will fail if it cannot find the zip files
2929
run: |
30-
touch modules/webhook/lambdas/webhook/webhook.zip
31-
touch modules/runners/lambdas/runners/runners.zip
32-
touch modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/runner-binaries-syncer.zip
30+
touch lambdas/functions/webhook/webhook.zip
31+
touch lambdas/functions/control-plane/runners.zip
32+
touch lambdas/functions/gh-agent-syncer/runner-binaries-syncer.zip
3333
- name: terraform init
3434
run: terraform init -get -backend=false -input=false
3535
- if: contains(matrix.terraform, '1.3.')

Diff for: .vscode/settings.json

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
{
2-
"sonarlint.rules": {
3-
"javascript:S4123": {
4-
"level": "off"
5-
}
6-
}
7-
}
1+
{}

Diff for: lambdas/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules/
2+
build/
3+
dist/
4+
*.log
5+
6+
# Ignore all yarn.lock files except the one in the root
7+
**/yarn.lock
8+
!/yarn.lock

Diff for: lambdas/functions/control-plane/jest.config.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { Config } from 'jest';
2+
3+
import defaultConfig from '../../jest.base.config';
4+
5+
const config: Config = {
6+
...defaultConfig,
7+
coverageThreshold: {
8+
global: {
9+
statements: 97,
10+
branches: 93,
11+
functions: 96,
12+
lines: 98,
13+
},
14+
},
15+
};
16+
17+
export default config;

Diff for: modules/runners/lambdas/runners/package.json renamed to lambdas/functions/control-plane/package.json

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "github-runner-lambda-scale-runners",
2+
"name": "@terraform-aws-github-runner/control-plane",
33
"version": "1.0.0",
44
"main": "lambda.ts",
55
"license": "MIT",
@@ -16,10 +16,8 @@
1616
"all": "yarn build && yarn format && yarn lint && yarn test"
1717
},
1818
"devDependencies": {
19-
"@babel/helper-get-function-arity": "^7.16.7",
2019
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
2120
"@types/aws-lambda": "^8.10.114",
22-
"@types/express": "^4.17.17",
2321
"@types/jest": "^29.5.0",
2422
"@types/node": "^18.11.18",
2523
"@typescript-eslint/eslint-plugin": "^5.57.0",
@@ -34,19 +32,19 @@
3432
"jest-mock-extended": "^3.0.4",
3533
"moment-timezone": "^0.5.43",
3634
"nock": "^13.3.0",
37-
"prettier": "2.8.7",
35+
"prettier": "2.8.8",
3836
"ts-jest": "^29.1.0",
3937
"ts-node": "^10.9.1",
4038
"ts-node-dev": "^2.0.0"
4139
},
4240
"dependencies": {
43-
"@aws-lambda-powertools/logger": "^1.8.0",
4441
"@aws-sdk/client-ec2": "^3.303.0",
45-
"@aws-sdk/client-ssm": "^3.303.0",
4642
"@octokit/auth-app": "4.0.9",
4743
"@octokit/rest": "^19.0.7",
4844
"@octokit/types": "^9.0.0",
45+
"@terraform-aws-github-runner/aws-ssm-util": "*",
46+
"@terraform-aws-github-runner/aws-powertools-util": "*",
4947
"cron-parser": "^4.8.1",
50-
"typescript": "^4.9.5"
48+
"typescript": "^5.0.4"
5149
}
5250
}

Diff for: modules/runners/lambdas/runners/src/aws/runners.ts renamed to lambdas/functions/control-plane/src/aws/runners.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
TerminateInstancesCommand,
99
} from '@aws-sdk/client-ec2';
1010
import { SSM } from '@aws-sdk/client-ssm';
11+
import { createChildLogger } from '@terraform-aws-github-runner/aws-powertools-util';
1112
import moment from 'moment';
1213

13-
import { createChildLogger } from '../logger';
1414
import ScaleError from './../scale-runners/ScaleError';
1515
import * as Runners from './runners.d';
1616

Diff for: modules/runners/lambdas/runners/src/gh-auth/gh-auth.test.ts renamed to lambdas/functions/control-plane/src/gh-auth/gh-auth.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { createAppAuth } from '@octokit/auth-app';
22
import { StrategyOptions } from '@octokit/auth-app/dist-types/types';
33
import { request } from '@octokit/request';
44
import { RequestInterface } from '@octokit/types';
5+
import { getParameterValue } from '@terraform-aws-github-runner/aws-ssm-util';
56
import { mocked } from 'jest-mock';
67
import { MockProxy, mock } from 'jest-mock-extended';
78
import nock from 'nock';
89

9-
import { getParameterValue } from './../aws/ssm';
1010
import { createGithubAppAuth, createOctoClient } from './gh-auth';
1111

12-
jest.mock('./../aws/ssm');
12+
jest.mock('@terraform-aws-github-runner/aws-ssm-util');
1313
jest.mock('@octokit/auth-app');
1414

1515
const cleanEnv = process.env;

Diff for: modules/runners/lambdas/runners/src/gh-auth/gh-auth.ts renamed to lambdas/functions/control-plane/src/gh-auth/gh-auth.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import {
1010
import { OctokitOptions } from '@octokit/core/dist-types/types';
1111
import { request } from '@octokit/request';
1212
import { Octokit } from '@octokit/rest';
13-
14-
import { getParameterValue } from '../aws/ssm';
15-
import { createChildLogger } from '../logger';
13+
import { createChildLogger } from '@terraform-aws-github-runner/aws-powertools-util';
14+
import { getParameterValue } from '@terraform-aws-github-runner/aws-ssm-util';
1615

1716
const logger = createChildLogger('gh-auth');
1817

Diff for: modules/runners/lambdas/runners/src/lambda.test.ts renamed to lambdas/functions/control-plane/src/lambda.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { logger } from '@terraform-aws-github-runner/aws-powertools-util';
12
import { Context, SQSEvent, SQSRecord } from 'aws-lambda';
23
import { mocked } from 'jest-mock';
34

45
import { adjustPool, scaleDownHandler, scaleUpHandler } from './lambda';
5-
import { logger } from './logger';
66
import { adjust } from './pool/pool';
77
import ScaleError from './scale-runners/ScaleError';
88
import { scaleDown } from './scale-runners/scale-down';
@@ -61,7 +61,7 @@ const context: Context = {
6161
jest.mock('./scale-runners/scale-up');
6262
jest.mock('./scale-runners/scale-down');
6363
jest.mock('./pool/pool');
64-
jest.mock('./logger');
64+
jest.mock('@terraform-aws-github-runner/aws-powertools-util');
6565

6666
// Docs for testing async with jest: https://jestjs.io/docs/tutorial-async
6767
describe('Test scale up lambda wrapper.', () => {

Diff for: modules/runners/lambdas/runners/src/lambda.ts renamed to lambdas/functions/control-plane/src/lambda.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { logger, setContext } from '@terraform-aws-github-runner/aws-powertools-util';
12
import { Context, SQSEvent } from 'aws-lambda';
23
import 'source-map-support/register';
34

4-
import { logger, setContext } from './logger';
55
import { PoolEvent, adjust } from './pool/pool';
66
import ScaleError from './scale-runners/ScaleError';
77
import { scaleDown } from './scale-runners/scale-down';

Diff for: modules/runners/lambdas/runners/src/local.ts renamed to lambdas/functions/control-plane/src/local.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { logger } from './logger';
1+
import { logger } from '@terraform-aws-github-runner/aws-powertools-util';
2+
23
import { ActionRequestMessage, scaleUp } from './scale-runners/scale-up';
34

45
const sqsEvent = {

Diff for: modules/runners/lambdas/runners/src/pool/pool.ts renamed to lambdas/functions/control-plane/src/pool/pool.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { createChildLogger } from '@terraform-aws-github-runner/aws-powertools-util';
12
import yn from 'yn';
23

34
import { bootTimeExceeded, listEC2Runners } from '../aws/runners';
45
import { createGithubAppAuth, createGithubInstallationAuth, createOctoClient } from '../gh-auth/gh-auth';
5-
import { createChildLogger } from '../logger';
66
import { createRunners } from '../scale-runners/scale-up';
77

88
const logger = createChildLogger('pool');

Diff for: modules/runners/lambdas/runners/src/scale-runners/scale-down.ts renamed to lambdas/functions/control-plane/src/scale-runners/scale-down.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Octokit } from '@octokit/rest';
2+
import { createChildLogger } from '@terraform-aws-github-runner/aws-powertools-util';
23
import moment from 'moment';
34

45
import { createGithubAppAuth, createGithubInstallationAuth, createOctoClient } from '../gh-auth/gh-auth';
5-
import { createChildLogger } from '../logger';
66
import { bootTimeExceeded, listEC2Runners, terminateRunner } from './../aws/runners';
77
import { RunnerInfo, RunnerList } from './../aws/runners.d';
88
import { GhRunners, githubCache } from './cache';

Diff for: modules/runners/lambdas/runners/src/scale-runners/scale-up.ts renamed to lambdas/functions/control-plane/src/scale-runners/scale-up.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Octokit } from '@octokit/rest';
2+
import { addPersistentContextToChildLogger, createChildLogger } from '@terraform-aws-github-runner/aws-powertools-util';
23
import yn from 'yn';
34

45
import { createGithubAppAuth, createGithubInstallationAuth, createOctoClient } from '../gh-auth/gh-auth';
5-
import { addPersistentContextToChildLogger, createChildLogger } from '../logger';
66
import { createRunner, listEC2Runners } from './../aws/runners';
77
import { RunnerInputParameters } from './../aws/runners.d';
88
import ScaleError from './ScaleError';

0 commit comments

Comments
 (0)