@@ -6,6 +6,7 @@ import * as cognito from 'aws-cdk-lib/aws-cognito';
66import * as apigateway from 'aws-cdk-lib/aws-apigateway' ;
77import * as servicediscovery from 'aws-cdk-lib/aws-servicediscovery' ;
88import * as iam from 'aws-cdk-lib/aws-iam' ;
9+ import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2' ;
910
1011import { Construct } from 'constructs' ;
1112import { AttributeType , BillingMode , Table } from 'aws-cdk-lib/aws-dynamodb' ;
@@ -245,11 +246,39 @@ export class BackendStack extends cdk.Stack {
245246 } ) ;
246247 }
247248
248- // Create VPC Link for API Gateway (using HTTP API VPC Link)
249- const vpcLink = new apigateway . VpcLink ( this , `${ appName } VpcLink -${ props . environment } ` , {
249+ // Create a Network Load Balancer for the Fargate service
250+ const nlb = new elbv2 . NetworkLoadBalancer ( this , `${ appName } NLB -${ props . environment } ` , {
250251 vpc,
252+ internetFacing : false ,
253+ vpcSubnets : { subnetType : ec2 . SubnetType . PRIVATE_WITH_EGRESS } ,
254+ } ) ;
255+
256+ // Add a listener to the NLB
257+ const listener = nlb . addListener ( `${ appName } Listener-${ props . environment } ` , {
258+ port : 80 ,
259+ protocol : elbv2 . Protocol . TCP ,
260+ } ) ;
261+
262+ // Add the Fargate service as a target to the listener
263+ listener . addTargets ( `${ appName } TargetGroup-${ props . environment } ` , {
264+ targets : [ fargateService ] ,
265+ port : 3000 ,
266+ protocol : elbv2 . Protocol . TCP ,
267+ healthCheck : {
268+ enabled : true ,
269+ protocol : elbv2 . Protocol . HTTP ,
270+ path : '/api/health' ,
271+ interval : cdk . Duration . seconds ( 30 ) ,
272+ healthyThresholdCount : 2 ,
273+ unhealthyThresholdCount : 2 ,
274+ timeout : cdk . Duration . seconds ( 5 ) ,
275+ } ,
276+ } ) ;
277+
278+ // Create VPC Link for API Gateway using the NLB
279+ const vpcLink = new apigateway . VpcLink ( this , `${ appName } VpcLink-${ props . environment } ` , {
280+ targets : [ nlb ] ,
251281 description : `VPC Link for ${ appName } ${ props . environment } ` ,
252- vpcLinkName : `${ appName } VpcLink-${ props . environment } ` ,
253282 } ) ;
254283
255284 // Create API Gateway
@@ -279,8 +308,8 @@ export class BackendStack extends cdk.Stack {
279308 } ,
280309 ) ;
281310
282- // Get the service URL from CloudMap (now using HTTPS)
283- const serviceUrl = `https ://${ appName . toLowerCase ( ) } -service. ${ appName . toLowerCase ( ) } .local:3443 ` ;
311+ // Use the NLB DNS name for the service URL
312+ const serviceUrl = `http ://${ nlb . loadBalancerDnsName } ` ;
284313
285314 // Create proxy resource with Cognito authorization
286315 const proxyResource = api . root . addResource ( '{proxy+}' ) ;
@@ -295,10 +324,6 @@ export class BackendStack extends cdk.Stack {
295324 requestParameters : {
296325 'integration.request.path.proxy' : 'method.request.path.proxy' ,
297326 } ,
298- // Skip TLS verification for self-signed certificates in internal traffic
299- tlsConfig : {
300- insecureSkipVerification : true ,
301- } ,
302327 } ,
303328 uri : `${ serviceUrl } /{proxy}` ,
304329 } ) ;
@@ -321,10 +346,6 @@ export class BackendStack extends cdk.Stack {
321346 options : {
322347 connectionType : apigateway . ConnectionType . VPC_LINK ,
323348 vpcLink : vpcLink ,
324- // Skip TLS verification for self-signed certificates in internal traffic
325- tlsConfig : {
326- insecureSkipVerification : true ,
327- } ,
328349 } ,
329350 uri : `${ serviceUrl } /api/health` ,
330351 } ) ,
@@ -375,9 +396,9 @@ export class BackendStack extends cdk.Stack {
375396 description : 'API Gateway URL' ,
376397 } ) ;
377398
378- new cdk . CfnOutput ( this , 'ServiceDiscoveryUrl ' , {
379- value : serviceUrl ,
380- description : 'Service Discovery URL ' ,
399+ new cdk . CfnOutput ( this , 'NetworkLoadBalancerDns ' , {
400+ value : nlb . loadBalancerDnsName ,
401+ description : 'Network Load Balancer DNS Name ' ,
381402 } ) ;
382403 }
383404}
0 commit comments