Skip to content

Commit

Permalink
fix(type-safe-api): use lambda region in both parts of function integ…
Browse files Browse the repository at this point in the history
…ration uri

The region in the first part of a function invocation uri is also the region of the lambda function
itself, rather than the api gateway rest api

Fixes #594
  • Loading branch information
cogwirrel committed Oct 12, 2023
1 parent 917a5db commit a70c20d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/type-safe-api/src/construct/integrations/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export class LambdaIntegration extends Integration {
/**
* Render the lambda integration as a snippet of OpenAPI
*/
public render(props: IntegrationRenderProps): ApiGatewayIntegration {
public render(_props: IntegrationRenderProps): ApiGatewayIntegration {
return {
type: "AWS_PROXY",
httpMethod: "POST",
uri: functionInvocationUri(props.scope, this.lambdaFunction),
uri: functionInvocationUri(this.lambdaFunction),
passthroughBehavior: "WHEN_NO_MATCH",
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/type-safe-api/src/construct/spec/api-gateway-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const cognitoSecurityScheme = (
* @param authorizer custom authorizer
*/
const customSecurityScheme = (
scope: Construct,
_scope: Construct,
authorizer: CustomAuthorizer
): CustomSecurityScheme => {
const singleHeaderMatch = authorizer.identitySource.match(
Expand All @@ -205,7 +205,7 @@ const customSecurityScheme = (
"x-amazon-apigateway-authtype": authorizer.authorizationType,
"x-amazon-apigateway-authorizer": {
type: authorizer.type,
authorizerUri: functionInvocationUri(scope, authorizer.function),
authorizerUri: functionInvocationUri(authorizer.function),
authorizerResultTtlInSeconds: authorizer.authorizerResultTtlInSeconds,
identitySource: authorizer.identitySource,
},
Expand Down
9 changes: 2 additions & 7 deletions packages/type-safe-api/src/construct/spec/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
SPDX-License-Identifier: Apache-2.0 */
import { Stack } from "aws-cdk-lib";
import { IFunction } from "aws-cdk-lib/aws-lambda";
import { Construct } from "constructs";

/**
* Generate the lambda function invocation uri for the given lambda within the given scope
* @param scope scope in which the lambda is deployed
* @param lambdaFunction the lambda function to be invoked
*/
export const functionInvocationUri = (
scope: Construct,
lambdaFunction: IFunction
): string => {
const stack = Stack.of(scope);
export const functionInvocationUri = (lambdaFunction: IFunction): string => {
const stack = Stack.of(lambdaFunction);
return `arn:${stack.partition}:apigateway:${stack.region}:lambda:path/2015-03-31/functions/${lambdaFunction.functionArn}/invocations`;
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion packages/type-safe-api/test/construct/type-safe-rest-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SPDX-License-Identifier: Apache-2.0 */
import * as fs from "fs";
import * as path from "path";
import { PDKNag } from "@aws/pdk-nag";
import { Size, Stack } from "aws-cdk-lib";
import { App, Size, Stack } from "aws-cdk-lib";
import { Template } from "aws-cdk-lib/assertions";
import { ApiKeySourceType, Cors } from "aws-cdk-lib/aws-apigateway";
import { UserPool } from "aws-cdk-lib/aws-cognito";
Expand Down Expand Up @@ -1234,4 +1234,32 @@ describe("Type Safe Rest Api Construct Unit Tests", () => {
snapshotExtendedSpec(api);
});
});

it("Should allow for lambdas in different regions", () => {
const app = new App();
const usStack = new Stack(app, "USStack", {
env: {
region: "us-east-1",
},
});
const auStack = new Stack(app, "AUStack", {
env: {
region: "ap-southeast-2",
},
});
const func = Function.fromFunctionName(usStack, "Func", "Test");
withTempSpec(sampleSpec, (specPath) => {
const api = new TypeSafeRestApi(auStack, "ApiTest", {
specPath,
operationLookup,
integrations: {
testOperation: {
integration: Integrations.lambda(func),
},
},
});

snapshotExtendedSpec(api);
});
});
});

0 comments on commit a70c20d

Please sign in to comment.