-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnextjs-global-containers.ts
More file actions
142 lines (132 loc) · 5.5 KB
/
Copy pathnextjs-global-containers.ts
File metadata and controls
142 lines (132 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { Distribution } from "aws-cdk-lib/aws-cloudfront";
import { ICluster } from "aws-cdk-lib/aws-ecs";
import { IApplicationLoadBalancer } from "aws-cdk-lib/aws-elasticloadbalancingv2";
import { Construct } from "constructs";
import { NextjsType } from "../constants";
import { OptionalNextjsContainersProps } from "../generated-structs/OptionalNextjsContainersProps";
import { OptionalNextjsDistributionProps } from "../generated-structs/OptionalNextjsDistributionProps";
import { OptionalNextjsPostDeployProps } from "../generated-structs/OptionalNextjsPostDeployProps";
import {
NextjsContainers,
NextjsContainersOverrides,
} from "../nextjs-compute/nextjs-containers";
import {
NextjsDistribution,
NextjsDistributionOverrides,
} from "../nextjs-distribution";
import {
NextjsPostDeploy,
NextjsPostDeployOverrides,
} from "../nextjs-post-deploy";
import {
NextjsBaseConstructOverrides,
NextjsBaseOverrides,
NextjsBaseConstruct,
NextjsBaseProps,
} from "./nextjs-base-construct";
export interface NextjsGlobalContainersConstructOverrides extends NextjsBaseConstructOverrides {
readonly nextjsContainersProps?: OptionalNextjsContainersProps;
readonly nextjsDistributionProps?: OptionalNextjsDistributionProps;
readonly nextjsPostDeployProps?: OptionalNextjsPostDeployProps;
}
/**
* Overrides for `NextjsGlobalContainers`. Overrides are lower level than
* props and are passed directly to CDK Constructs giving you more control. It's
* recommended to use caution and review source code so you know how they're used.
*/
export interface NextjsGlobalContainersOverrides extends NextjsBaseOverrides {
readonly nextjsGlobalContainers?: NextjsGlobalContainersConstructOverrides;
readonly nextjsContainers?: NextjsContainersOverrides;
readonly nextjsDistribution?: NextjsDistributionOverrides;
readonly nextjsPostDeploy?: NextjsPostDeployOverrides;
}
export interface NextjsGlobalContainersProps extends NextjsBaseProps {
/**
* Bring your own Application Load Balancer. When provided, it is passed
* directly to `ApplicationLoadBalancedFargateService`. If the ALB already
* has a listener on port 80, call `removeAutoCreatedListener()` after
* construction to avoid deployment failures.
*/
readonly alb?: IApplicationLoadBalancer;
/**
* Bring your own distribution. Can be used with `basePath` to host multiple
* apps on the same CloudFront distribution.
*/
readonly distribution?: Distribution;
/**
* Bring your own ECS cluster. When provided, cdk-nextjs will skip creating
* a new cluster and VPC gateway endpoints.
*/
readonly ecsCluster?: ICluster;
/**
* Override props of any construct.
*/
readonly overrides?: NextjsGlobalContainersOverrides;
}
/**
* Deploy Next.js globally distributed with containers. Uses [CloudFront
* Distribution](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-working-with.html)
* as Content Delivery Network (CDN) for global distribution and [AWS Fargate](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/AWS_Fargate.html)
* for containers.
*/
export class NextjsGlobalContainers extends NextjsBaseConstruct {
nextjsContainers: NextjsContainers;
nextjsDistribution: NextjsDistribution;
nextjsPostDeploy: NextjsPostDeploy;
get url(): string {
return `https://${this.nextjsDistribution.distribution.domainName}`;
}
private props: NextjsGlobalContainersProps;
constructor(
scope: Construct,
id: string,
props: NextjsGlobalContainersProps,
) {
super(scope, id, props, NextjsType.GLOBAL_CONTAINERS);
this.props = props;
this.nextjsContainers = this.createNextjsContainers();
this.nextjsDistribution = this.createNextjsDistribution();
this.nextjsPostDeploy = this.createNextjsPostDeploy();
}
private createNextjsContainers(): NextjsContainers {
// Create containers with local build output
return new NextjsContainers(this, "NextjsContainers", {
...this.computeBaseProps(),
alb: this.props.alb,
ecsCluster: this.props.ecsCluster,
relativeEntrypointPath: this.nextjsBuild.relativePathToEntrypoint,
overrides: {
...this.props.overrides?.nextjsContainers,
ecsClusterProps: {
...this.props.overrides?.nextjsContainers?.ecsClusterProps,
vpc: this.baseProps.vpc,
},
},
...this.props.overrides?.nextjsGlobalContainers?.nextjsContainersProps,
});
}
private createNextjsDistribution() {
return new NextjsDistribution(this, "NextjsDistribution", {
assetsBucket: this.nextjsStaticAssets.bucket,
basePath: this.props.basePath,
certificate: this.nextjsContainers.albFargateService.certificate,
distribution: this.props.distribution,
loadBalancer: this.nextjsContainers.albFargateService.loadBalancer,
nextjsType: this.nextjsType,
overrides: this.props.overrides?.nextjsDistribution,
publicDirEntries: this.nextjsBuild.publicDirEntries,
...this.props.overrides?.nextjsGlobalContainers?.nextjsDistributionProps,
});
}
private createNextjsPostDeploy(): NextjsPostDeploy {
return new NextjsPostDeploy(this, "NextjsPostDeploy", {
buildId: this.nextjsBuild.buildId,
distribution: this.nextjsDistribution.distribution,
cacheBucket: this.nextjsCache.cacheBucket,
revalidationTable: this.nextjsCache.revalidationTable,
staticAssetsBucket: this.nextjsStaticAssets.bucket,
overrides: this.props.overrides?.nextjsPostDeploy,
...this.props.overrides?.nextjsGlobalContainers?.nextjsPostDeployProps,
});
}
}