@@ -15,7 +15,8 @@ import {
15
15
aws_route53_targets as targets ,
16
16
aws_wafv2 as wafv2 ,
17
17
} from 'aws-cdk-lib'
18
- import { app } from '../../config/src'
18
+ import { projectPath } from '@stacksjs/path'
19
+ import { app } from '@stacksjs/config'
19
20
20
21
export class StacksCloud extends Stack {
21
22
constructor ( scope : Construct , id : string , props ?: StackProps ) {
@@ -35,11 +36,6 @@ export class StacksCloud extends Stack {
35
36
validation : acm . CertificateValidation . fromDns ( zone ) ,
36
37
} )
37
38
38
- const docsCertificate = new acm . Certificate ( this , 'DocsCertificate' , {
39
- domainName : `${ app . subdomains . docs } .${ domainName } ` ,
40
- validation : acm . CertificateValidation . fromDns ( zone ) ,
41
- } )
42
-
43
39
const publicBucket = new s3 . Bucket ( this , 'PublicBucket' , {
44
40
bucketName : `${ domainName } -${ app . env } ` ,
45
41
versioned : true ,
@@ -54,27 +50,6 @@ export class StacksCloud extends Stack {
54
50
autoDeleteObjects : true ,
55
51
} )
56
52
57
- const docsBucket = new s3 . Bucket ( this , 'DocsBucket' , {
58
- bucketName : `docs.${ domainName } -${ app . env } ` ,
59
- versioned : true ,
60
- removalPolicy : RemovalPolicy . DESTROY ,
61
- autoDeleteObjects : true ,
62
- } )
63
-
64
- // Create an S3 bucket for CloudFront access logs
65
- const logBucket = new s3 . Bucket ( this , 'LogBucket' , {
66
- bucketName : `${ domainName } -logs-${ app . env } ` ,
67
- removalPolicy : RemovalPolicy . DESTROY ,
68
- autoDeleteObjects : true ,
69
- objectOwnership : s3 . ObjectOwnership . BUCKET_OWNER_PREFERRED ,
70
- } )
71
-
72
- logBucket . addLifecycleRule ( {
73
- enabled : true ,
74
- expiration : Duration . days ( 30 ) ,
75
- id : 'rule' ,
76
- } )
77
-
78
53
// Create WAF WebAcl
79
54
const webAcl = new wafv2 . CfnWebACL ( this , 'WebAcl' , {
80
55
scope : 'CLOUDFRONT' ,
@@ -132,62 +107,13 @@ export class StacksCloud extends Stack {
132
107
// ],
133
108
} )
134
109
135
- const docsDistribution = new cloudfront . Distribution ( this , 'DocsDistribution' , {
136
- domainNames : [ `${ app . subdomains . docs } .${ app . url } ` ] ,
137
- defaultRootObject : 'index.html' ,
138
- comment : `CDN for ${ app . subdomains . docs } .${ app . url } ` ,
139
- certificate : docsCertificate ,
140
- // originShieldEnabled: true,
141
- enableLogging : true ,
142
- logBucket,
143
- httpVersion : cloudfront . HttpVersion . HTTP2_AND_3 ,
144
- priceClass : cloudfront . PriceClass . PRICE_CLASS_ALL ,
145
- enabled : true ,
146
- minimumProtocolVersion : cloudfront . SecurityPolicyProtocol . TLS_V1_2_2021 ,
147
- webAclId : webAcl . attrArn ,
148
- enableIpv6 : true ,
149
-
150
- defaultBehavior : {
151
- origin : new origins . S3Origin ( docsBucket , {
152
- originAccessIdentity,
153
- } ) ,
154
- compress : true ,
155
- allowedMethods : cloudfront . AllowedMethods . ALLOW_GET_HEAD ,
156
- cachedMethods : cloudfront . CachedMethods . CACHE_GET_HEAD ,
157
- viewerProtocolPolicy : cloudfront . ViewerProtocolPolicy . REDIRECT_TO_HTTPS ,
158
- cachePolicy : cloudfront . CachePolicy . CACHING_OPTIMIZED ,
159
- } ,
160
-
161
- // errorResponses: [
162
- // {
163
- // httpStatus: 403,
164
- // responsePagePath: '/index.html',
165
- // responseHttpStatus: 200,
166
- // ttl: cdk.Duration.minutes(0),
167
- // },
168
- // {
169
- // httpStatus: 404,
170
- // responsePagePath: '/index.html',
171
- // responseHttpStatus: 200,
172
- // ttl: cdk.Duration.minutes(0),
173
- // },
174
- // ],
175
- } )
176
-
177
110
// Create a Route53 record pointing to the CloudFront distribution
178
111
new route53 . ARecord ( this , 'AliasRecord' , {
179
112
recordName : domainName ,
180
113
zone,
181
114
target : route53 . RecordTarget . fromAlias ( new targets . CloudFrontTarget ( distribution ) ) ,
182
115
} )
183
116
184
- // Create a Route53 record pointing to the Docs CloudFront distribution
185
- new route53 . ARecord ( this , 'DocsAliasRecord' , {
186
- recordName : `${ app . subdomains . docs } .${ domainName } ` ,
187
- zone,
188
- target : route53 . RecordTarget . fromAlias ( new targets . CloudFrontTarget ( docsDistribution ) ) ,
189
- } )
190
-
191
117
new route53 . CnameRecord ( this , 'WwwCnameRecord' , {
192
118
zone,
193
119
recordName : 'www' ,
@@ -210,25 +136,105 @@ export class StacksCloud extends Stack {
210
136
destinationBucket : privateBucket ,
211
137
} )
212
138
213
- new s3deploy . BucketDeployment ( this , 'DeployDocs' , {
214
- sources : [ s3deploy . Source . asset ( docsSource ) ] ,
215
- destinationBucket : docsBucket ,
216
- distribution : docsDistribution ,
217
- distributionPaths : [ '/*' ] ,
218
- } )
219
-
139
+ if ( shouldDeployDocs ( ) ) {
140
+ const docsCertificate = new acm . Certificate ( this , 'DocsCertificate' , {
141
+ domainName : `${ app . subdomains . docs } .${ domainName } ` ,
142
+ validation : acm . CertificateValidation . fromDns ( zone ) ,
143
+ } )
144
+
145
+ const docsBucket = new s3 . Bucket ( this , 'DocsBucket' , {
146
+ bucketName : `docs.${ domainName } -${ app . env } ` ,
147
+ versioned : true ,
148
+ removalPolicy : RemovalPolicy . DESTROY ,
149
+ autoDeleteObjects : true ,
150
+ } )
151
+
152
+ // Create an S3 bucket for CloudFront access logs
153
+ const logBucket = new s3 . Bucket ( this , 'LogBucket' , {
154
+ bucketName : `${ domainName } -logs-${ app . env } ` ,
155
+ removalPolicy : RemovalPolicy . DESTROY ,
156
+ autoDeleteObjects : true ,
157
+ objectOwnership : s3 . ObjectOwnership . BUCKET_OWNER_PREFERRED ,
158
+ } )
159
+
160
+ logBucket . addLifecycleRule ( {
161
+ enabled : true ,
162
+ expiration : Duration . days ( 30 ) , // TODO: make this configurable
163
+ id : 'rule' ,
164
+ } )
165
+
166
+ const docsDistribution = new cloudfront . Distribution ( this , 'DocsDistribution' , {
167
+ domainNames : [ `${ app . subdomains . docs } .${ app . url } ` ] ,
168
+ defaultRootObject : 'index.html' ,
169
+ comment : `CDN for ${ app . subdomains . docs } .${ app . url } ` ,
170
+ certificate : docsCertificate ,
171
+ // originShieldEnabled: true,
172
+ enableLogging : true ,
173
+ logBucket,
174
+ httpVersion : cloudfront . HttpVersion . HTTP2_AND_3 ,
175
+ priceClass : cloudfront . PriceClass . PRICE_CLASS_ALL ,
176
+ enabled : true ,
177
+ minimumProtocolVersion : cloudfront . SecurityPolicyProtocol . TLS_V1_2_2021 ,
178
+ webAclId : webAcl . attrArn ,
179
+ enableIpv6 : true ,
180
+
181
+ defaultBehavior : {
182
+ origin : new origins . S3Origin ( docsBucket , {
183
+ originAccessIdentity,
184
+ } ) ,
185
+ compress : true ,
186
+ allowedMethods : cloudfront . AllowedMethods . ALLOW_GET_HEAD ,
187
+ cachedMethods : cloudfront . CachedMethods . CACHE_GET_HEAD ,
188
+ viewerProtocolPolicy : cloudfront . ViewerProtocolPolicy . REDIRECT_TO_HTTPS ,
189
+ cachePolicy : cloudfront . CachePolicy . CACHING_OPTIMIZED ,
190
+ } ,
191
+
192
+ // errorResponses: [
193
+ // {
194
+ // httpStatus: 403,
195
+ // responsePagePath: '/index.html',
196
+ // responseHttpStatus: 200,
197
+ // ttl: cdk.Duration.minutes(0),
198
+ // },
199
+ // {
200
+ // httpStatus: 404,
201
+ // responsePagePath: '/index.html',
202
+ // responseHttpStatus: 200,
203
+ // ttl: cdk.Duration.minutes(0),
204
+ // },
205
+ // ],
206
+ } )
207
+ // Create a Route53 record pointing to the Docs CloudFront distribution
208
+ new route53 . ARecord ( this , 'DocsAliasRecord' , {
209
+ recordName : `${ app . subdomains . docs } .${ domainName } ` ,
210
+ zone,
211
+ target : route53 . RecordTarget . fromAlias ( new targets . CloudFrontTarget ( docsDistribution ) ) ,
212
+ } )
213
+
214
+ new s3deploy . BucketDeployment ( this , 'DeployDocs' , {
215
+ sources : [ s3deploy . Source . asset ( docsSource ) ] ,
216
+ destinationBucket : docsBucket ,
217
+ distribution : docsDistribution ,
218
+ distributionPaths : [ '/*' ] ,
219
+ } )
220
+
221
+ new Output ( this , 'DocsBucketName' , {
222
+ value : docsBucket . bucketName ,
223
+ description : 'The name of the docs bucket' ,
224
+ } )
225
+
226
+ // Prints out the web endpoint to the terminal
227
+ new Output ( this , 'DocsUrl' , {
228
+ value : `https://${ app . subdomains . docs } .${ app . url } ` ,
229
+ description : 'The URL of the deployed documentation' ,
230
+ } )
231
+ }
220
232
// Prints out the web endpoint to the terminal
221
233
new Output ( this , 'AppUrl' , {
222
234
value : `https://${ domainName } ` ,
223
235
description : 'The URL of the deployed application' ,
224
236
} )
225
237
226
- // Prints out the web endpoint to the terminal
227
- new Output ( this , 'DocsUrl' , {
228
- value : `https://${ app . subdomains . docs } .${ app . url } ` ,
229
- description : 'The URL of the deployed documentation' ,
230
- } )
231
-
232
238
// Prints out the web endpoint to the terminal
233
239
new Output ( this , 'VanityUrl' , {
234
240
value : `https://${ distribution . domainName } ` ,
@@ -242,3 +248,7 @@ export class StacksCloud extends Stack {
242
248
// })
243
249
}
244
250
}
251
+
252
+ function shouldDeployDocs ( ) {
253
+ return hasFiles ( projectPath ( 'docs' ) )
254
+ }
0 commit comments