@stacksjs/ts-cloud-core 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +321 -0
- package/package.json +31 -0
- package/src/advanced-features.test.ts +465 -0
- package/src/aws/cloudformation.ts +421 -0
- package/src/aws/cloudfront.ts +158 -0
- package/src/aws/credentials.test.ts +132 -0
- package/src/aws/credentials.ts +545 -0
- package/src/aws/index.ts +87 -0
- package/src/aws/s3.test.ts +188 -0
- package/src/aws/s3.ts +1088 -0
- package/src/aws/signature.test.ts +670 -0
- package/src/aws/signature.ts +1155 -0
- package/src/backup/disaster-recovery.test.ts +726 -0
- package/src/backup/disaster-recovery.ts +500 -0
- package/src/backup/index.ts +34 -0
- package/src/backup/manager.test.ts +498 -0
- package/src/backup/manager.ts +432 -0
- package/src/cicd/circleci.ts +430 -0
- package/src/cicd/github-actions.ts +424 -0
- package/src/cicd/gitlab-ci.ts +255 -0
- package/src/cicd/index.ts +8 -0
- package/src/cli/history.ts +396 -0
- package/src/cli/index.ts +10 -0
- package/src/cli/progress.ts +458 -0
- package/src/cli/repl.ts +454 -0
- package/src/cli/suggestions.ts +327 -0
- package/src/cli/table.test.ts +319 -0
- package/src/cli/table.ts +332 -0
- package/src/cloudformation/builder.test.ts +327 -0
- package/src/cloudformation/builder.ts +378 -0
- package/src/cloudformation/builders/api-gateway.ts +449 -0
- package/src/cloudformation/builders/cache.ts +334 -0
- package/src/cloudformation/builders/cdn.ts +278 -0
- package/src/cloudformation/builders/compute.ts +485 -0
- package/src/cloudformation/builders/database.ts +392 -0
- package/src/cloudformation/builders/functions.ts +343 -0
- package/src/cloudformation/builders/messaging.ts +140 -0
- package/src/cloudformation/builders/monitoring.ts +300 -0
- package/src/cloudformation/builders/network.ts +264 -0
- package/src/cloudformation/builders/queue.ts +147 -0
- package/src/cloudformation/builders/security.ts +399 -0
- package/src/cloudformation/builders/storage.ts +285 -0
- package/src/cloudformation/index.ts +30 -0
- package/src/cloudformation/types.ts +173 -0
- package/src/compliance/aws-config.ts +543 -0
- package/src/compliance/cloudtrail.ts +376 -0
- package/src/compliance/compliance.test.ts +423 -0
- package/src/compliance/guardduty.ts +446 -0
- package/src/compliance/index.ts +66 -0
- package/src/compliance/security-hub.ts +456 -0
- package/src/containers/build-optimization.ts +416 -0
- package/src/containers/containers.test.ts +508 -0
- package/src/containers/image-scanning.ts +360 -0
- package/src/containers/index.ts +9 -0
- package/src/containers/registry.ts +293 -0
- package/src/containers/service-mesh.ts +520 -0
- package/src/database/database.test.ts +762 -0
- package/src/database/index.ts +9 -0
- package/src/database/migrations.ts +444 -0
- package/src/database/performance.ts +528 -0
- package/src/database/replicas.ts +534 -0
- package/src/database/users.ts +494 -0
- package/src/dependency-graph.ts +143 -0
- package/src/deployment/ab-testing.ts +582 -0
- package/src/deployment/blue-green.ts +452 -0
- package/src/deployment/canary.ts +500 -0
- package/src/deployment/deployment.test.ts +526 -0
- package/src/deployment/index.ts +61 -0
- package/src/deployment/progressive.ts +62 -0
- package/src/dns/dns.test.ts +641 -0
- package/src/dns/dnssec.ts +315 -0
- package/src/dns/index.ts +8 -0
- package/src/dns/resolver.ts +496 -0
- package/src/dns/routing.ts +593 -0
- package/src/email/advanced/analytics.ts +445 -0
- package/src/email/advanced/index.ts +11 -0
- package/src/email/advanced/rules.ts +465 -0
- package/src/email/advanced/scheduling.ts +352 -0
- package/src/email/advanced/search.ts +412 -0
- package/src/email/advanced/shared-mailboxes.ts +404 -0
- package/src/email/advanced/templates.ts +455 -0
- package/src/email/advanced/threading.ts +281 -0
- package/src/email/analytics.ts +467 -0
- package/src/email/bounce-handling.ts +425 -0
- package/src/email/email.test.ts +431 -0
- package/src/email/handlers/__tests__/inbound.test.ts +38 -0
- package/src/email/handlers/__tests__/outbound.test.ts +37 -0
- package/src/email/handlers/converter.ts +227 -0
- package/src/email/handlers/feedback.ts +228 -0
- package/src/email/handlers/inbound.ts +169 -0
- package/src/email/handlers/outbound.ts +178 -0
- package/src/email/index.ts +15 -0
- package/src/email/reputation.ts +303 -0
- package/src/email/templates.ts +352 -0
- package/src/errors/index.test.ts +434 -0
- package/src/errors/index.ts +416 -0
- package/src/health-checks/index.ts +40 -0
- package/src/index.ts +360 -0
- package/src/intrinsic-functions.ts +118 -0
- package/src/lambda/concurrency.ts +330 -0
- package/src/lambda/destinations.ts +345 -0
- package/src/lambda/dlq.ts +425 -0
- package/src/lambda/index.ts +11 -0
- package/src/lambda/lambda.test.ts +840 -0
- package/src/lambda/layers.ts +263 -0
- package/src/lambda/versions.ts +376 -0
- package/src/lambda/vpc.ts +399 -0
- package/src/local/config.ts +114 -0
- package/src/local/index.ts +6 -0
- package/src/local/mock-aws.ts +351 -0
- package/src/modules/ai.ts +340 -0
- package/src/modules/api.ts +478 -0
- package/src/modules/auth.ts +805 -0
- package/src/modules/cache.ts +417 -0
- package/src/modules/cdn.ts +1062 -0
- package/src/modules/communication.ts +1094 -0
- package/src/modules/compute.ts +3348 -0
- package/src/modules/database.ts +554 -0
- package/src/modules/deployment.ts +1079 -0
- package/src/modules/dns.ts +337 -0
- package/src/modules/email.ts +1538 -0
- package/src/modules/filesystem.ts +515 -0
- package/src/modules/index.ts +32 -0
- package/src/modules/messaging.ts +486 -0
- package/src/modules/monitoring.ts +2086 -0
- package/src/modules/network.ts +664 -0
- package/src/modules/parameter-store.ts +325 -0
- package/src/modules/permissions.ts +1081 -0
- package/src/modules/phone.ts +494 -0
- package/src/modules/queue.ts +1260 -0
- package/src/modules/redirects.ts +464 -0
- package/src/modules/registry.ts +699 -0
- package/src/modules/search.ts +401 -0
- package/src/modules/secrets.ts +416 -0
- package/src/modules/security.ts +731 -0
- package/src/modules/sms.ts +389 -0
- package/src/modules/storage.ts +1120 -0
- package/src/modules/workflow.ts +680 -0
- package/src/multi-account/config.ts +521 -0
- package/src/multi-account/index.ts +7 -0
- package/src/multi-account/manager.ts +427 -0
- package/src/multi-region/cross-region.ts +410 -0
- package/src/multi-region/index.ts +8 -0
- package/src/multi-region/manager.ts +483 -0
- package/src/multi-region/regions.ts +435 -0
- package/src/network-security/index.ts +48 -0
- package/src/observability/index.ts +9 -0
- package/src/observability/logs.ts +522 -0
- package/src/observability/metrics.ts +460 -0
- package/src/observability/observability.test.ts +782 -0
- package/src/observability/synthetics.ts +568 -0
- package/src/observability/xray.ts +358 -0
- package/src/phone/advanced/analytics.ts +349 -0
- package/src/phone/advanced/callbacks.ts +428 -0
- package/src/phone/advanced/index.ts +8 -0
- package/src/phone/advanced/ivr-builder.ts +504 -0
- package/src/phone/advanced/recording.ts +310 -0
- package/src/phone/handlers/__tests__/incoming-call.test.ts +40 -0
- package/src/phone/handlers/incoming-call.ts +117 -0
- package/src/phone/handlers/missed-call.ts +116 -0
- package/src/phone/handlers/voicemail.ts +179 -0
- package/src/phone/index.ts +9 -0
- package/src/presets/api-backend.ts +134 -0
- package/src/presets/data-pipeline.ts +204 -0
- package/src/presets/extend.test.ts +295 -0
- package/src/presets/extend.ts +297 -0
- package/src/presets/fullstack-app.ts +144 -0
- package/src/presets/index.ts +27 -0
- package/src/presets/jamstack.ts +135 -0
- package/src/presets/microservices.ts +167 -0
- package/src/presets/ml-api.ts +208 -0
- package/src/presets/nodejs-server.ts +104 -0
- package/src/presets/nodejs-serverless.ts +114 -0
- package/src/presets/realtime-app.ts +184 -0
- package/src/presets/static-site.ts +64 -0
- package/src/presets/traditional-web-app.ts +339 -0
- package/src/presets/wordpress.ts +138 -0
- package/src/preview/github.test.ts +249 -0
- package/src/preview/github.ts +297 -0
- package/src/preview/index.ts +37 -0
- package/src/preview/manager.test.ts +440 -0
- package/src/preview/manager.ts +326 -0
- package/src/preview/notifications.test.ts +582 -0
- package/src/preview/notifications.ts +341 -0
- package/src/queue/batch-processing.ts +402 -0
- package/src/queue/dlq-monitoring.ts +402 -0
- package/src/queue/fifo.ts +342 -0
- package/src/queue/index.ts +9 -0
- package/src/queue/management.ts +428 -0
- package/src/queue/queue.test.ts +429 -0
- package/src/resource-mgmt/index.ts +39 -0
- package/src/resource-naming.ts +62 -0
- package/src/s3/index.ts +523 -0
- package/src/schema/cloud-config.schema.json +554 -0
- package/src/schema/index.ts +68 -0
- package/src/security/certificate-manager.ts +492 -0
- package/src/security/index.ts +9 -0
- package/src/security/scanning.ts +545 -0
- package/src/security/secrets-manager.ts +476 -0
- package/src/security/secrets-rotation.ts +456 -0
- package/src/security/security.test.ts +738 -0
- package/src/sms/advanced/ab-testing.ts +389 -0
- package/src/sms/advanced/analytics.ts +336 -0
- package/src/sms/advanced/campaigns.ts +523 -0
- package/src/sms/advanced/chatbot.ts +224 -0
- package/src/sms/advanced/index.ts +10 -0
- package/src/sms/advanced/link-tracking.ts +248 -0
- package/src/sms/advanced/mms.ts +308 -0
- package/src/sms/handlers/__tests__/send.test.ts +40 -0
- package/src/sms/handlers/delivery-status.ts +133 -0
- package/src/sms/handlers/receive.ts +162 -0
- package/src/sms/handlers/send.ts +174 -0
- package/src/sms/index.ts +9 -0
- package/src/stack-diff.ts +389 -0
- package/src/static-site/index.ts +85 -0
- package/src/template-builder.ts +110 -0
- package/src/template-validator.ts +574 -0
- package/src/utils/cache.ts +291 -0
- package/src/utils/diff.ts +269 -0
- package/src/utils/hash.ts +227 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/parallel.ts +294 -0
- package/src/validators/credentials.test.ts +274 -0
- package/src/validators/credentials.ts +233 -0
- package/src/validators/quotas.test.ts +434 -0
- package/src/validators/quotas.ts +217 -0
- package/test/ai.test.ts +327 -0
- package/test/api.test.ts +511 -0
- package/test/auth.test.ts +632 -0
- package/test/cache.test.ts +406 -0
- package/test/cdn.test.ts +247 -0
- package/test/compute.test.ts +861 -0
- package/test/database.test.ts +523 -0
- package/test/deployment.test.ts +499 -0
- package/test/dns.test.ts +270 -0
- package/test/email.test.ts +439 -0
- package/test/filesystem.test.ts +382 -0
- package/test/integration.test.ts +350 -0
- package/test/messaging.test.ts +514 -0
- package/test/monitoring.test.ts +634 -0
- package/test/network.test.ts +425 -0
- package/test/permissions.test.ts +488 -0
- package/test/queue.test.ts +484 -0
- package/test/registry.test.ts +306 -0
- package/test/security.test.ts +462 -0
- package/test/storage.test.ts +463 -0
- package/test/template-validator.test.ts +559 -0
- package/test/workflow.test.ts +592 -0
- package/tsconfig.json +16 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,1120 @@
|
|
|
1
|
+
import type { BackupPlan, BackupSelection, BackupVault, S3Bucket, S3BucketPolicy } from '@stacksjs/ts-cloud-aws-types'
|
|
2
|
+
import type { IAMRole } from '@stacksjs/ts-cloud-aws-types'
|
|
3
|
+
import { existsSync, readdirSync } from 'node:fs'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { Fn } from '../intrinsic-functions'
|
|
6
|
+
import { generateLogicalId, generateResourceName } from '../resource-naming'
|
|
7
|
+
import type { EnvironmentType } from '@stacksjs/ts-cloud-types'
|
|
8
|
+
|
|
9
|
+
export interface BucketOptions {
|
|
10
|
+
name?: string
|
|
11
|
+
bucketName?: string // Alias for name
|
|
12
|
+
slug: string
|
|
13
|
+
environment: EnvironmentType
|
|
14
|
+
public?: boolean
|
|
15
|
+
versioning?: boolean
|
|
16
|
+
website?: boolean
|
|
17
|
+
encryption?: boolean
|
|
18
|
+
intelligentTiering?: boolean
|
|
19
|
+
cors?: CorsRule[]
|
|
20
|
+
lifecycleRules?: LifecycleRule[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CorsRule {
|
|
24
|
+
allowedOrigins: string[]
|
|
25
|
+
allowedMethods: string[]
|
|
26
|
+
allowedHeaders?: string[]
|
|
27
|
+
maxAge?: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface LifecycleRule {
|
|
31
|
+
id: string
|
|
32
|
+
enabled: boolean
|
|
33
|
+
expirationDays?: number
|
|
34
|
+
transitions?: Array<{
|
|
35
|
+
days: number
|
|
36
|
+
storageClass: 'GLACIER' | 'DEEP_ARCHIVE' | 'INTELLIGENT_TIERING' | 'STANDARD_IA' | 'ONEZONE_IA'
|
|
37
|
+
}>
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface S3NotificationConfig {
|
|
41
|
+
functionArn: string | { 'Fn::GetAtt': [string, string] }
|
|
42
|
+
events: Array<'s3:ObjectCreated:*' | 's3:ObjectCreated:Put' | 's3:ObjectCreated:Post' | 's3:ObjectCreated:Copy' | 's3:ObjectCreated:CompleteMultipartUpload' | 's3:ObjectRemoved:*' | 's3:ObjectRemoved:Delete' | 's3:ObjectRemoved:DeleteMarkerCreated'>
|
|
43
|
+
filter?: {
|
|
44
|
+
prefix?: string
|
|
45
|
+
suffix?: string
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface BackupPlanOptions {
|
|
50
|
+
name: string
|
|
51
|
+
slug: string
|
|
52
|
+
environment: EnvironmentType
|
|
53
|
+
bucketLogicalIds: string[]
|
|
54
|
+
retentionDays: number
|
|
55
|
+
schedule?: string // Cron expression (default: daily at 5am)
|
|
56
|
+
vaultName?: string
|
|
57
|
+
enableContinuousBackup?: boolean
|
|
58
|
+
moveToColdStorageAfterDays?: number
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Storage Module - S3 Bucket Management
|
|
63
|
+
* Provides clean API for creating and configuring S3 buckets
|
|
64
|
+
*/
|
|
65
|
+
export class Storage {
|
|
66
|
+
/**
|
|
67
|
+
* Create an S3 bucket with the specified options
|
|
68
|
+
*/
|
|
69
|
+
static createBucket(options: BucketOptions): { bucket: S3Bucket, bucketPolicy?: S3BucketPolicy, logicalId: string } {
|
|
70
|
+
const {
|
|
71
|
+
name,
|
|
72
|
+
slug,
|
|
73
|
+
environment,
|
|
74
|
+
public: isPublic = false,
|
|
75
|
+
versioning = false,
|
|
76
|
+
website = false,
|
|
77
|
+
encryption = true,
|
|
78
|
+
intelligentTiering = false,
|
|
79
|
+
cors,
|
|
80
|
+
lifecycleRules,
|
|
81
|
+
} = options
|
|
82
|
+
|
|
83
|
+
const resourceName = generateResourceName({
|
|
84
|
+
slug,
|
|
85
|
+
environment,
|
|
86
|
+
resourceType: 's3',
|
|
87
|
+
suffix: name,
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
const logicalId = generateLogicalId(resourceName)
|
|
91
|
+
|
|
92
|
+
const bucket: S3Bucket = {
|
|
93
|
+
Type: 'AWS::S3::Bucket',
|
|
94
|
+
Properties: {
|
|
95
|
+
BucketName: resourceName,
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Configure encryption
|
|
100
|
+
if (encryption) {
|
|
101
|
+
bucket.Properties!.BucketEncryption = {
|
|
102
|
+
ServerSideEncryptionConfiguration: [{
|
|
103
|
+
ServerSideEncryptionByDefault: {
|
|
104
|
+
SSEAlgorithm: 'AES256',
|
|
105
|
+
},
|
|
106
|
+
}],
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Configure versioning
|
|
111
|
+
if (versioning) {
|
|
112
|
+
bucket.Properties!.VersioningConfiguration = {
|
|
113
|
+
Status: 'Enabled',
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Configure website hosting
|
|
118
|
+
if (website) {
|
|
119
|
+
bucket.Properties!.WebsiteConfiguration = {
|
|
120
|
+
IndexDocument: 'index.html',
|
|
121
|
+
ErrorDocument: 'error.html',
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Configure public access block
|
|
126
|
+
if (!isPublic) {
|
|
127
|
+
bucket.Properties!.PublicAccessBlockConfiguration = {
|
|
128
|
+
BlockPublicAcls: true,
|
|
129
|
+
BlockPublicPolicy: true,
|
|
130
|
+
IgnorePublicAcls: true,
|
|
131
|
+
RestrictPublicBuckets: true,
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Configure CORS
|
|
136
|
+
if (cors && cors.length > 0) {
|
|
137
|
+
bucket.Properties!.CorsConfiguration = {
|
|
138
|
+
CorsRules: cors.map(rule => ({
|
|
139
|
+
AllowedOrigins: rule.allowedOrigins,
|
|
140
|
+
AllowedMethods: rule.allowedMethods,
|
|
141
|
+
AllowedHeaders: rule.allowedHeaders,
|
|
142
|
+
MaxAge: rule.maxAge,
|
|
143
|
+
})),
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Configure lifecycle rules
|
|
148
|
+
if (lifecycleRules && lifecycleRules.length > 0) {
|
|
149
|
+
bucket.Properties!.LifecycleConfiguration = {
|
|
150
|
+
Rules: lifecycleRules.map(rule => ({
|
|
151
|
+
Id: rule.id,
|
|
152
|
+
Status: rule.enabled ? 'Enabled' : 'Disabled',
|
|
153
|
+
ExpirationInDays: rule.expirationDays,
|
|
154
|
+
Transitions: rule.transitions?.map(t => ({
|
|
155
|
+
TransitionInDays: t.days,
|
|
156
|
+
StorageClass: t.storageClass,
|
|
157
|
+
})),
|
|
158
|
+
})),
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Configure intelligent tiering
|
|
163
|
+
if (intelligentTiering && lifecycleRules) {
|
|
164
|
+
const intelligentTieringRule: LifecycleRule = {
|
|
165
|
+
id: 'IntelligentTieringRule',
|
|
166
|
+
enabled: true,
|
|
167
|
+
transitions: [{
|
|
168
|
+
days: 0,
|
|
169
|
+
storageClass: 'INTELLIGENT_TIERING',
|
|
170
|
+
}],
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!bucket.Properties!.LifecycleConfiguration) {
|
|
174
|
+
bucket.Properties!.LifecycleConfiguration = { Rules: [] }
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
bucket.Properties!.LifecycleConfiguration.Rules.push({
|
|
178
|
+
Id: intelligentTieringRule.id,
|
|
179
|
+
Status: 'Enabled',
|
|
180
|
+
Transitions: intelligentTieringRule.transitions?.map(t => ({
|
|
181
|
+
TransitionInDays: t.days,
|
|
182
|
+
StorageClass: t.storageClass,
|
|
183
|
+
})),
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Create bucket policy for public access if needed
|
|
188
|
+
let bucketPolicy: S3BucketPolicy | undefined
|
|
189
|
+
|
|
190
|
+
if (isPublic) {
|
|
191
|
+
bucketPolicy = {
|
|
192
|
+
Type: 'AWS::S3::BucketPolicy',
|
|
193
|
+
Properties: {
|
|
194
|
+
Bucket: Fn.Ref(logicalId),
|
|
195
|
+
PolicyDocument: {
|
|
196
|
+
Version: '2012-10-17',
|
|
197
|
+
Statement: [{
|
|
198
|
+
Sid: 'PublicReadGetObject',
|
|
199
|
+
Effect: 'Allow',
|
|
200
|
+
Principal: '*',
|
|
201
|
+
Action: ['s3:GetObject'],
|
|
202
|
+
Resource: [Fn.Join('', [Fn.GetAtt(logicalId, 'Arn'), '/*']) as any],
|
|
203
|
+
}],
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
bucket,
|
|
211
|
+
bucketPolicy,
|
|
212
|
+
logicalId,
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Enable versioning on an existing bucket
|
|
218
|
+
*/
|
|
219
|
+
static enableVersioning(bucket: S3Bucket): S3Bucket {
|
|
220
|
+
if (!bucket.Properties) {
|
|
221
|
+
bucket.Properties = {}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
bucket.Properties.VersioningConfiguration = {
|
|
225
|
+
Status: 'Enabled',
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return bucket
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Enable website hosting on an existing bucket
|
|
233
|
+
*/
|
|
234
|
+
static enableWebsiteHosting(
|
|
235
|
+
bucket: S3Bucket,
|
|
236
|
+
indexDocument = 'index.html',
|
|
237
|
+
errorDocument = 'error.html',
|
|
238
|
+
): S3Bucket {
|
|
239
|
+
if (!bucket.Properties) {
|
|
240
|
+
bucket.Properties = {}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
bucket.Properties.WebsiteConfiguration = {
|
|
244
|
+
IndexDocument: indexDocument,
|
|
245
|
+
ErrorDocument: errorDocument,
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return bucket
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Set lifecycle rules on an existing bucket
|
|
253
|
+
*/
|
|
254
|
+
static setLifecycleRules(bucket: S3Bucket, rules: LifecycleRule[]): S3Bucket {
|
|
255
|
+
if (!bucket.Properties) {
|
|
256
|
+
bucket.Properties = {}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
bucket.Properties.LifecycleConfiguration = {
|
|
260
|
+
Rules: rules.map(rule => ({
|
|
261
|
+
Id: rule.id,
|
|
262
|
+
Status: rule.enabled ? 'Enabled' : 'Disabled',
|
|
263
|
+
ExpirationInDays: rule.expirationDays,
|
|
264
|
+
Transitions: rule.transitions?.map(t => ({
|
|
265
|
+
TransitionInDays: t.days,
|
|
266
|
+
StorageClass: t.storageClass,
|
|
267
|
+
})),
|
|
268
|
+
})),
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return bucket
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Enable intelligent tiering on an existing bucket
|
|
276
|
+
*/
|
|
277
|
+
static enableIntelligentTiering(bucket: S3Bucket): S3Bucket {
|
|
278
|
+
if (!bucket.Properties) {
|
|
279
|
+
bucket.Properties = {}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (!bucket.Properties.LifecycleConfiguration) {
|
|
283
|
+
bucket.Properties.LifecycleConfiguration = { Rules: [] }
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
bucket.Properties.LifecycleConfiguration.Rules.push({
|
|
287
|
+
Id: 'IntelligentTieringRule',
|
|
288
|
+
Status: 'Enabled',
|
|
289
|
+
Transitions: [{
|
|
290
|
+
TransitionInDays: 0,
|
|
291
|
+
StorageClass: 'INTELLIGENT_TIERING',
|
|
292
|
+
}],
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
return bucket
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Add Lambda notification to bucket
|
|
300
|
+
*/
|
|
301
|
+
static addLambdaNotification(bucket: S3Bucket, config: S3NotificationConfig): S3Bucket {
|
|
302
|
+
if (!bucket.Properties) {
|
|
303
|
+
bucket.Properties = {}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (!bucket.Properties.NotificationConfiguration) {
|
|
307
|
+
bucket.Properties.NotificationConfiguration = {}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (!bucket.Properties.NotificationConfiguration.LambdaConfigurations) {
|
|
311
|
+
bucket.Properties.NotificationConfiguration.LambdaConfigurations = []
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const lambdaConfig: any = {
|
|
315
|
+
Event: config.events[0], // S3 requires single event per config
|
|
316
|
+
Function: config.functionArn,
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (config.filter) {
|
|
320
|
+
lambdaConfig.Filter = {
|
|
321
|
+
S3Key: {
|
|
322
|
+
Rules: [
|
|
323
|
+
...(config.filter.prefix ? [{ Name: 'prefix', Value: config.filter.prefix }] : []),
|
|
324
|
+
...(config.filter.suffix ? [{ Name: 'suffix', Value: config.filter.suffix }] : []),
|
|
325
|
+
],
|
|
326
|
+
},
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Add a configuration for each event type
|
|
331
|
+
for (const event of config.events) {
|
|
332
|
+
const eventConfig = { ...lambdaConfig, Event: event }
|
|
333
|
+
bucket.Properties.NotificationConfiguration.LambdaConfigurations.push(eventConfig)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return bucket
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Common notification configurations
|
|
341
|
+
*/
|
|
342
|
+
static readonly Notifications = {
|
|
343
|
+
/**
|
|
344
|
+
* Trigger Lambda on any object creation
|
|
345
|
+
*/
|
|
346
|
+
onObjectCreated: (functionArn: string | { 'Fn::GetAtt': [string, string] }): {
|
|
347
|
+
functionArn: string | { 'Fn::GetAtt': [string, string] }
|
|
348
|
+
events: readonly ['s3:ObjectCreated:*']
|
|
349
|
+
} => ({
|
|
350
|
+
functionArn: functionArn,
|
|
351
|
+
events: ['s3:ObjectCreated:*'] as const,
|
|
352
|
+
}),
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Trigger Lambda on object deletion
|
|
356
|
+
*/
|
|
357
|
+
onObjectRemoved: (functionArn: string | { 'Fn::GetAtt': [string, string] }): {
|
|
358
|
+
functionArn: string | { 'Fn::GetAtt': [string, string] }
|
|
359
|
+
events: readonly ['s3:ObjectRemoved:*']
|
|
360
|
+
} => ({
|
|
361
|
+
functionArn: functionArn,
|
|
362
|
+
events: ['s3:ObjectRemoved:*'] as const,
|
|
363
|
+
}),
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Trigger Lambda on image uploads (jpg, png, gif)
|
|
367
|
+
*/
|
|
368
|
+
onImageUpload: (functionArn: string | { 'Fn::GetAtt': [string, string] }, prefix?: string): {
|
|
369
|
+
functionArn: string | { 'Fn::GetAtt': [string, string] }
|
|
370
|
+
events: readonly ['s3:ObjectCreated:*']
|
|
371
|
+
filter: { prefix: string | undefined; suffix: string }
|
|
372
|
+
} => ({
|
|
373
|
+
functionArn: functionArn,
|
|
374
|
+
events: ['s3:ObjectCreated:*'] as const,
|
|
375
|
+
filter: {
|
|
376
|
+
prefix: prefix,
|
|
377
|
+
suffix: '.jpg',
|
|
378
|
+
},
|
|
379
|
+
}),
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Trigger Lambda on specific file type
|
|
383
|
+
*/
|
|
384
|
+
onFileType: (
|
|
385
|
+
functionArn: string | { 'Fn::GetAtt': [string, string] },
|
|
386
|
+
suffix: string,
|
|
387
|
+
prefix?: string,
|
|
388
|
+
): {
|
|
389
|
+
functionArn: string | { 'Fn::GetAtt': [string, string] }
|
|
390
|
+
events: readonly ['s3:ObjectCreated:*']
|
|
391
|
+
filter: { prefix: string | undefined; suffix: string }
|
|
392
|
+
} => ({
|
|
393
|
+
functionArn: functionArn,
|
|
394
|
+
events: ['s3:ObjectCreated:*'] as const,
|
|
395
|
+
filter: {
|
|
396
|
+
prefix: prefix,
|
|
397
|
+
suffix: suffix,
|
|
398
|
+
},
|
|
399
|
+
}),
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Trigger Lambda on uploads to specific folder
|
|
403
|
+
*/
|
|
404
|
+
onFolderUpload: (
|
|
405
|
+
functionArn: string | { 'Fn::GetAtt': [string, string] },
|
|
406
|
+
folder: string,
|
|
407
|
+
): {
|
|
408
|
+
functionArn: string | { 'Fn::GetAtt': [string, string] }
|
|
409
|
+
events: readonly ['s3:ObjectCreated:*']
|
|
410
|
+
filter: { prefix: string }
|
|
411
|
+
} => ({
|
|
412
|
+
functionArn: functionArn,
|
|
413
|
+
events: ['s3:ObjectCreated:*'] as const,
|
|
414
|
+
filter: {
|
|
415
|
+
prefix: folder.endsWith('/') ? folder : `${folder}/`,
|
|
416
|
+
},
|
|
417
|
+
}),
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Create an AWS Backup plan for S3 buckets
|
|
422
|
+
*/
|
|
423
|
+
static createBackupPlan(options: BackupPlanOptions): {
|
|
424
|
+
vault: BackupVault
|
|
425
|
+
plan: BackupPlan
|
|
426
|
+
selection: BackupSelection
|
|
427
|
+
role: IAMRole
|
|
428
|
+
vaultLogicalId: string
|
|
429
|
+
planLogicalId: string
|
|
430
|
+
selectionLogicalId: string
|
|
431
|
+
roleLogicalId: string
|
|
432
|
+
} {
|
|
433
|
+
const {
|
|
434
|
+
name,
|
|
435
|
+
slug,
|
|
436
|
+
environment,
|
|
437
|
+
bucketLogicalIds,
|
|
438
|
+
retentionDays,
|
|
439
|
+
schedule = 'cron(0 5 * * ? *)', // Daily at 5am UTC
|
|
440
|
+
vaultName,
|
|
441
|
+
enableContinuousBackup = false,
|
|
442
|
+
moveToColdStorageAfterDays,
|
|
443
|
+
} = options
|
|
444
|
+
|
|
445
|
+
// Create backup vault
|
|
446
|
+
const vaultResourceName = vaultName || generateResourceName({
|
|
447
|
+
slug,
|
|
448
|
+
environment,
|
|
449
|
+
resourceType: 'backup-vault',
|
|
450
|
+
suffix: name,
|
|
451
|
+
})
|
|
452
|
+
|
|
453
|
+
const vaultLogicalId = generateLogicalId(vaultResourceName)
|
|
454
|
+
|
|
455
|
+
const vault: BackupVault = {
|
|
456
|
+
Type: 'AWS::Backup::BackupVault',
|
|
457
|
+
Properties: {
|
|
458
|
+
BackupVaultName: vaultResourceName,
|
|
459
|
+
},
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// Create IAM role for AWS Backup
|
|
463
|
+
const roleResourceName = generateResourceName({
|
|
464
|
+
slug,
|
|
465
|
+
environment,
|
|
466
|
+
resourceType: 'backup-role',
|
|
467
|
+
suffix: name,
|
|
468
|
+
})
|
|
469
|
+
|
|
470
|
+
const roleLogicalId = generateLogicalId(roleResourceName)
|
|
471
|
+
|
|
472
|
+
const role: IAMRole = {
|
|
473
|
+
Type: 'AWS::IAM::Role',
|
|
474
|
+
Properties: {
|
|
475
|
+
RoleName: roleResourceName,
|
|
476
|
+
AssumeRolePolicyDocument: {
|
|
477
|
+
Version: '2012-10-17',
|
|
478
|
+
Statement: [{
|
|
479
|
+
Effect: 'Allow',
|
|
480
|
+
Principal: {
|
|
481
|
+
Service: 'backup.amazonaws.com',
|
|
482
|
+
},
|
|
483
|
+
Action: 'sts:AssumeRole',
|
|
484
|
+
}],
|
|
485
|
+
},
|
|
486
|
+
ManagedPolicyArns: [
|
|
487
|
+
'arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup',
|
|
488
|
+
'arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForRestores',
|
|
489
|
+
],
|
|
490
|
+
},
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// Create backup plan
|
|
494
|
+
const planResourceName = generateResourceName({
|
|
495
|
+
slug,
|
|
496
|
+
environment,
|
|
497
|
+
resourceType: 'backup-plan',
|
|
498
|
+
suffix: name,
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
const planLogicalId = generateLogicalId(planResourceName)
|
|
502
|
+
|
|
503
|
+
const lifecycle: any = {
|
|
504
|
+
DeleteAfterDays: retentionDays,
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (moveToColdStorageAfterDays && moveToColdStorageAfterDays < retentionDays) {
|
|
508
|
+
lifecycle.MoveToColdStorageAfterDays = moveToColdStorageAfterDays
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const plan: BackupPlan = {
|
|
512
|
+
Type: 'AWS::Backup::BackupPlan',
|
|
513
|
+
Properties: {
|
|
514
|
+
BackupPlan: {
|
|
515
|
+
BackupPlanName: planResourceName,
|
|
516
|
+
BackupPlanRule: [{
|
|
517
|
+
RuleName: `${name}-daily-backup`,
|
|
518
|
+
TargetBackupVault: Fn.Ref(vaultLogicalId) as any,
|
|
519
|
+
ScheduleExpression: schedule,
|
|
520
|
+
StartWindowMinutes: 60,
|
|
521
|
+
CompletionWindowMinutes: 120,
|
|
522
|
+
Lifecycle: lifecycle,
|
|
523
|
+
EnableContinuousBackup: enableContinuousBackup,
|
|
524
|
+
}],
|
|
525
|
+
},
|
|
526
|
+
},
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// Create backup selection
|
|
530
|
+
const selectionResourceName = generateResourceName({
|
|
531
|
+
slug,
|
|
532
|
+
environment,
|
|
533
|
+
resourceType: 'backup-selection',
|
|
534
|
+
suffix: name,
|
|
535
|
+
})
|
|
536
|
+
|
|
537
|
+
const selectionLogicalId = generateLogicalId(selectionResourceName)
|
|
538
|
+
|
|
539
|
+
// Build bucket ARNs from logical IDs
|
|
540
|
+
const bucketArns = bucketLogicalIds.map(logicalId =>
|
|
541
|
+
Fn.GetAtt(logicalId, 'Arn') as any,
|
|
542
|
+
)
|
|
543
|
+
|
|
544
|
+
const selection: BackupSelection = {
|
|
545
|
+
Type: 'AWS::Backup::BackupSelection',
|
|
546
|
+
Properties: {
|
|
547
|
+
BackupPlanId: Fn.Ref(planLogicalId) as any,
|
|
548
|
+
BackupSelection: {
|
|
549
|
+
SelectionName: selectionResourceName,
|
|
550
|
+
IamRoleArn: Fn.GetAtt(roleLogicalId, 'Arn') as any,
|
|
551
|
+
Resources: bucketArns,
|
|
552
|
+
},
|
|
553
|
+
},
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
return {
|
|
557
|
+
vault,
|
|
558
|
+
plan,
|
|
559
|
+
selection,
|
|
560
|
+
role,
|
|
561
|
+
vaultLogicalId,
|
|
562
|
+
planLogicalId,
|
|
563
|
+
selectionLogicalId,
|
|
564
|
+
roleLogicalId,
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Common backup schedule expressions
|
|
570
|
+
*/
|
|
571
|
+
static readonly BackupSchedules = {
|
|
572
|
+
HOURLY: 'cron(0 * * * ? *)',
|
|
573
|
+
DAILY_5AM: 'cron(0 5 * * ? *)',
|
|
574
|
+
DAILY_MIDNIGHT: 'cron(0 0 * * ? *)',
|
|
575
|
+
WEEKLY_SUNDAY: 'cron(0 5 ? * SUN *)',
|
|
576
|
+
WEEKLY_SATURDAY: 'cron(0 5 ? * SAT *)',
|
|
577
|
+
MONTHLY_FIRST: 'cron(0 5 1 * ? *)',
|
|
578
|
+
EVERY_12_HOURS: 'cron(0 */12 * * ? *)',
|
|
579
|
+
EVERY_6_HOURS: 'cron(0 */6 * * ? *)',
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Common backup retention periods (in days)
|
|
584
|
+
*/
|
|
585
|
+
static readonly BackupRetention = {
|
|
586
|
+
ONE_DAY: 1,
|
|
587
|
+
ONE_WEEK: 7,
|
|
588
|
+
TWO_WEEKS: 14,
|
|
589
|
+
ONE_MONTH: 30,
|
|
590
|
+
THREE_MONTHS: 90,
|
|
591
|
+
SIX_MONTHS: 180,
|
|
592
|
+
ONE_YEAR: 365,
|
|
593
|
+
TWO_YEARS: 730,
|
|
594
|
+
FIVE_YEARS: 1825,
|
|
595
|
+
SEVEN_YEARS: 2555,
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Create a www redirect bucket
|
|
600
|
+
* This bucket redirects www.domain.com to domain.com (or vice versa)
|
|
601
|
+
*/
|
|
602
|
+
static createWwwRedirectBucket(options: {
|
|
603
|
+
slug: string
|
|
604
|
+
environment: EnvironmentType
|
|
605
|
+
sourceDomain: string // e.g., www.example.com
|
|
606
|
+
targetDomain: string // e.g., example.com
|
|
607
|
+
protocol?: 'http' | 'https'
|
|
608
|
+
}): { bucket: S3Bucket, bucketPolicy: S3BucketPolicy, logicalId: string } {
|
|
609
|
+
const {
|
|
610
|
+
slug,
|
|
611
|
+
environment,
|
|
612
|
+
sourceDomain,
|
|
613
|
+
targetDomain,
|
|
614
|
+
protocol = 'https',
|
|
615
|
+
} = options
|
|
616
|
+
|
|
617
|
+
const resourceName = generateResourceName({
|
|
618
|
+
slug,
|
|
619
|
+
environment,
|
|
620
|
+
resourceType: 's3',
|
|
621
|
+
suffix: 'www-redirect',
|
|
622
|
+
})
|
|
623
|
+
|
|
624
|
+
const logicalId = generateLogicalId(resourceName)
|
|
625
|
+
|
|
626
|
+
const bucket: S3Bucket = {
|
|
627
|
+
Type: 'AWS::S3::Bucket',
|
|
628
|
+
Properties: {
|
|
629
|
+
BucketName: sourceDomain,
|
|
630
|
+
WebsiteConfiguration: {
|
|
631
|
+
RedirectAllRequestsTo: {
|
|
632
|
+
HostName: targetDomain,
|
|
633
|
+
Protocol: protocol,
|
|
634
|
+
},
|
|
635
|
+
},
|
|
636
|
+
PublicAccessBlockConfiguration: {
|
|
637
|
+
BlockPublicAcls: false,
|
|
638
|
+
BlockPublicPolicy: false,
|
|
639
|
+
IgnorePublicAcls: false,
|
|
640
|
+
RestrictPublicBuckets: false,
|
|
641
|
+
},
|
|
642
|
+
},
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// Policy to allow public read access for redirect
|
|
646
|
+
const bucketPolicy: S3BucketPolicy = {
|
|
647
|
+
Type: 'AWS::S3::BucketPolicy',
|
|
648
|
+
Properties: {
|
|
649
|
+
Bucket: Fn.Ref(logicalId),
|
|
650
|
+
PolicyDocument: {
|
|
651
|
+
Version: '2012-10-17',
|
|
652
|
+
Statement: [{
|
|
653
|
+
Sid: 'PublicReadForRedirect',
|
|
654
|
+
Effect: 'Allow',
|
|
655
|
+
Principal: '*',
|
|
656
|
+
Action: ['s3:GetObject'],
|
|
657
|
+
Resource: [Fn.Join('', [Fn.GetAtt(logicalId, 'Arn'), '/*']) as any],
|
|
658
|
+
}],
|
|
659
|
+
},
|
|
660
|
+
},
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
return {
|
|
664
|
+
bucket,
|
|
665
|
+
bucketPolicy,
|
|
666
|
+
logicalId,
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Create a docs bucket (for documentation sites)
|
|
672
|
+
* Conditional creation based on docs presence
|
|
673
|
+
*/
|
|
674
|
+
static createDocsBucket(options: {
|
|
675
|
+
slug: string
|
|
676
|
+
environment: EnvironmentType
|
|
677
|
+
domain?: string
|
|
678
|
+
}): { bucket: S3Bucket, bucketPolicy?: S3BucketPolicy, logicalId: string } {
|
|
679
|
+
const {
|
|
680
|
+
slug,
|
|
681
|
+
environment,
|
|
682
|
+
domain,
|
|
683
|
+
} = options
|
|
684
|
+
|
|
685
|
+
const resourceName = domain || generateResourceName({
|
|
686
|
+
slug,
|
|
687
|
+
environment,
|
|
688
|
+
resourceType: 's3',
|
|
689
|
+
suffix: 'docs',
|
|
690
|
+
})
|
|
691
|
+
|
|
692
|
+
const logicalId = generateLogicalId(resourceName)
|
|
693
|
+
|
|
694
|
+
const bucket: S3Bucket = {
|
|
695
|
+
Type: 'AWS::S3::Bucket',
|
|
696
|
+
Properties: {
|
|
697
|
+
BucketName: resourceName,
|
|
698
|
+
BucketEncryption: {
|
|
699
|
+
ServerSideEncryptionConfiguration: [{
|
|
700
|
+
ServerSideEncryptionByDefault: {
|
|
701
|
+
SSEAlgorithm: 'AES256',
|
|
702
|
+
},
|
|
703
|
+
}],
|
|
704
|
+
},
|
|
705
|
+
PublicAccessBlockConfiguration: {
|
|
706
|
+
BlockPublicAcls: true,
|
|
707
|
+
BlockPublicPolicy: true,
|
|
708
|
+
IgnorePublicAcls: true,
|
|
709
|
+
RestrictPublicBuckets: true,
|
|
710
|
+
},
|
|
711
|
+
Tags: [{
|
|
712
|
+
Key: 'backup',
|
|
713
|
+
Value: 'weekly',
|
|
714
|
+
}],
|
|
715
|
+
},
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
return {
|
|
719
|
+
bucket,
|
|
720
|
+
logicalId,
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* Create an email bucket for storing emails
|
|
726
|
+
*/
|
|
727
|
+
static createEmailBucket(options: {
|
|
728
|
+
slug: string
|
|
729
|
+
environment: EnvironmentType
|
|
730
|
+
}): { bucket: S3Bucket, bucketPolicy: S3BucketPolicy, logicalId: string } {
|
|
731
|
+
const {
|
|
732
|
+
slug,
|
|
733
|
+
environment,
|
|
734
|
+
} = options
|
|
735
|
+
|
|
736
|
+
const resourceName = generateResourceName({
|
|
737
|
+
slug,
|
|
738
|
+
environment,
|
|
739
|
+
resourceType: 's3',
|
|
740
|
+
suffix: 'email',
|
|
741
|
+
})
|
|
742
|
+
|
|
743
|
+
const logicalId = generateLogicalId(resourceName)
|
|
744
|
+
|
|
745
|
+
const bucket: S3Bucket = {
|
|
746
|
+
Type: 'AWS::S3::Bucket',
|
|
747
|
+
Properties: {
|
|
748
|
+
BucketName: resourceName,
|
|
749
|
+
BucketEncryption: {
|
|
750
|
+
ServerSideEncryptionConfiguration: [{
|
|
751
|
+
ServerSideEncryptionByDefault: {
|
|
752
|
+
SSEAlgorithm: 'AES256',
|
|
753
|
+
},
|
|
754
|
+
}],
|
|
755
|
+
},
|
|
756
|
+
PublicAccessBlockConfiguration: {
|
|
757
|
+
BlockPublicAcls: true,
|
|
758
|
+
BlockPublicPolicy: true,
|
|
759
|
+
IgnorePublicAcls: true,
|
|
760
|
+
RestrictPublicBuckets: true,
|
|
761
|
+
},
|
|
762
|
+
LifecycleConfiguration: {
|
|
763
|
+
Rules: [{
|
|
764
|
+
Id: 'EmailRetention',
|
|
765
|
+
Status: 'Enabled',
|
|
766
|
+
ExpirationInDays: 90, // Keep emails for 90 days
|
|
767
|
+
Transitions: [{
|
|
768
|
+
TransitionInDays: 30,
|
|
769
|
+
StorageClass: 'STANDARD_IA', // Move to IA after 30 days
|
|
770
|
+
}],
|
|
771
|
+
}],
|
|
772
|
+
},
|
|
773
|
+
},
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
// Policy to allow SES to deliver emails to this bucket
|
|
777
|
+
const bucketPolicy: S3BucketPolicy = {
|
|
778
|
+
Type: 'AWS::S3::BucketPolicy',
|
|
779
|
+
Properties: {
|
|
780
|
+
Bucket: Fn.Ref(logicalId),
|
|
781
|
+
PolicyDocument: {
|
|
782
|
+
Version: '2012-10-17',
|
|
783
|
+
Statement: [{
|
|
784
|
+
Sid: 'AllowSESPuts',
|
|
785
|
+
Effect: 'Allow',
|
|
786
|
+
Principal: {
|
|
787
|
+
Service: 'ses.amazonaws.com',
|
|
788
|
+
},
|
|
789
|
+
Action: 's3:PutObject',
|
|
790
|
+
Resource: Fn.Join('', [Fn.GetAtt(logicalId, 'Arn'), '/*']) as any,
|
|
791
|
+
Condition: {
|
|
792
|
+
StringEquals: {
|
|
793
|
+
'AWS:SourceAccount': Fn.Ref('AWS::AccountId') as any,
|
|
794
|
+
},
|
|
795
|
+
},
|
|
796
|
+
}],
|
|
797
|
+
},
|
|
798
|
+
},
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
return {
|
|
802
|
+
bucket,
|
|
803
|
+
bucketPolicy,
|
|
804
|
+
logicalId,
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
/**
|
|
809
|
+
* Check if a docs directory exists and has content
|
|
810
|
+
*/
|
|
811
|
+
static docsExist(options: {
|
|
812
|
+
projectRoot?: string
|
|
813
|
+
docsPaths?: string[]
|
|
814
|
+
} = {}): {
|
|
815
|
+
exists: boolean
|
|
816
|
+
path: string | null
|
|
817
|
+
hasDistFolder: boolean
|
|
818
|
+
distPath: string | null
|
|
819
|
+
} {
|
|
820
|
+
const {
|
|
821
|
+
projectRoot = process.cwd(),
|
|
822
|
+
docsPaths = ['docs', 'documentation', 'doc'],
|
|
823
|
+
} = options
|
|
824
|
+
|
|
825
|
+
for (const docsPath of docsPaths) {
|
|
826
|
+
const fullPath = join(projectRoot, docsPath)
|
|
827
|
+
|
|
828
|
+
if (existsSync(fullPath)) {
|
|
829
|
+
// Check for dist/build folder within docs
|
|
830
|
+
const distPaths = ['dist', 'build', '.vitepress/dist', '_site', 'out', 'public']
|
|
831
|
+
|
|
832
|
+
for (const distPath of distPaths) {
|
|
833
|
+
const fullDistPath = join(fullPath, distPath)
|
|
834
|
+
if (existsSync(fullDistPath)) {
|
|
835
|
+
// Verify it has files
|
|
836
|
+
try {
|
|
837
|
+
const files = readdirSync(fullDistPath)
|
|
838
|
+
if (files.length > 0) {
|
|
839
|
+
return {
|
|
840
|
+
exists: true,
|
|
841
|
+
path: fullPath,
|
|
842
|
+
hasDistFolder: true,
|
|
843
|
+
distPath: fullDistPath,
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
catch {
|
|
848
|
+
// Continue to next path
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
// Docs folder exists but no dist yet
|
|
854
|
+
return {
|
|
855
|
+
exists: true,
|
|
856
|
+
path: fullPath,
|
|
857
|
+
hasDistFolder: false,
|
|
858
|
+
distPath: null,
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
return {
|
|
864
|
+
exists: false,
|
|
865
|
+
path: null,
|
|
866
|
+
hasDistFolder: false,
|
|
867
|
+
distPath: null,
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* Conditionally create docs bucket if docs exist
|
|
873
|
+
*/
|
|
874
|
+
static createDocsBucketIfExists(options: {
|
|
875
|
+
slug: string
|
|
876
|
+
environment: EnvironmentType
|
|
877
|
+
domain?: string
|
|
878
|
+
projectRoot?: string
|
|
879
|
+
docsPaths?: string[]
|
|
880
|
+
}): {
|
|
881
|
+
bucket: S3Bucket | null
|
|
882
|
+
bucketPolicy?: S3BucketPolicy
|
|
883
|
+
logicalId: string | null
|
|
884
|
+
docsInfo: {
|
|
885
|
+
exists: boolean
|
|
886
|
+
path: string | null
|
|
887
|
+
hasDistFolder: boolean
|
|
888
|
+
distPath: string | null
|
|
889
|
+
}
|
|
890
|
+
} {
|
|
891
|
+
const docsInfo = Storage.docsExist({
|
|
892
|
+
projectRoot: options.projectRoot,
|
|
893
|
+
docsPaths: options.docsPaths,
|
|
894
|
+
})
|
|
895
|
+
|
|
896
|
+
if (!docsInfo.exists) {
|
|
897
|
+
return {
|
|
898
|
+
bucket: null,
|
|
899
|
+
logicalId: null,
|
|
900
|
+
docsInfo,
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
const result = Storage.createDocsBucket({
|
|
905
|
+
slug: options.slug,
|
|
906
|
+
environment: options.environment,
|
|
907
|
+
domain: options.domain,
|
|
908
|
+
})
|
|
909
|
+
|
|
910
|
+
return {
|
|
911
|
+
bucket: result.bucket,
|
|
912
|
+
bucketPolicy: result.bucketPolicy,
|
|
913
|
+
logicalId: result.logicalId,
|
|
914
|
+
docsInfo,
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Create private files bucket
|
|
920
|
+
* For storing private/sensitive files not accessible publicly
|
|
921
|
+
*/
|
|
922
|
+
static createPrivateBucket(options: {
|
|
923
|
+
slug: string
|
|
924
|
+
environment: EnvironmentType
|
|
925
|
+
enableVersioning?: boolean
|
|
926
|
+
retentionDays?: number
|
|
927
|
+
encryptionKeyArn?: string
|
|
928
|
+
}): { bucket: S3Bucket, logicalId: string } {
|
|
929
|
+
const {
|
|
930
|
+
slug,
|
|
931
|
+
environment,
|
|
932
|
+
enableVersioning = true,
|
|
933
|
+
retentionDays,
|
|
934
|
+
encryptionKeyArn,
|
|
935
|
+
} = options
|
|
936
|
+
|
|
937
|
+
const resourceName = generateResourceName({
|
|
938
|
+
slug,
|
|
939
|
+
environment,
|
|
940
|
+
resourceType: 's3',
|
|
941
|
+
suffix: 'private',
|
|
942
|
+
})
|
|
943
|
+
|
|
944
|
+
const logicalId = generateLogicalId(resourceName)
|
|
945
|
+
|
|
946
|
+
const bucket: S3Bucket = {
|
|
947
|
+
Type: 'AWS::S3::Bucket',
|
|
948
|
+
Properties: {
|
|
949
|
+
BucketName: resourceName,
|
|
950
|
+
BucketEncryption: {
|
|
951
|
+
ServerSideEncryptionConfiguration: [{
|
|
952
|
+
ServerSideEncryptionByDefault: encryptionKeyArn
|
|
953
|
+
? {
|
|
954
|
+
SSEAlgorithm: 'aws:kms',
|
|
955
|
+
KMSMasterKeyID: encryptionKeyArn,
|
|
956
|
+
}
|
|
957
|
+
: {
|
|
958
|
+
SSEAlgorithm: 'AES256',
|
|
959
|
+
},
|
|
960
|
+
}],
|
|
961
|
+
},
|
|
962
|
+
PublicAccessBlockConfiguration: {
|
|
963
|
+
BlockPublicAcls: true,
|
|
964
|
+
BlockPublicPolicy: true,
|
|
965
|
+
IgnorePublicAcls: true,
|
|
966
|
+
RestrictPublicBuckets: true,
|
|
967
|
+
},
|
|
968
|
+
VersioningConfiguration: enableVersioning
|
|
969
|
+
? { Status: 'Enabled' }
|
|
970
|
+
: undefined,
|
|
971
|
+
LifecycleConfiguration: retentionDays
|
|
972
|
+
? {
|
|
973
|
+
Rules: [{
|
|
974
|
+
Id: 'RetentionPolicy',
|
|
975
|
+
Status: 'Enabled',
|
|
976
|
+
ExpirationInDays: retentionDays,
|
|
977
|
+
}],
|
|
978
|
+
}
|
|
979
|
+
: undefined,
|
|
980
|
+
Tags: [{
|
|
981
|
+
Key: 'backup',
|
|
982
|
+
Value: 'daily',
|
|
983
|
+
}],
|
|
984
|
+
},
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
return {
|
|
988
|
+
bucket,
|
|
989
|
+
logicalId,
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* Check if source paths exist for deployment
|
|
995
|
+
* Common paths: views/web/dist, docs/dist, private
|
|
996
|
+
*/
|
|
997
|
+
static checkSourcePaths(options: {
|
|
998
|
+
projectRoot?: string
|
|
999
|
+
paths?: {
|
|
1000
|
+
web?: string
|
|
1001
|
+
docs?: string
|
|
1002
|
+
private?: string
|
|
1003
|
+
}
|
|
1004
|
+
} = {}): {
|
|
1005
|
+
web: { exists: boolean, path: string }
|
|
1006
|
+
docs: { exists: boolean, path: string }
|
|
1007
|
+
private: { exists: boolean, path: string }
|
|
1008
|
+
} {
|
|
1009
|
+
const {
|
|
1010
|
+
projectRoot = process.cwd(),
|
|
1011
|
+
paths = {},
|
|
1012
|
+
} = options
|
|
1013
|
+
|
|
1014
|
+
const webPath = paths.web || 'views/web/dist'
|
|
1015
|
+
const docsPath = paths.docs || 'docs/dist'
|
|
1016
|
+
const privatePath = paths.private || 'private'
|
|
1017
|
+
|
|
1018
|
+
return {
|
|
1019
|
+
web: {
|
|
1020
|
+
exists: existsSync(join(projectRoot, webPath)),
|
|
1021
|
+
path: join(projectRoot, webPath),
|
|
1022
|
+
},
|
|
1023
|
+
docs: {
|
|
1024
|
+
exists: existsSync(join(projectRoot, docsPath)),
|
|
1025
|
+
path: join(projectRoot, docsPath),
|
|
1026
|
+
},
|
|
1027
|
+
private: {
|
|
1028
|
+
exists: existsSync(join(projectRoot, privatePath)),
|
|
1029
|
+
path: join(projectRoot, privatePath),
|
|
1030
|
+
},
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
/**
|
|
1035
|
+
* Create all deployment buckets based on source paths
|
|
1036
|
+
* Conditionally creates buckets only for source paths that exist
|
|
1037
|
+
*/
|
|
1038
|
+
static createDeploymentBuckets(options: {
|
|
1039
|
+
slug: string
|
|
1040
|
+
environment: EnvironmentType
|
|
1041
|
+
domain?: string
|
|
1042
|
+
projectRoot?: string
|
|
1043
|
+
paths?: {
|
|
1044
|
+
web?: string
|
|
1045
|
+
docs?: string
|
|
1046
|
+
private?: string
|
|
1047
|
+
}
|
|
1048
|
+
}): {
|
|
1049
|
+
resources: Record<string, S3Bucket | S3BucketPolicy>
|
|
1050
|
+
created: {
|
|
1051
|
+
web: boolean
|
|
1052
|
+
docs: boolean
|
|
1053
|
+
private: boolean
|
|
1054
|
+
}
|
|
1055
|
+
sourcePaths: {
|
|
1056
|
+
web: { exists: boolean, path: string }
|
|
1057
|
+
docs: { exists: boolean, path: string }
|
|
1058
|
+
private: { exists: boolean, path: string }
|
|
1059
|
+
}
|
|
1060
|
+
} {
|
|
1061
|
+
const {
|
|
1062
|
+
slug,
|
|
1063
|
+
environment,
|
|
1064
|
+
domain,
|
|
1065
|
+
projectRoot,
|
|
1066
|
+
paths,
|
|
1067
|
+
} = options
|
|
1068
|
+
|
|
1069
|
+
const sourcePaths = Storage.checkSourcePaths({ projectRoot, paths })
|
|
1070
|
+
const resources: Record<string, S3Bucket | S3BucketPolicy> = {}
|
|
1071
|
+
const created = {
|
|
1072
|
+
web: false,
|
|
1073
|
+
docs: false,
|
|
1074
|
+
private: false,
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
// Create web bucket (always created for main site)
|
|
1078
|
+
const webBucket = Storage.createBucket({
|
|
1079
|
+
name: 'web',
|
|
1080
|
+
slug,
|
|
1081
|
+
environment,
|
|
1082
|
+
public: false,
|
|
1083
|
+
versioning: false,
|
|
1084
|
+
encryption: true,
|
|
1085
|
+
})
|
|
1086
|
+
resources[webBucket.logicalId] = webBucket.bucket
|
|
1087
|
+
created.web = true
|
|
1088
|
+
|
|
1089
|
+
// Create docs bucket if docs exist
|
|
1090
|
+
if (sourcePaths.docs.exists) {
|
|
1091
|
+
const docsBucket = Storage.createDocsBucket({
|
|
1092
|
+
slug,
|
|
1093
|
+
environment,
|
|
1094
|
+
domain: domain ? `docs.${domain}` : undefined,
|
|
1095
|
+
})
|
|
1096
|
+
resources[docsBucket.logicalId] = docsBucket.bucket
|
|
1097
|
+
if (docsBucket.bucketPolicy) {
|
|
1098
|
+
resources[`${docsBucket.logicalId}Policy`] = docsBucket.bucketPolicy
|
|
1099
|
+
}
|
|
1100
|
+
created.docs = true
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
// Create private bucket if private files exist
|
|
1104
|
+
if (sourcePaths.private.exists) {
|
|
1105
|
+
const privateBucket = Storage.createPrivateBucket({
|
|
1106
|
+
slug,
|
|
1107
|
+
environment,
|
|
1108
|
+
enableVersioning: true,
|
|
1109
|
+
})
|
|
1110
|
+
resources[privateBucket.logicalId] = privateBucket.bucket
|
|
1111
|
+
created.private = true
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
return {
|
|
1115
|
+
resources,
|
|
1116
|
+
created,
|
|
1117
|
+
sourcePaths,
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|