Skip to content

Commit

Permalink
Merge pull request #16 from MarcioMeier/fix/api-gateway-vpce-ids
Browse files Browse the repository at this point in the history
fix: api gateway vpc endpoint ids
  • Loading branch information
MarcioMeier authored May 8, 2024
2 parents 9057700 + c283c9f commit a3fbfc9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
12 changes: 10 additions & 2 deletions lib/src/apigateway/openapi-gateway-lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('openapi-gateway-lambda', () => {
],
"Condition": {
"StringEquals": {
"aws:SourceVpce": [
"aws:sourceVpce": [
"vpce-123123"
]
}
Expand All @@ -199,6 +199,14 @@ describe('openapi-gateway-lambda', () => {
}`,
);

// Make sure that the workaround adds the endpoint configuration to the rest api construct
template.hasResourceProperties('AWS::ApiGateway::RestApi', {
EndpointConfiguration: {
Types: ['PRIVATE'],
VpcEndpointIds: ['vpce-123123'],
},
});

template.hasResourceProperties('AWS::Logs::LogGroup', {
LogGroupName: 'apigateway-accesslogs-myapi',
RetentionInDays: 30,
Expand Down Expand Up @@ -238,7 +246,7 @@ describe('openapi-gateway-lambda', () => {
});
expect(
openapiDoc31WithVPCE['x-amazon-apigateway-policy'].Statement[0].Condition.StringEquals[
'aws:SourceVpce'
'aws:sourceVpce'
],
).toStrictEqual(originProps.vpcEndpointIds);
// output includes fields from input
Expand Down
21 changes: 15 additions & 6 deletions lib/src/apigateway/openapi-gateway-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SpecRestApi,
SpecRestApiProps,
StageOptions,
CfnRestApi,
} from 'aws-cdk-lib/aws-apigateway';
import type { oas30, oas31 } from 'openapi3-ts';
import { Construct } from 'constructs';
Expand Down Expand Up @@ -82,6 +83,17 @@ export class OpenApiGatewayLambda extends Construct {
deployOptions,
});

// Workaround to link the vpc endpoint ids top the api gateway > api settings
// This should be removed when this issue is fixed
// https://github.com/aws/aws-cdk/issues/9684
const hasPrivate = props.endpointTypes?.some((value) => value === EndpointType.PRIVATE);
if (hasPrivate) {
(specRestApi.node.defaultChild as CfnRestApi).endpointConfiguration = {
types: propsWithDefaults.endpointTypes ?? [EndpointType.PRIVATE],
vpcEndpointIds: propsWithDefaults.vpcEndpointIds,
};
}

this.specRestApi = specRestApi;
this.openapiDocument = openapiDoc30;
this.logGroupAccessLog = logGroupAccessLog;
Expand Down Expand Up @@ -143,10 +155,7 @@ export const addVPCEndpointConfig = (
props: OpenApiGatewayLambdaProps,
openapiDoc31: oas31.OpenAPIObject,
): { openapiDoc31WithVPCE: oas31.OpenAPIObject } => {
const hasPrivate = props.endpointTypes?.reduce(
(cur, value) => cur || value === EndpointType.PRIVATE,
false,
);
const hasPrivate = props.endpointTypes?.some((value) => value === EndpointType.PRIVATE);
if (!hasPrivate) {
return { openapiDoc31WithVPCE: openapiDoc31 };
}
Expand Down Expand Up @@ -175,7 +184,7 @@ export const addVPCEndpointConfig = (
Resource: ['execute-api:/*'],
Condition: {
StringEquals: {
'aws:SourceVpce': props.vpcEndpointIds,
'aws:sourceVpce': props.vpcEndpointIds,
},
},
},
Expand Down Expand Up @@ -279,7 +288,7 @@ export const getPropsWithDefaults = (
return {
...props,

// opiniated default props
// opinionated default props
minCompressionSize: props.minCompressionSize ?? Size.bytes(200000),
accessLogRetention: props.accessLogRetention ?? RetentionDays.SIX_MONTHS,
deploy: props.deploy ?? true,
Expand Down

0 comments on commit a3fbfc9

Please sign in to comment.