Skip to content

Commit 6f073de

Browse files
invoke the lambda directly without using the API gateway
1 parent ab30f17 commit 6f073de

5 files changed

Lines changed: 39 additions & 52 deletions

File tree

infrastructure/terraform/components/api/module_lambda_supplier_mock.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ module "supplier_mock" {
3535
log_subscription_role_arn = local.acct.log_subscription_role_arn
3636

3737
lambda_env_vars = merge(local.common_lambda_env_vars, {
38-
ENVIRONMENT = var.environment
38+
ENVIRONMENT = var.environment
39+
GET_LETTERS_FUNCTION_NAME = module.get_letters.function_name
3940
})
4041
}
4142

lambdas/supplier-mock/src/deps.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,21 @@
1-
/* eslint-disable sonarjs/no-commented-code */
21
import { Logger } from "pino";
32
import { createLogger } from "@internal/helpers/src";
43
import { LambdaClient } from "@aws-sdk/client-lambda";
5-
import {
6-
APIGatewayClient,
7-
paginateGetRestApis,
8-
} from "@aws-sdk/client-api-gateway";
94
import { EnvVars, envVars } from "./env";
105

116
export type Deps = {
127
logger: Logger;
138
env: EnvVars;
149
lambdaClient: LambdaClient;
15-
apiClient: APIGatewayClient;
16-
baseUrl: string;
1710
};
1811

19-
export async function createDependenciesContainer(): Promise<Deps> {
12+
export function createDependenciesContainer(): Deps {
2013
const log = createLogger({ logLevel: envVars.PINO_LOG_LEVEL });
2114
const lambdaClient = new LambdaClient();
22-
const apiClient = new APIGatewayClient();
23-
const baseUrl = await getRestApiGatewayBaseUrl(envVars, apiClient, log);
2415

2516
return {
2617
logger: log,
2718
env: envVars,
2819
lambdaClient,
29-
apiClient,
30-
baseUrl,
3120
};
3221
}
33-
34-
async function getRestApiGatewayBaseUrl(
35-
environment: EnvVars,
36-
apiClient: APIGatewayClient,
37-
logger: Logger,
38-
): Promise<string> {
39-
logger.info(
40-
"VLASIS - about to retrieve API Gateway base URL using API client",
41-
);
42-
// const apiName = `nhs-${environment.ENVIRONMENT}-supapi`;
43-
const apiName = `nhs-pr535-supapi`;
44-
const api = await getApi(apiName, apiClient);
45-
// return `https://${api.id}.execute-api.${environment.AWS_REGION}.amazonaws.com/main`;
46-
return `https://${api.id}.execute-api.eu-west-2.amazonaws.com/main`;
47-
}
48-
49-
async function getApi(apiName: string, client: APIGatewayClient) {
50-
for await (const page of paginateGetRestApis({ client }, {})) {
51-
const filtered = page.items?.filter((api) => api.name === apiName);
52-
if (filtered?.length === 1) {
53-
return filtered[0];
54-
}
55-
}
56-
throw new Error(`API with name "${apiName}" not found.`);
57-
}

lambdas/supplier-mock/src/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const EnvVarsSchema = z.object({
44
PINO_LOG_LEVEL: z.coerce.string().optional(),
55
ENVIRONMENT: z.string().optional(),
66
AWS_REGION: z.string().optional(),
7+
GET_LETTERS_FUNCTION_NAME: z.string().optional(),
78
});
89

910
export type EnvVars = z.infer<typeof EnvVarsSchema>;

lambdas/supplier-mock/src/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,4 @@ import createHandler from "./supplier-mock";
44
const containerPromise = createDependenciesContainer();
55

66
// eslint-disable-next-line import-x/prefer-default-export
7-
export const supplierMockHandler = async (
8-
...args: Parameters<ReturnType<typeof createHandler>>
9-
) => {
10-
return createHandler(await containerPromise)(...args);
11-
};
7+
export const supplierMockHandler = createHandler(containerPromise);

lambdas/supplier-mock/src/supplier-mock.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable sonarjs/no-commented-code */
2+
import { InvokeCommand } from "@aws-sdk/client-lambda";
23
import { Deps } from "./deps";
34
import { RequestHeaders } from "../../../tests/constants/request-headers";
45

@@ -35,22 +36,46 @@ export default function createHandler(deps: Deps) {
3536
// }),
3637
// );
3738

38-
deps.logger.info(
39-
"VLASIS - about to make a request to the get letters endpoint of the supplier API",
40-
);
41-
deps.logger.info(`Base URL from deps: ${deps.baseUrl}`);
39+
deps.logger.info("Invoking get_letters lambda directly");
40+
41+
if (!deps.env.GET_LETTERS_FUNCTION_NAME) {
42+
throw new Error("GET_LETTERS_FUNCTION_NAME is not configured");
43+
}
44+
4245
const headers: RequestHeaders = {
4346
"NHSD-Supplier-ID": "TestSupplier1",
4447
"NHSD-Correlation-ID": "12345",
4548
"X-Request-ID": "requestId1",
4649
};
4750

48-
const getLettersResponse = await fetch(`${deps.baseUrl}/letters`, {
49-
method: "GET",
50-
headers,
51-
});
51+
const invokeResponse = await deps.lambdaClient.send(
52+
new InvokeCommand({
53+
FunctionName: deps.env.GET_LETTERS_FUNCTION_NAME,
54+
InvocationType: "RequestResponse",
55+
Payload: Buffer.from(
56+
JSON.stringify({
57+
headers: {
58+
"nhsd-supplier-id": headers["NHSD-Supplier-ID"],
59+
"nhsd-correlation-id": headers["NHSD-Correlation-ID"],
60+
"x-request-id": headers["X-Request-ID"],
61+
},
62+
queryStringParameters: null,
63+
requestContext: {},
64+
}),
65+
),
66+
}),
67+
);
68+
69+
const responsePayload = invokeResponse.Payload
70+
? JSON.parse(Buffer.from(invokeResponse.Payload).toString("utf8"))
71+
: undefined;
72+
5273
deps.logger.info(
53-
`Response from get letters lambda: ${getLettersResponse.status} - ${getLettersResponse.statusText}`,
74+
{
75+
statusCode: responsePayload?.statusCode,
76+
functionError: invokeResponse.FunctionError,
77+
},
78+
"Received response from get_letters lambda",
5479
);
5580
};
5681
}

0 commit comments

Comments
 (0)