Skip to content

Commit b078f96

Browse files
committed
chore: wip
chore: wip chore: wip
1 parent 8bafae8 commit b078f96

File tree

10 files changed

+116
-447
lines changed

10 files changed

+116
-447
lines changed
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
/* eslint-disable no-new */
2-
import { NestedStack, RemovalPolicy, Tags, aws_backup as backup, aws_iam as iam, aws_s3 as s3, aws_s3_deployment as s3deploy } from 'aws-cdk-lib'
2+
import { NestedStack, aws_s3_deployment as s3deploy } from 'aws-cdk-lib'
33
import type { Construct } from 'constructs'
4-
import type { NestedCloudProps } from './index'
4+
import type { NestedCloudProps } from '../types'
55

66
export class StorageStack extends NestedStack {
77
constructor(scope: Construct, props: NestedCloudProps) {
88
super(scope, 'Deploy', props)
99

10-
new s3deploy.BucketDeployment(this, 'DeployWebsite', {
10+
new s3deploy.BucketDeployment(this, 'Website', {
1111
sources: [s3deploy.Source.asset(this.websiteSource)],
1212
destinationBucket: props.publicBucket,
1313
distribution: props.cdn,
1414
distributionPaths: ['/*'],
1515
})
16+
17+
new s3deploy.BucketDeployment(this, 'PrivateFiles', {
18+
sources: [s3deploy.Source.asset(this.privateSource)],
19+
destinationBucket: props.privateBucket,
20+
})
1621
}
1722
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { NestedStackProps } from 'aws-cdk-lib'
21
import { NestedStack } from 'aws-cdk-lib'
32
import type { Construct } from 'constructs'
3+
import type { NestedCloudProps } from '../types'
44

55
export class DocsStack extends NestedStack {
66
constructor(scope: Construct, props: NestedStackProps) {

.stacks/core/cloud/src/cloud/index.ts

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,18 @@
11
/* eslint-disable no-new */
22
import type { Construct } from 'constructs'
33
import { Stack } from 'aws-cdk-lib'
4-
import type { NestedStackProps, StackProps, aws_certificatemanager as acm, aws_cloudfront as cloudfront, aws_route53 as route53, aws_s3 as s3, aws_wafv2 as wafv2 } from 'aws-cdk-lib'
4+
import type { CloudProps } from '../types'
55
import { CdnStack } from './cdn'
66
import { DocsStack } from './docs'
77
import { StorageStack } from './storage'
8-
9-
export interface CloudProps extends StackProps {
10-
env: {
11-
account: string
12-
region: string
13-
}
14-
appName: string
15-
appEnv: string
16-
domain: string
17-
partialAppKey: string
18-
zone: route53.HostedZone
19-
certificate: acm.Certificate
20-
logBucket: s3.Bucket
21-
firewall: wafv2.CfnWebACL
22-
storage: {
23-
publicBucket: s3.Bucket
24-
accessPoint: s3.CfnAccessPoint | undefined
25-
}
26-
cdn: cloudfront.Distribution
27-
}
28-
29-
export interface NestedCloudProps extends NestedStackProps {
30-
env: {
31-
account: string
32-
region: string
33-
}
34-
appName: string
35-
appEnv: string
36-
domain: string
37-
partialAppKey: string
38-
zone: route53.HostedZone
39-
certificate: acm.Certificate
40-
logBucket: s3.Bucket
41-
firewall: wafv2.CfnWebACL
42-
storage: {
43-
publicBucket: s3.Bucket
44-
accessPoint: s3.CfnAccessPoint | undefined
45-
}
46-
cdn: cloudfront.Distribution
47-
}
8+
import { SecurityStack } from './security'
489

4910
export class Cloud extends Stack {
5011
constructor(scope: Construct, id: string, props: CloudProps) {
5112
super(scope, id, props)
5213

5314
// please beware: be careful changing the order of the stacks creation below
15+
new SecurityStack(this, props)
5416
new StorageStack(this, props)
5517
new CdnStack(this, props)
5618
new DocsStack(this, props)

.stacks/core/cloud/src/cloud/permissions.ts

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// waf and encryption
2+
import { NestedStack } from 'aws-cdk-lib'
3+
import type { Construct } from 'constructs'
4+
import type { NestedCloudProps } from '../types'
5+
6+
export class SecurityStack extends NestedStack {
7+
constructor(scope: Construct, props: NestedStackProps) {
8+
super(scope, 'Security', props)
9+
// ...
10+
}
11+
}

.stacks/core/cloud/src/cloud/storage.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-new */
2-
import { NestedStack, RemovalPolicy, Tags, aws_backup as backup, aws_iam as iam, aws_s3 as s3, aws_s3_deployment as s3deploy } from 'aws-cdk-lib'
2+
import { NestedStack, RemovalPolicy, Tags, aws_backup as backup, aws_iam as iam, aws_s3 as s3 } from 'aws-cdk-lib'
33
import type { Construct } from 'constructs'
4-
import type { NestedCloudProps } from './index'
4+
import type { NestedCloudProps } from '../types'
55

66
export class StorageStack extends NestedStack {
77
websiteSource: string
@@ -15,15 +15,13 @@ export class StorageStack extends NestedStack {
1515
super(scope, 'Storage', props)
1616
this.websiteSource = config.app.docMode ? this.docsSource : '../../../storage/public'
1717
this.bucketPrefix = `${props.appName}-${props.appEnv}`
18-
19-
2018
}
2119

2220
async manageStorage() {
2321
// the bucketName should not contain the domainName because when the APP_URL is changed,
2422
// we want it to deploy properly, and this way we would not force a recreation of the
2523
// resources that contain the domain name
26-
this.storage.publicBucket = await this.getOrCreateBucket()
24+
this.publicBucket = await this.getOrCreateBucket()
2725
// for each redirect, create a bucket & redirect it to the APP_URL
2826
config.dns.redirects?.forEach((redirect) => {
2927
// TODO: use string-ts function here instead
@@ -45,11 +43,11 @@ export class StorageStack extends NestedStack {
4543
})
4644
})
4745

48-
this.storage.privateBucket = await this.getOrCreateBucket('private')
49-
const bucketPrefix = `${this.appName}-${appEnv}`
46+
this.privateBucket = await this.getOrCreateBucket('private')
47+
const bucketPrefix = `${props.appName}-${props.appEnv}`
5048

51-
this.storage.logBucket = new s3.Bucket(this, 'LogsBucket', {
52-
bucketName: `${bucketPrefix}-logs-${partialAppKey}`,
49+
this.logBucket = new s3.Bucket(this, 'LogsBucket', {
50+
bucketName: `${bucketPrefix}-logs-${props.partialAppKey}`,
5351
removalPolicy: RemovalPolicy.DESTROY,
5452
autoDeleteObjects: true,
5553
blockPublicAccess: new s3.BlockPublicAccess({
@@ -60,13 +58,13 @@ export class StorageStack extends NestedStack {
6058
}),
6159
objectOwnership: s3.ObjectOwnership.BUCKET_OWNER_PREFERRED,
6260
})
63-
Tags.of(this.storage.logBucket).add('daily-backup', 'true')
61+
Tags.of(this.logBucket).add('daily-backup', 'true')
6462

6563
const backupRole = this.createBackupRole()
6664

6765
// Daily 35 day retention
6866
const vault = new backup.BackupVault(this, 'BackupVault', {
69-
backupVaultName: `${this.appName}-${appEnv}-daily-backup-vault`,
67+
backupVaultName: `${props.appName}-${appEnv}-daily-backup-vault`,
7068
encryptionKey: this.encryptionKey,
7169
removalPolicy: RemovalPolicy.DESTROY,
7270
})

.stacks/core/cloud/src/deploy.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,46 @@ import { config } from '@stacksjs/config'
44
import { env } from '@stacksjs/env'
55
import * as cdk from 'aws-cdk-lib'
66
import { Cloud } from './cloud/'
7+
import type { CloudOptions } from './types'
78

89
const app = new cdk.App()
910
const appEnv = config.app.env === 'local' ? 'dev' : config.app.env
10-
const cloudName = `stacks-cloud-${appEnv}`
11+
const appKey = config.app.key
12+
const domain = config.app.url
13+
const name = `stacks-cloud-${appEnv}`
1114
const account = env.AWS_ACCOUNT_ID
1215
const region = env.AWS_DEFAULT_REGION
1316

14-
if (!account || !region) {
15-
console.error('Missing accountId or region in config.')
16-
process.exit(1)
17+
if (!appKey) {
18+
log.info('Please set an application key. `buddy key:generate` is your friend, in this case.')
19+
process.exit(ExitCode.InvalidArgument)
1720
}
1821

22+
const parts = appKey.split(':')
23+
if (parts && parts.length < 2)
24+
throw new Error('Invalid format application key format. Expected a colon-separated string. You may need to run `buddy key:generate`.')
25+
26+
const partialAppKey = parts[1] ? parts[1].substring(0, 10).toLowerCase() : undefined
27+
28+
if (!partialAppKey)
29+
throw new Error('The application key seems to be missing. Please set it before deploying. `buddy key:generate` is your friend, in this case.')
30+
31+
if (!account || !region)
32+
throw new Error('Missing accountId or region in config.')
33+
1934
const usEnv = {
2035
account,
2136
region,
2237
}
2338

24-
new Cloud(app, cloudName, {
39+
export const options = {
2540
env: usEnv,
26-
})
41+
name,
42+
appEnv,
43+
domain,
44+
partialAppKey,
45+
} satisfies CloudOptions
46+
47+
new Cloud(app, name, options)
2748

2849
app.synth()

0 commit comments

Comments
 (0)