Skip to content

Instantly share code, notes, and snippets.

@Gershon-A
Last active May 30, 2023 13:12
Show Gist options
  • Save Gershon-A/9ea44eb13e1986a962ae0f9109f0e32f to your computer and use it in GitHub Desktop.
Save Gershon-A/9ea44eb13e1986a962ae0f9109f0e32f to your computer and use it in GitHub Desktop.
REST API using Amazon API Gateway with a VPC Link integration API Gateway and a DomainName record set group using AWS Route53.

This is a CloudFormation template written in YAML format. It creates a REST API using Amazon API Gateway with a VPC Link integration. It also creates an endpoint for the API Gateway and a DomainName record set group using AWS Route53.

The parameters section allows the user to specify the environment to be deployed (prod, stage, or dev), the NLB Domain Name, the API Gateway domain, the type of Amazon API Gateway domain name, and the hosted zone ID of the given domain name.

The resources section creates the REST API endpoint, HTTP methods for the REST API, a VPC Link that integrates with the NLB Internal Arn, a DomainName record set group, and a mapping between the API Gateway and stage.

The outputs section lists the API Gateway endpoint to be used during tests.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Serverless patterns - Amazon API Gateway REST API with VPC Link integration
Parameters:
usecase:
Description: What environment should be deployed
Type: String
AllowedValues:
- "prod"
- "stage"
- "dev"
Default: prod
NlbInternalArn:
Type: String
Default:
NlbDomainName:
Description: NLB Domain Name
Type: String
Default:
GatewayDomain:
Description: ApiGateway domain
Type: String
Default:
DomainConfiguration:
Description: endpoint types of an Amazon API Gateway domain name
Type: String
Default: REGIONAL
HostedZoneId:
Default:
Type: String
Description: hosted zone ID for given domain name
Resources:
# REST API
AppApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: kong-apigw-rest-api-vpclink
Description: VPC Link integraton REST API demo
RootMethod:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref AppApi
ResourceId: !GetAtt AppApi.RootResourceId
HttpMethod: ANY
AuthorizationType: NONE
RequestParameters:
method.request.path.proxy: true
Integration:
CacheKeyParameters:
- 'method.request.path.proxy'
Type: HTTP_PROXY
ConnectionType: VPC_LINK
IntegrationHttpMethod: ANY
ConnectionId: !Ref VPCLinkRestNlbInternal
Uri: !Sub https://${NlbDomainName}
PassthroughBehavior: WHEN_NO_MATCH
RootMethodProxy:
DependsOn:
- RootMethod
Type: 'AWS::ApiGateway::Resource'
Properties:
ParentId: !GetAtt AppApi.RootResourceId
RestApiId: !Ref AppApi
PathPart: '{proxy+}'
ProxyMethod:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref AppApi
ResourceId: !Ref RootMethodProxy
HttpMethod: ANY
AuthorizationType: NONE
RequestParameters:
method.request.path.proxy: true
Integration:
CacheKeyParameters:
- 'method.request.path.proxy'
RequestParameters:
integration.request.path.proxy: 'method.request.path.proxy' # 'request' should be 'integration.request'
Type: HTTP_PROXY
ConnectionType: VPC_LINK
IntegrationHttpMethod: ANY
ConnectionId: !Ref VPCLinkRestNlbInternal
Uri: !Sub 'https://${NlbDomainName}/{proxy}' # Added single quotes around URI to avoid error in '&' character
PassthroughBehavior: WHEN_NO_MATCH
Deployment:
Type: AWS::ApiGateway::Deployment
DependsOn:
- RootMethodProxy
- ProxyMethod
Properties:
RestApiId: !Ref AppApi
Stage:
Type: AWS::ApiGateway::Stage
Properties:
StageName: Prod
RestApiId: !Ref AppApi
DeploymentId: !Ref Deployment
VPCLinkRestNlbInternal:
Type: AWS::ApiGateway::VpcLink
Properties:
Name: VPCLinkRestNlbInternal
TargetArns:
- !Ref NlbInternalArn
GatewayDomainName:
DependsOn: AppApi
Type: 'AWS::ApiGateway::DomainName'
Properties:
DomainName: !Ref GatewayDomain
# CertificateArn: !Sub "{{resolve:ssm:/${usecase}/infrastructure-ttm4j/certificate-arn:1}}"
EndpointConfiguration:
Types:
- !Ref DomainConfiguration
RegionalCertificateArn: !Sub "{{resolve:ssm:/${usecase}/infrastructure-ttm4j/certificate-arn:1}}"
GatewayDomainRecordSetGroup:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneId: !Ref HostedZoneId
RecordSets:
- Name: !Ref GatewayDomain
Type: A
AliasTarget:
DNSName: !GetAtt GatewayDomainName.RegionalDomainName
HostedZoneId: !GetAtt GatewayDomainName.RegionalHostedZoneId
Mapping:
Type: 'AWS::ApiGateway::BasePathMapping'
Properties:
DomainName: !Ref GatewayDomain
RestApiId: !Ref AppApi
Stage: !Ref Stage
Outputs:
# API Gateway endpoint to be used during tests
AppApiEndpoint:
Description: API Endpoint
Value: !Sub "https://${AppApi}.execute-api.${AWS::Region}.amazonaws.com/Prod"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment