Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit 2f724a8

Browse files
fabiobsladg
authored andcommitted
feat(cdk): enables customization of the API domain
While the Next.js rewrite support works nicely, it increases the total cost of the deployment since the server API will be charged while the real backend does its work. This gets worse when the backend is also deployed as a lambda function on the same account, as they will consume two concurrent execution quotas.
1 parent 688fd7f commit 2f724a8

File tree

4 files changed

+8
-2
lines changed

4 files changed

+8
-2
lines changed

Diff for: lib/cdk/app.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ new NextStandaloneStack(app, process.env.STACK_NAME, {
3838
imageLambdaMemory: process.env.LAMBDA_MEMORY ? Number(process.env.IMAGE_LAMBDA_MEMORY) : IMAGE_LAMBDA_DEFAULT_MEMORY,
3939
hostedZone: process.env.HOSTED_ZONE ?? undefined,
4040
dnsPrefix: process.env.DNS_PREFIX ?? undefined,
41+
customApiDomain: process.env.CUSTOM_API_DOMAIN ?? undefined,
4142
env: {
4243
account: process.env.CDK_DEFAULT_ACCOUNT,
4344
region: process.env.CDK_DEFAULT_REGION,

Diff for: lib/cdk/stack.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { HttpApi } from '@aws-cdk/aws-apigatewayv2-alpha'
22
import { App, Stack } from 'aws-cdk-lib'
33
import { ICertificate } from 'aws-cdk-lib/aws-certificatemanager'
44
import { IDistribution } from 'aws-cdk-lib/aws-cloudfront'
5+
import { HttpOrigin } from 'aws-cdk-lib/aws-cloudfront-origins'
56
import { Function } from 'aws-cdk-lib/aws-lambda'
67
import { HostedZone, IHostedZone } from 'aws-cdk-lib/aws-route53'
78
import { Bucket } from 'aws-cdk-lib/aws-s3'
@@ -76,6 +77,7 @@ export class NextStandaloneStack extends Stack {
7677
serverBasePath: config.apigwServerPath,
7778
domainName: config.dnsPrefix ? `${config.dnsPrefix}.${config.hostedZone}` : config.hostedZone,
7879
certificate: this.cfnCertificate,
80+
customApiOrigin: config.customApiDomain ? new HttpOrigin(config.customApiDomain) : undefined,
7981
})
8082

8183
this.uploadStaticAssets({

Diff for: lib/cdk/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ export interface CustomStackProps extends StackProps {
1717
imageLambdaMemory?: number
1818
hostedZone?: string
1919
dnsPrefix?: string
20+
customApiDomain?: string
2021
}

Diff for: lib/cdk/utils/cfnDistro.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
CachePolicy,
99
CacheQueryStringBehavior,
1010
Distribution,
11+
IOrigin,
1112
PriceClass,
1213
ViewerProtocolPolicy,
1314
} from 'aws-cdk-lib/aws-cloudfront'
@@ -21,9 +22,10 @@ export interface SetupCfnDistroProps {
2122
imageBasePath: string
2223
serverBasePath: string
2324
assetsBucket: Bucket
25+
customApiOrigin?: IOrigin
2426
}
2527

26-
export const setupCfnDistro = (scope: Stack, { apiGateway, imageBasePath, serverBasePath, assetsBucket, domainName, certificate }: SetupCfnDistroProps) => {
28+
export const setupCfnDistro = (scope: Stack, { apiGateway, imageBasePath, serverBasePath, assetsBucket, domainName, certificate, customApiOrigin }: SetupCfnDistroProps) => {
2729
const apiGwDomainName = `${apiGateway.apiId}.execute-api.${scope.region}.amazonaws.com`
2830

2931
const serverOrigin = new HttpOrigin(apiGwDomainName, { originPath: serverBasePath })
@@ -80,7 +82,7 @@ export const setupCfnDistro = (scope: Stack, { apiGateway, imageBasePath, server
8082
additionalBehaviors: {
8183
'/api*': {
8284
...defaultOptions,
83-
origin: serverOrigin,
85+
origin: customApiOrigin ?? serverOrigin,
8486
allowedMethods: AllowedMethods.ALLOW_ALL,
8587
cachePolicy: apiCachePolicy,
8688
},

0 commit comments

Comments
 (0)