Skip to content

Commit 27b60d5

Browse files
feature: support serverless.tf
1 parent 282ef04 commit 27b60d5

File tree

4 files changed

+81
-19
lines changed

4 files changed

+81
-19
lines changed

src/frameworks/terraformFramework.ts

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@ import { LldConfigBase } from '../types/lldConfig.js';
1212

1313
export const execAsync = promisify(exec);
1414

15-
interface TerraformState {
16-
resources: Array<{
17-
type: string;
18-
name: string;
19-
values: {
20-
function_name?: string;
21-
handler?: string;
22-
source_dir?: string;
23-
source_file?: string;
15+
interface TerraformResource {
16+
type: string;
17+
name: string;
18+
address: string;
19+
values: {
20+
function_name?: string;
21+
handler?: string;
22+
source_dir?: string;
23+
source_file?: string;
24+
query?: {
25+
source_path?: string;
2426
};
25-
//dependencies > depends_on
26-
depends_on: Array<string>;
27-
}>;
27+
};
28+
//dependencies > depends_on
29+
depends_on: Array<string>;
2830
}
2931

3032
/**
@@ -151,15 +153,15 @@ export class TerraformFramework implements IFramework {
151153
return lambdasDiscovered;
152154
}
153155

154-
protected extractLambdaInfo(state: TerraformState) {
156+
protected extractLambdaInfo(resources: TerraformResource[]) {
155157
const lambdas: Array<{
156158
functionName: string;
157159
sourceDir?: string;
158160
sourceFilename?: string;
159161
handler: string;
160162
}> = [];
161163

162-
for (const resource of state.resources) {
164+
for (const resource of resources) {
163165
if (resource.type === 'aws_lambda_function') {
164166
Logger.verbose(
165167
'[Terraform] Found Lambda:',
@@ -186,9 +188,7 @@ export class TerraformFramework implements IFramework {
186188
if (archiveFileResourceName) {
187189
// get the resource
188190
const name = archiveFileResourceName.split('.')[2];
189-
const archiveFileResource = state.resources.find(
190-
(r) => r.name === name,
191-
);
191+
const archiveFileResource = resources.find((r) => r.name === name);
192192

193193
// get source_dir or source_filename
194194
if (archiveFileResource) {
@@ -197,6 +197,28 @@ export class TerraformFramework implements IFramework {
197197
}
198198
}
199199

200+
// get dependency "archive_prepare" = serverless.tf support
201+
const archivePrepareResourceName = dependencies.find((dep) =>
202+
dep.includes('.archive_prepare'),
203+
);
204+
205+
if (archivePrepareResourceName) {
206+
// get the resource
207+
const name = archivePrepareResourceName;
208+
const archivePrepareResource = resources.find((r) =>
209+
r.address?.startsWith(name),
210+
);
211+
212+
// get source_dir or source_filename
213+
if (archivePrepareResource) {
214+
sourceDir =
215+
archivePrepareResource.values.query?.source_path?.replaceAll(
216+
'"',
217+
'',
218+
);
219+
}
220+
}
221+
200222
if (!sourceDir && !sourceFilename) {
201223
Logger.error(`Failed to find source code for Lambda ${functionName}`);
202224
} else {
@@ -213,7 +235,7 @@ export class TerraformFramework implements IFramework {
213235
return lambdas;
214236
}
215237

216-
protected async readTerraformState(): Promise<TerraformState> {
238+
protected async readTerraformState(): Promise<TerraformResource[]> {
217239
// Is there a better way to get the Terraform state???
218240

219241
let output: any;
@@ -254,7 +276,16 @@ export class TerraformFramework implements IFramework {
254276

255277
try {
256278
const state = JSON.parse(jsonString);
257-
return state.values.root_module as TerraformState;
279+
280+
const rootResources: TerraformResource[] =
281+
state.values?.root_module?.resources ?? [];
282+
283+
const childResources: TerraformResource[] =
284+
state.values?.root_module?.child_modules
285+
?.map((m: any) => m.resources)
286+
.flat() ?? [];
287+
288+
return [...rootResources, ...childResources] as TerraformResource[];
258289
} catch (error: any) {
259290
//save state to file
260291
await fs.writeFile('terraform-state.json', jsonString);

test/terraform-basic.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ describe('terraform-basic', async () => {
103103
}
104104
});
105105

106+
test('call Lambda - testJsCommonJs_3', async () => {
107+
const lambdaName = await getFunctionName(
108+
folder,
109+
'lambda-test-js-commonjs_3_name',
110+
);
111+
112+
const payload = getSamplePayload(lambdaName);
113+
const response = await callLambda(lambdaName, payload);
114+
115+
expect(response.inputEvent).toEqual(payload);
116+
expect(response.runningLocally).toEqual(!observableMode);
117+
if (observableMode) {
118+
await validateLocalResponse(lambdaName, payload);
119+
}
120+
});
121+
106122
test('call Lambda - testJsEsModule', async () => {
107123
const lambdaName = await getFunctionName(
108124
folder,

test/terraform-basic/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ hello-world.zip
3636
response.json
3737

3838
dist
39+
builds
3940
.terraform.lock.hcl
4041
terraform-outputs.json

test/terraform-basic/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ resource "aws_lambda_function" "test-js-commonjs_2" {
8787
role = aws_iam_role.lambda_role.arn
8888
}
8989

90+
module "test-js-commonjs_3" {
91+
source = "terraform-aws-modules/lambda/aws"
92+
93+
function_name = "lld-terraform-basic-test-js-commonjs_3"
94+
handler = "lambda.lambdaHandler"
95+
runtime = "nodejs22.x"
96+
97+
source_path = "services/testJsCommonJs"
98+
}
99+
90100
// services/testJsEsModule/lambda.js
91101
data "archive_file" "test-js-esmodule_zip" {
92102
type = "zip"
@@ -173,6 +183,10 @@ output "lambda-test-js-commonjs_2_name" {
173183
value = aws_lambda_function.test-js-commonjs_2.function_name
174184
}
175185

186+
output "lambda-test-js-commonjs_3_name" {
187+
value = module.test-js-commonjs_3.lambda_function_name
188+
}
189+
176190
output "lambda-test-js-esmodule_name" {
177191
value = aws_lambda_function.test-js-esmodule.function_name
178192
}

0 commit comments

Comments
 (0)