-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.ts
172 lines (164 loc) · 5.02 KB
/
app.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { IAspect, Stack, StackProps } from "aws-cdk-lib";
import { Construct, IConstruct } from "constructs";
import { NextjsGlobalFunctions } from "cdk-nextjs";
import { fileURLToPath } from "node:url";
import { App, Aspects } from "aws-cdk-lib";
import { AwsSolutionsChecks, NagSuppressions } from "cdk-nag";
import {
suppressCommonNags,
suppressGlobalNags,
suppressLambdaNags,
} from "../shared/suppress-nags";
import {
InstanceClass,
InstanceSize,
InstanceType,
Vpc,
} from "aws-cdk-lib/aws-ec2";
import { FckNatInstanceProvider } from "cdk-fck-nat";
import { PriceClass } from "aws-cdk-lib/aws-cloudfront";
import { CfnLogGroup, RetentionDays } from "aws-cdk-lib/aws-logs";
import {
Certificate,
CertificateValidation,
} from "aws-cdk-lib/aws-certificatemanager";
import {
AaaaRecord,
ARecord,
HostedZone,
IHostedZone,
RecordTarget,
} from "aws-cdk-lib/aws-route53";
import { CloudFrontTarget } from "aws-cdk-lib/aws-route53-targets";
const app = new App();
/**
* Example demonstrates how to deploy cdk-nextjs with low cost. In order to
* achieve lower cost, we sacrafice security. This is not recommended by AWS
* but we understand low cost may be more important to customers for certain
* use cases such as personal blogs. This example also demonstrates how to
* setup DNS.
*
* Low Cost Feature Summary:
* - NAT Instance instead of NAT Gateway
* - Lowest CloudFront Distribution Price Class
* - Logs expire after 1 month to reduce CloudWatch costs
*/
class LowCostStack extends Stack {
#hostedZoneDomainName = "example.com";
#distributionDomainName = `blog.${this.#hostedZoneDomainName}`;
constructor(scope: Construct, id: string, props: StackProps) {
super(scope, id, props);
const vpc = this.#createVpc();
const hostedZone = this.#getHostedZone();
const certificate = this.#getCertificate(hostedZone);
const nextjs = new NextjsGlobalFunctions(this, "Nextjs", {
healthCheckPath: "/api/health",
buildContext: fileURLToPath(new URL("..", import.meta.url)),
overrides: {
nextjsDistribution: {
distributionProps: {
certificate,
domainNames: [this.#distributionDomainName],
priceClass: PriceClass.PRICE_CLASS_100,
},
},
nextjsGlobalFunctions: {
nextjsBuildProps: {
builderImageProps: {
exclude: [
"**/node_modules",
"**/.next",
"global-containers",
"global-functions",
"regional-containers",
"shared",
"*.md",
],
},
},
nextjsVpcProps: {
vpc,
},
},
},
relativePathToWorkspace: "./app-playground",
});
this.#createDnsRecords(nextjs, hostedZone);
}
#getHostedZone() {
return HostedZone.fromLookup(this, "HostedZone", {
domainName: this.#hostedZoneDomainName,
});
}
#getCertificate(hostedZone: IHostedZone) {
return new Certificate(this, "Certificate", {
domainName: this.#distributionDomainName,
validation: CertificateValidation.fromDns(hostedZone),
});
}
#createVpc() {
const natGatewayProvider = new FckNatInstanceProvider({
instanceType: InstanceType.of(InstanceClass.T4G, InstanceSize.NANO),
});
const vpcName = this.stackName + "FckNatVpc";
const vpc = new Vpc(this, vpcName, {
natGatewayProvider,
vpcName,
});
return vpc;
}
#createDnsRecords(nextjs: NextjsGlobalFunctions, hostedZone: IHostedZone) {
new ARecord(this, "ARecord", {
recordName: this.#distributionDomainName,
target: RecordTarget.fromAlias(
new CloudFrontTarget(nextjs.nextjsDistribution.distribution),
),
zone: hostedZone,
});
new AaaaRecord(this, "AAAARecord", {
recordName: this.#distributionDomainName,
target: RecordTarget.fromAlias(
new CloudFrontTarget(nextjs.nextjsDistribution.distribution),
),
zone: hostedZone,
});
}
}
export const stack = new LowCostStack(app, "low-cost", {
env: {
account: process.env["CDK_DEFAULT_ACCOUNT"],
region: process.env["CDK_DEFAULT_REGION"],
},
});
suppressCommonNags(stack);
suppressGlobalNags(stack);
suppressLambdaNags(stack);
NagSuppressions.addResourceSuppressionsByPath(
stack,
`/${stack.stackName}/Nextjs/NextjsRevalidation/Queue/Resource`,
[
{
id: "AwsSolutions-SQS3",
reason: "DLQ not required for example app",
},
],
);
NagSuppressions.addResourceSuppressionsByPath(
stack,
`/${stack.stackName}/Nextjs/NextjsRevalidation/Fn/ServiceRole/Resource`,
[
{
id: "AwsSolutions-IAM4",
reason: "AWSLambdaBasicExecutionRole is not overly permissive",
},
],
);
Aspects.of(app).add(new AwsSolutionsChecks({ verbose: true }));
class LogGroupRetentionAspect implements IAspect {
public visit(node: IConstruct): void {
if (node instanceof CfnLogGroup) {
node.retentionInDays = RetentionDays.ONE_MONTH;
}
}
}
Aspects.of(app).add(new LogGroupRetentionAspect());