Skip to content

Commit

Permalink
feat(cdk): enables customization of the API domain
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fabiob authored and sladg committed Jan 3, 2023
1 parent 688fd7f commit 2f724a8
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/cdk/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ new NextStandaloneStack(app, process.env.STACK_NAME, {
imageLambdaMemory: process.env.LAMBDA_MEMORY ? Number(process.env.IMAGE_LAMBDA_MEMORY) : IMAGE_LAMBDA_DEFAULT_MEMORY,
hostedZone: process.env.HOSTED_ZONE ?? undefined,
dnsPrefix: process.env.DNS_PREFIX ?? undefined,
customApiDomain: process.env.CUSTOM_API_DOMAIN ?? undefined,
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
Expand Down
2 changes: 2 additions & 0 deletions lib/cdk/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpApi } from '@aws-cdk/aws-apigatewayv2-alpha'
import { App, Stack } from 'aws-cdk-lib'
import { ICertificate } from 'aws-cdk-lib/aws-certificatemanager'
import { IDistribution } from 'aws-cdk-lib/aws-cloudfront'
import { HttpOrigin } from 'aws-cdk-lib/aws-cloudfront-origins'
import { Function } from 'aws-cdk-lib/aws-lambda'
import { HostedZone, IHostedZone } from 'aws-cdk-lib/aws-route53'
import { Bucket } from 'aws-cdk-lib/aws-s3'
Expand Down Expand Up @@ -76,6 +77,7 @@ export class NextStandaloneStack extends Stack {
serverBasePath: config.apigwServerPath,
domainName: config.dnsPrefix ? `${config.dnsPrefix}.${config.hostedZone}` : config.hostedZone,
certificate: this.cfnCertificate,
customApiOrigin: config.customApiDomain ? new HttpOrigin(config.customApiDomain) : undefined,
})

this.uploadStaticAssets({
Expand Down
1 change: 1 addition & 0 deletions lib/cdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export interface CustomStackProps extends StackProps {
imageLambdaMemory?: number
hostedZone?: string
dnsPrefix?: string
customApiDomain?: string
}
6 changes: 4 additions & 2 deletions lib/cdk/utils/cfnDistro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CachePolicy,
CacheQueryStringBehavior,
Distribution,
IOrigin,
PriceClass,
ViewerProtocolPolicy,
} from 'aws-cdk-lib/aws-cloudfront'
Expand All @@ -21,9 +22,10 @@ export interface SetupCfnDistroProps {
imageBasePath: string
serverBasePath: string
assetsBucket: Bucket
customApiOrigin?: IOrigin
}

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

const serverOrigin = new HttpOrigin(apiGwDomainName, { originPath: serverBasePath })
Expand Down Expand Up @@ -80,7 +82,7 @@ export const setupCfnDistro = (scope: Stack, { apiGateway, imageBasePath, server
additionalBehaviors: {
'/api*': {
...defaultOptions,
origin: serverOrigin,
origin: customApiOrigin ?? serverOrigin,
allowedMethods: AllowedMethods.ALLOW_ALL,
cachePolicy: apiCachePolicy,
},
Expand Down

0 comments on commit 2f724a8

Please sign in to comment.