Skip to content

Commit 1c8a393

Browse files
committed
fix(lambda): build & test the ami-updater lambda
1 parent 2540ab6 commit 1c8a393

File tree

11 files changed

+15893
-233
lines changed

11 files changed

+15893
-233
lines changed

lambdas/functions/ami-updater/index.ts

Lines changed: 0 additions & 130 deletions
This file was deleted.
Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
11
{
2-
"name": "@lambda/ami-updater",
2+
"name": "@aws-github-runner/ami-updater",
33
"version": "1.0.0",
4-
"description": "Lambda function to update AMIs in launch templates",
5-
"main": "dist/index.js",
4+
"main": "lambda.ts",
5+
"type": "module",
6+
"license": "MIT",
67
"scripts": {
7-
"build": "tsc",
8+
"start": "ts-node-dev src/local.ts",
89
"test": "vitest run",
910
"test:watch": "vitest",
10-
"lint": "eslint . --ext .ts",
11-
"lint:fix": "eslint . --ext .ts --fix"
11+
"lint": "eslint src",
12+
"watch": "ts-node-dev --respawn --exit-child src/local.ts",
13+
"build": "ncc build src/lambda.ts -o dist",
14+
"dist": "yarn build && cp package.json dist/ && cd dist && zip ../ami-updater.zip *",
15+
"format": "prettier --write \"**/*.ts\"",
16+
"format-check": "prettier --check \"**/*.ts\"",
17+
"all": "yarn build && yarn format && yarn lint && yarn test"
1218
},
1319
"dependencies": {
14-
"@aws-lambda-powertools/logger": "^1.17.0",
15-
"@aws-lambda-powertools/metrics": "^1.17.0",
16-
"@aws-lambda-powertools/tracer": "^1.17.0",
17-
"@aws-sdk/client-ec2": "^3.470.0",
18-
"@types/aws-lambda": "^8.10.130"
20+
"@aws-github-runner/aws-powertools-util": "*",
21+
"@aws-github-runner/aws-ssm-util": "*",
22+
"@aws-sdk/client-ec2": "^3.767.0",
23+
"@aws-sdk/client-ssm": "^3.759.0"
1924
},
2025
"devDependencies": {
26+
"@aws-sdk/types": "^3.734.0",
27+
"@types/aws-lambda": "^8.10.147",
2128
"@types/node": "^20.10.4",
2229
"@typescript-eslint/eslint-plugin": "^6.13.2",
2330
"@typescript-eslint/parser": "^6.13.2",
31+
"@vercel/ncc": "^0.38.3",
32+
"aws-sdk-client-mock": "^4.1.0",
33+
"aws-sdk-client-mock-jest": "^4.1.0",
2434
"eslint": "^8.55.0",
35+
"prettier": "^3.0.0",
2536
"typescript": "^5.3.3",
26-
"vitest": "^1.0.2"
37+
"vitest": "^3.0.9"
38+
},
39+
"nx": {
40+
"includedScripts": [
41+
"build",
42+
"dist",
43+
"format",
44+
"format-check",
45+
"lint",
46+
"start",
47+
"watch",
48+
"all"
49+
]
2750
}
28-
}
51+
}

lambdas/functions/ami-updater/src/__tests__/index.test.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
2-
import { handler } from '../index';
2+
import { handler } from '../lambda';
33
import { AMIManager } from '../ami';
44
import { getConfig } from '../config';
55

66
vi.mock('../ami');
77
vi.mock('../config');
8-
vi.mock('../../shared/aws-powertools-util', () => ({
8+
vi.mock('@aws-github-runner/aws-powertools-util', () => ({
99
logger: {
1010
addContext: vi.fn(),
1111
info: vi.fn(),
1212
error: vi.fn(),
1313
},
14-
metrics: {
15-
addMetric: vi.fn(),
16-
},
1714
}));
1815

1916
describe('Lambda Handler', () => {
@@ -43,13 +40,13 @@ describe('Lambda Handler', () => {
4340
message: 'Updated successfully',
4441
});
4542

46-
await handler({}, mockContext as any);
43+
await handler({}, mockContext);
4744

4845
expect(AMIManager.prototype.getLatestAmi).toHaveBeenCalledWith(mockConfig.amiFilter);
4946
expect(AMIManager.prototype.updateLaunchTemplate).toHaveBeenCalledWith(
5047
mockConfig.launchTemplateName,
5148
'ami-new',
52-
mockConfig.dryRun
49+
mockConfig.dryRun,
5350
);
5451
});
5552

@@ -60,21 +57,21 @@ describe('Lambda Handler', () => {
6057
message: 'Update failed',
6158
});
6259

63-
await expect(handler({}, mockContext as any)).rejects.toThrow('Update failed');
60+
await expect(handler({}, mockContext)).rejects.toThrow('Update failed');
6461
});
6562

6663
it('should handle errors in getLatestAmi', async () => {
6764
const error = new Error('Failed to get AMI');
6865
vi.mocked(AMIManager.prototype.getLatestAmi).mockRejectedValue(error);
6966

70-
await expect(handler({}, mockContext as any)).rejects.toThrow('Failed to get AMI');
67+
await expect(handler({}, mockContext)).rejects.toThrow('Failed to get AMI');
7168
});
7269

7370
it('should handle errors in updateLaunchTemplate', async () => {
7471
vi.mocked(AMIManager.prototype.getLatestAmi).mockResolvedValue('ami-new');
7572
const error = new Error('Failed to update template');
7673
vi.mocked(AMIManager.prototype.updateLaunchTemplate).mockRejectedValue(error);
7774

78-
await expect(handler({}, mockContext as any)).rejects.toThrow('Failed to update template');
75+
await expect(handler({}, mockContext)).rejects.toThrow('Failed to update template');
7976
});
80-
});
77+
});

0 commit comments

Comments
 (0)