Skip to content

Commit 5c1d609

Browse files
committed
Add API Gateway with ALB
1 parent 0bb9dfc commit 5c1d609

File tree

3 files changed

+51
-30
lines changed

3 files changed

+51
-30
lines changed

backend/package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
},
2929
"dependencies": {
3030
"@aws-sdk/client-dynamodb": "^3.758.0",
31-
"@aws-sdk/util-dynamodb": "^3.758.0",
3231
"@aws-sdk/client-secrets-manager": "^3.758.0",
32+
"@aws-sdk/util-dynamodb": "^3.758.0",
3333
"@nestjs/common": "^10.0.0",
3434
"@nestjs/config": "^3.1.1",
3535
"@nestjs/core": "^10.0.0",
3636
"@nestjs/jwt": "^10.2.0",
37-
"@nestjs/platform-express": "^10.0.0",
3837
"@nestjs/passport": "^10.0.3",
38+
"@nestjs/platform-express": "^10.0.0",
39+
"@nestjs/swagger": "^7.1.13",
3940
"@types/jest": "^29.5.12",
4041
"axios": "^1.8.1",
4142
"class-transformer": "^0.5.1",
@@ -47,15 +48,14 @@
4748
"helmet": "^7.0.0",
4849
"jsonwebtoken": "^9.0.2",
4950
"jwk-to-pem": "^2.0.5",
51+
"passport": "^0.7.0",
52+
"passport-jwt": "^4.0.1",
5053
"reflect-metadata": "^0.1.13",
5154
"rxjs": "^7.8.1",
5255
"source-map-support": "^0.5.21",
53-
"web-vitals": "^2.1.4",
54-
"aws-cdk-lib": "2.184.1",
55-
"@nestjs/swagger": "^7.1.13",
5656
"swagger-ui-express": "^5.0.0",
57-
"passport": "^0.7.0",
58-
"passport-jwt": "^4.0.1"
57+
"web-vitals": "^2.1.4",
58+
"aws-cdk-lib": "^2.185.0"
5959
},
6060
"devDependencies": {
6161
"@aws-cdk/assert": "^2.68.0",
@@ -73,7 +73,7 @@
7373
"@typescript-eslint/parser": "^7.9.0",
7474
"@vitest/coverage-c8": "^0.33.0",
7575
"aws-cdk": "2.139.0",
76-
"aws-cdk-lib": "^2.184.1",
76+
"aws-cdk-lib": "^2.185.0",
7777
"dotenv-cli": "^8.0.0",
7878
"eslint": "^8.57.0",
7979
"eslint-config-prettier": "^9.0.0",
@@ -91,6 +91,6 @@
9191
"vitest": "^0.33.0"
9292
},
9393
"peerDependencies": {
94-
"aws-cdk-lib": "2.184.1"
94+
"aws-cdk-lib": "^2.185.0"
9595
}
9696
}

backend/src/iac/backend-stack.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as cognito from 'aws-cdk-lib/aws-cognito';
66
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
77
import * as servicediscovery from 'aws-cdk-lib/aws-servicediscovery';
88
import * as iam from 'aws-cdk-lib/aws-iam';
9+
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
910

1011
import { Construct } from 'constructs';
1112
import { 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

Comments
 (0)