Skip to content

Commit 34d98a4

Browse files
committed
chore: wip
1 parent 52999f7 commit 34d98a4

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

.stacks/core/cloud/src/cloud/cdn.ts

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ export class CdnStack extends NestedStack {
3030
certificateArn: string
3131
certificate: acm.ICertificate
3232
logBucket: s3.IBucket
33+
publicBucket: s3.IBucket
34+
firewallArn: string
35+
apiCachePolicy: cloudfront.CachePolicy | undefined
36+
props: NestedCloudProps
3337

3438
constructor(scope: Construct, props: NestedCloudProps) {
3539
super(scope, 'Cdn', props)
40+
this.props = props
3641

3742
// do lookups
3843
this.zone = route53.PublicHostedZone.fromLookup(this, 'AppUrlHostedZone', {
@@ -41,11 +46,18 @@ export class CdnStack extends NestedStack {
4146
this.certificateArn = Stack.of(this).formatArn({
4247
service: 'acm',
4348
resource: 'certificate',
44-
resourceName: 'Certificate', // replace with your actual certificate logical ID
49+
resourceName: 'Certificate',
4550
})
4651
this.certificate = acm.Certificate.fromCertificateArn(this, 'Certificate', this.certificateArn)
52+
this.firewallArn = Stack.of(this).formatArn({
53+
service: 'wafv2',
54+
resource: 'webacl',
55+
resourceName: 'WebFirewall',
56+
})
57+
4758
const bucketPrefix = `${props.appName}-${props.appEnv}`
4859
this.logBucket = s3.Bucket.fromBucketName(this, 'LogBucket', `${bucketPrefix}-logs-${props.partialAppKey}`)
60+
this.publicBucket = s3.Bucket.fromBucketName(this, 'PublicBucket', `${bucketPrefix}-${props.partialAppKey}`)
4961

5062
// proceed with cdn logic
5163
this.originAccessIdentity = new cloudfront.OriginAccessIdentity(this, 'OAI')
@@ -99,22 +111,23 @@ export class CdnStack extends NestedStack {
99111
const cfnOriginRequestFunction = originRequestFunction.node.defaultChild as CfnResource
100112
cfnOriginRequestFunction.applyRemovalPolicy(RemovalPolicy.RETAIN)
101113

102-
const cdn = new cloudfront.Distribution(this, 'Distribution', {
114+
// the actual CDN distribution
115+
new cloudfront.Distribution(this, 'Distribution', {
103116
domainNames: [props.domain],
104117
defaultRootObject: 'index.html',
105118
comment: `CDN for ${config.app.url}`,
106-
certificate,
119+
certificate: this.certificate,
107120
enableLogging: true,
108-
logBucket: props.logBucket,
121+
logBucket: this.logBucket,
109122
httpVersion: cloudfront.HttpVersion.HTTP2_AND_3,
110123
priceClass: cloudfront.PriceClass.PRICE_CLASS_ALL,
111124
enabled: true,
112125
minimumProtocolVersion: cloudfront.SecurityPolicyProtocol.TLS_V1_2_2021,
113-
webAclId: props.firewall.attrArn,
126+
webAclId: this.firewallArn,
114127
enableIpv6: true,
115128

116129
defaultBehavior: {
117-
origin: new origins.S3Origin(props.publicBucket, {
130+
origin: new origins.S3Origin(this.publicBucket, {
118131
originAccessIdentity: this.originAccessIdentity,
119132
}),
120133
edgeLambdas: [
@@ -240,24 +253,6 @@ export class CdnStack extends NestedStack {
240253
return config.cloud.deploy?.api
241254
}
242255

243-
deployApi(props) {
244-
const keysToRemove = ['_HANDLER', '_X_AMZN_TRACE_ID', 'AWS_REGION', 'AWS_EXECUTION_ENV', 'AWS_LAMBDA_FUNCTION_NAME', 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'AWS_LAMBDA_FUNCTION_VERSION', 'AWS_LAMBDA_INITIALIZATION_TYPE', 'AWS_LAMBDA_LOG_GROUP_NAME', 'AWS_LAMBDA_LOG_STREAM_NAME', 'AWS_ACCESS_KEY', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_SESSION_TOKEN', 'AWS_LAMBDA_RUNTIME_API', 'LAMBDA_TASK_ROOT', 'LAMBDA_RUNTIME_DIR', '_']
245-
keysToRemove.forEach(key => delete env[key as EnvKey])
246-
247-
const secrets = new secretsmanager.Secret(this, 'StacksSecrets', {
248-
secretName: `${props.appName}-${props.appEnv}-secrets`,
249-
description: 'Secrets for the Stacks application',
250-
generateSecretString: {
251-
secretStringTemplate: JSON.stringify(env),
252-
generateStringKey: Object.keys(env).join(',').length.toString(),
253-
},
254-
})
255-
256-
const functionName = `${props.appName}-${props.appEnv}-server`
257-
258-
// this.apiVanityUrl = api.url
259-
}
260-
261256
apiBehaviorOptions(): Record<string, cloudfront.BehaviorOptions> {
262257
const origin = (path: '/api' | '/api/*' = '/api') => new origins.HttpOrigin(Fn.select(2, Fn.split('/', this.apiVanityUrl)), { // removes the https://
263258
originPath: path,
@@ -287,7 +282,7 @@ export class CdnStack extends NestedStack {
287282
docsBehaviorOptions(): Record<string, cloudfront.BehaviorOptions> {
288283
return {
289284
'/docs': {
290-
origin: new origins.S3Origin(this.props.storage.publicBucket, {
285+
origin: new origins.S3Origin(this.publicBucket, {
291286
originAccessIdentity: this.originAccessIdentity,
292287
originPath: '/docs',
293288
}),
@@ -298,7 +293,7 @@ export class CdnStack extends NestedStack {
298293
cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED,
299294
},
300295
'/docs/*': {
301-
origin: new origins.S3Origin(this.props.storage.publicBucket, {
296+
origin: new origins.S3Origin(this.publicBucket, {
302297
originAccessIdentity: this.originAccessIdentity,
303298
originPath: '/docs',
304299
}),
@@ -315,13 +310,23 @@ export class CdnStack extends NestedStack {
315310
return hasFiles(p.projectPath('docs'))
316311
}
317312

318-
additionalBehaviors(props): Record<string, cloudfront.BehaviorOptions> {
313+
additionalBehaviors(props: NestedCloudProps): Record<string, cloudfront.BehaviorOptions> {
319314
let behaviorOptions: Record<string, cloudfront.BehaviorOptions> = {}
320315

321316
if (this.shouldDeployApi()) {
322-
this.deployApi(props)
317+
const keysToRemove = ['_HANDLER', '_X_AMZN_TRACE_ID', 'AWS_REGION', 'AWS_EXECUTION_ENV', 'AWS_LAMBDA_FUNCTION_NAME', 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'AWS_LAMBDA_FUNCTION_VERSION', 'AWS_LAMBDA_INITIALIZATION_TYPE', 'AWS_LAMBDA_LOG_GROUP_NAME', 'AWS_LAMBDA_LOG_STREAM_NAME', 'AWS_ACCESS_KEY', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_SESSION_TOKEN', 'AWS_LAMBDA_RUNTIME_API', 'LAMBDA_TASK_ROOT', 'LAMBDA_RUNTIME_DIR', '_']
318+
keysToRemove.forEach(key => delete env[key as EnvKey])
319+
320+
new secretsmanager.Secret(this, 'StacksSecrets', {
321+
secretName: `${props.appName}-${props.appEnv}-secrets`,
322+
description: 'Secrets for the Stacks application',
323+
generateSecretString: {
324+
secretStringTemplate: JSON.stringify(env),
325+
generateStringKey: Object.keys(env).join(',').length.toString(),
326+
},
327+
})
323328

324-
behaviorOptions = this.apiBehaviorOptions()
329+
// behaviorOptions = this.apiBehaviorOptions()
325330
}
326331

327332
// if docMode is used, we don't need to add a behavior for the docs

.stacks/core/cloud/src/cloud/docs.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
import { NestedStack } from 'aws-cdk-lib'
1+
/* eslint-disable no-new */
2+
import {
3+
NestedStack,
4+
CfnOutput as Output,
5+
} from 'aws-cdk-lib'
26
import type { Construct } from 'constructs'
7+
import { config } from '@stacksjs/config'
38
import type { NestedCloudProps } from '../types'
49

510
export class DocsStack extends NestedStack {
611
constructor(scope: Construct, props: NestedCloudProps) {
712
super(scope, 'Docs', props)
8-
// ...
13+
// if docsPrefix is not set, then we know we are in docsMode and the documentation lives at the root of the domain
14+
const docsPrefix = config.app.docMode ? undefined : config.docs.base
15+
const docsSource = '../../../storage/framework/docs'
16+
17+
new Output(this, 'DocsUrl', {
18+
value: `https://${props.domain}/${docsPrefix}`,
19+
description: 'The URL of the deployed documentation',
20+
})
921
}
1022
}

.stacks/core/cloud/src/old.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ export class StacksCloud extends Stack {
4444
appName = config.app.name?.toLocaleLowerCase() || 'stacks'
4545
teamName = config.team.name.toLowerCase() || 'stacks'
4646
apiPrefix!: string
47-
docsPrefix?: string
4847
apiVanityUrl!: string
4948
vanityUrl!: string
50-
docsSource!: string
5149
websiteSource!: string
5250
privateSource!: string
5351
zone!: route53.IHostedZone
@@ -100,8 +98,7 @@ export class StacksCloud extends Stack {
10098
this.domain = `${appEnv}.${config.app.url}`
10199

102100
this.apiPrefix = config.api.prefix || 'api'
103-
this.docsPrefix = config.app.docMode ? undefined : config.docs.base
104-
this.docsSource = '../../../storage/framework/docs'
101+
// this.docsSource = '../../../storage/framework/docs'
105102
this.websiteSource = config.app.docMode ? this.docsSource : '../../../storage/public'
106103
this.privateSource = '../../../storage/private'
107104
this.apiVanityUrl = ''
@@ -1045,14 +1042,6 @@ export class StacksCloud extends Stack {
10451042
description: 'The ID of the EC2 instance that can be used to SSH into the Stacks Cloud.',
10461043
})
10471044
}
1048-
1049-
// if docsPrefix is not set, then we know we are in docsMode and the documentation lives at the root of the domain
1050-
if (this.shouldDeployDocs() && this.docsPrefix) {
1051-
new Output(this, 'DocsUrl', {
1052-
value: `https://${this.domain}/${this.docsPrefix}`,
1053-
description: 'The URL of the deployed documentation',
1054-
})
1055-
}
10561045
}
10571046

10581047
handleEmailBucket(bucketPrefix: string): s3.Bucket {

0 commit comments

Comments
 (0)