import { CfnOutput , CfnResource , Construct , Stack , StackProps } from '@aws-cdk/core' ; import { Vpc } from '@aws-cdk/aws-ec2' ; import { Cluster , ContainerImage } from '@aws-cdk/aws-ecs' ; import { ApplicationLoadBalancedFargateService } from '@aws-cdk/aws-ecs-patterns' ; import { CfnIntegration , CfnRoute , HttpApi } from '@aws-cdk/aws-apigatewayv2' ; import path = require ( 'path' ) ; export class CdkStack extends Stack { constructor ( scope , id , props ? ) { super ( scope , id , props ) ; const vpc = new Vpc ( this , 'MyVpc' , { maxAzs : 3 } ) ; const cluster = new Cluster ( this , 'MyCluster' , { vpc : vpc } ) ; const fargate = new ApplicationLoadBalancedFargateService ( this , 'MyFargateService' , { assignPublicIp : false , cluster : cluster , cpu : 512 , desiredCount : 1 , memoryLimitMiB : 2048 , publicLoadBalancer : false , taskImageOptions : { image : ContainerImage . fromAsset ( path . join ( __dirname , '../src/' ) ) , } , } ) ; const httpVpcLink = new CfnResource ( this , 'HttpVpcLink' , { type : 'AWS::ApiGatewayV2::VpcLink' , properties : { Name : 'V2 VPC Link' , SubnetIds : vpc . privateSubnets . map ( m => m . subnetId ) } } ) ; const api = new HttpApi ( this , 'HttpApiGateway' , { apiName : 'ApigwFargate' , description : 'Integration between apigw and Application Load-Balanced Fargate Service' , } ) ; const integration = new CfnIntegration ( this , 'HttpApiGatewayIntegration' , { apiId : api . httpApiId , connectionId : httpVpcLink . ref , connectionType : 'VPC_LINK' , description : 'API Integration with AWS Fargate Service' , integrationMethod : 'GET' , integrationType : 'HTTP_PROXY' , integrationUri : fargate . listener . listenerArn , payloadFormatVersion : '1.0' , } ) ; new CfnRoute ( this , 'Route' , { apiId : api . httpApiId , routeKey : 'GET /' , target : `integrations/${ integration . ref }` , } ) ; new CfnOutput ( this , 'APIGatewayUrl' , { description : 'API Gateway URL to access the GET endpoint' , value : api . url ! } ) ; } }