Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(type-safe-api): use lambda region in both parts of function integration uri #595

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});
Loading