Skip to content

Commit

Permalink
add CORS config to api gw
Browse files Browse the repository at this point in the history
  • Loading branch information
fredex42 committed Mar 18, 2024
1 parent 8799751 commit 9211648
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions cdk/lib/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Construct} from "constructs";
import type {GuStack, GuStackProps} from "@guardian/cdk/lib/constructs/core";
import {HttpApi, VpcLink} from "aws-cdk-lib/aws-apigatewayv2"
import {HttpAlbIntegration, HttpUrlIntegration} from "aws-cdk-lib/aws-apigatewayv2-integrations";
import {IApplicationLoadBalancer, IListener} from "aws-cdk-lib/aws-elasticloadbalancingv2";
import {ISecurityGroup, IVpc, Peer, Port, SecurityGroup, SubnetSelection} from "aws-cdk-lib/aws-ec2";
import type {GuStack} from "@guardian/cdk/lib/constructs/core";
import {CorsHttpMethod, HttpApi, VpcLink} from "aws-cdk-lib/aws-apigatewayv2"
import {HttpAlbIntegration} from "aws-cdk-lib/aws-apigatewayv2-integrations";
import {IApplicationLoadBalancer} from "aws-cdk-lib/aws-elasticloadbalancingv2";
import {ISecurityGroup, IVpc, Peer, Port, SubnetSelection} from "aws-cdk-lib/aws-ec2";
import {GuSecurityGroup} from "@guardian/cdk/lib/constructs/ec2";
import {CfnUsagePlan, UsagePlan} from "aws-cdk-lib/aws-apigateway";
import {IApplicationListener} from "aws-cdk-lib/aws-elasticloadbalancingv2/lib/alb/application-listener";
import {Duration} from "aws-cdk-lib";

interface HttpGatewayProps {
stage: "CODE"|"PROD";
Expand All @@ -15,6 +15,7 @@ interface HttpGatewayProps {
backendListener: IApplicationListener;
backendLbIncomingSg: ISecurityGroup;
lbDomainName: string;
deployedUrl: string;
subnets: SubnetSelection;
vpc: IVpc;
}
Expand Down Expand Up @@ -50,6 +51,13 @@ export class HttpGateway extends Construct {
vpcLink,
secureServerName: props.lbDomainName,
}),
corsPreflight: {
allowOrigins: ['http://localhost:8081', props.deployedUrl],
allowMethods: [CorsHttpMethod.POST, CorsHttpMethod.GET, CorsHttpMethod.OPTIONS],
allowHeaders: ['content-type', 'x-api-key'],
maxAge: Duration.minutes(5),
allowCredentials: true
},
createDefaultStage: true,
});
//
Expand Down

0 comments on commit 9211648

Please sign in to comment.