@stacksjs/ts-cloud-core 0.1.3 → 0.1.6

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.
Files changed (250) hide show
  1. package/README.md +98 -13
  2. package/package.json +12 -3
  3. package/src/advanced-features.test.ts +0 -465
  4. package/src/aws/cloudformation.ts +0 -421
  5. package/src/aws/cloudfront.ts +0 -158
  6. package/src/aws/credentials.test.ts +0 -132
  7. package/src/aws/credentials.ts +0 -545
  8. package/src/aws/index.ts +0 -87
  9. package/src/aws/s3.test.ts +0 -188
  10. package/src/aws/s3.ts +0 -1088
  11. package/src/aws/signature.test.ts +0 -670
  12. package/src/aws/signature.ts +0 -1155
  13. package/src/backup/disaster-recovery.test.ts +0 -726
  14. package/src/backup/disaster-recovery.ts +0 -500
  15. package/src/backup/index.ts +0 -34
  16. package/src/backup/manager.test.ts +0 -498
  17. package/src/backup/manager.ts +0 -432
  18. package/src/cicd/circleci.ts +0 -430
  19. package/src/cicd/github-actions.ts +0 -424
  20. package/src/cicd/gitlab-ci.ts +0 -255
  21. package/src/cicd/index.ts +0 -8
  22. package/src/cli/history.ts +0 -396
  23. package/src/cli/index.ts +0 -10
  24. package/src/cli/progress.ts +0 -458
  25. package/src/cli/repl.ts +0 -454
  26. package/src/cli/suggestions.ts +0 -327
  27. package/src/cli/table.test.ts +0 -319
  28. package/src/cli/table.ts +0 -332
  29. package/src/cloudformation/builder.test.ts +0 -327
  30. package/src/cloudformation/builder.ts +0 -378
  31. package/src/cloudformation/builders/api-gateway.ts +0 -449
  32. package/src/cloudformation/builders/cache.ts +0 -334
  33. package/src/cloudformation/builders/cdn.ts +0 -278
  34. package/src/cloudformation/builders/compute.ts +0 -485
  35. package/src/cloudformation/builders/database.ts +0 -392
  36. package/src/cloudformation/builders/functions.ts +0 -343
  37. package/src/cloudformation/builders/messaging.ts +0 -140
  38. package/src/cloudformation/builders/monitoring.ts +0 -300
  39. package/src/cloudformation/builders/network.ts +0 -264
  40. package/src/cloudformation/builders/queue.ts +0 -147
  41. package/src/cloudformation/builders/security.ts +0 -399
  42. package/src/cloudformation/builders/storage.ts +0 -285
  43. package/src/cloudformation/index.ts +0 -30
  44. package/src/cloudformation/types.ts +0 -173
  45. package/src/compliance/aws-config.ts +0 -543
  46. package/src/compliance/cloudtrail.ts +0 -376
  47. package/src/compliance/compliance.test.ts +0 -423
  48. package/src/compliance/guardduty.ts +0 -446
  49. package/src/compliance/index.ts +0 -66
  50. package/src/compliance/security-hub.ts +0 -456
  51. package/src/containers/build-optimization.ts +0 -416
  52. package/src/containers/containers.test.ts +0 -508
  53. package/src/containers/image-scanning.ts +0 -360
  54. package/src/containers/index.ts +0 -9
  55. package/src/containers/registry.ts +0 -293
  56. package/src/containers/service-mesh.ts +0 -520
  57. package/src/database/database.test.ts +0 -762
  58. package/src/database/index.ts +0 -9
  59. package/src/database/migrations.ts +0 -444
  60. package/src/database/performance.ts +0 -528
  61. package/src/database/replicas.ts +0 -534
  62. package/src/database/users.ts +0 -494
  63. package/src/dependency-graph.ts +0 -143
  64. package/src/deployment/ab-testing.ts +0 -582
  65. package/src/deployment/blue-green.ts +0 -452
  66. package/src/deployment/canary.ts +0 -500
  67. package/src/deployment/deployment.test.ts +0 -526
  68. package/src/deployment/index.ts +0 -61
  69. package/src/deployment/progressive.ts +0 -62
  70. package/src/dns/dns.test.ts +0 -641
  71. package/src/dns/dnssec.ts +0 -315
  72. package/src/dns/index.ts +0 -8
  73. package/src/dns/resolver.ts +0 -496
  74. package/src/dns/routing.ts +0 -593
  75. package/src/email/advanced/analytics.ts +0 -445
  76. package/src/email/advanced/index.ts +0 -11
  77. package/src/email/advanced/rules.ts +0 -465
  78. package/src/email/advanced/scheduling.ts +0 -352
  79. package/src/email/advanced/search.ts +0 -412
  80. package/src/email/advanced/shared-mailboxes.ts +0 -404
  81. package/src/email/advanced/templates.ts +0 -455
  82. package/src/email/advanced/threading.ts +0 -281
  83. package/src/email/analytics.ts +0 -467
  84. package/src/email/bounce-handling.ts +0 -425
  85. package/src/email/email.test.ts +0 -431
  86. package/src/email/handlers/__tests__/inbound.test.ts +0 -38
  87. package/src/email/handlers/__tests__/outbound.test.ts +0 -37
  88. package/src/email/handlers/converter.ts +0 -227
  89. package/src/email/handlers/feedback.ts +0 -228
  90. package/src/email/handlers/inbound.ts +0 -169
  91. package/src/email/handlers/outbound.ts +0 -178
  92. package/src/email/index.ts +0 -15
  93. package/src/email/reputation.ts +0 -303
  94. package/src/email/templates.ts +0 -352
  95. package/src/errors/index.test.ts +0 -434
  96. package/src/errors/index.ts +0 -416
  97. package/src/health-checks/index.ts +0 -40
  98. package/src/index.ts +0 -360
  99. package/src/intrinsic-functions.ts +0 -118
  100. package/src/lambda/concurrency.ts +0 -330
  101. package/src/lambda/destinations.ts +0 -345
  102. package/src/lambda/dlq.ts +0 -425
  103. package/src/lambda/index.ts +0 -11
  104. package/src/lambda/lambda.test.ts +0 -840
  105. package/src/lambda/layers.ts +0 -263
  106. package/src/lambda/versions.ts +0 -376
  107. package/src/lambda/vpc.ts +0 -399
  108. package/src/local/config.ts +0 -114
  109. package/src/local/index.ts +0 -6
  110. package/src/local/mock-aws.ts +0 -351
  111. package/src/modules/ai.ts +0 -340
  112. package/src/modules/api.ts +0 -478
  113. package/src/modules/auth.ts +0 -805
  114. package/src/modules/cache.ts +0 -417
  115. package/src/modules/cdn.ts +0 -1062
  116. package/src/modules/communication.ts +0 -1094
  117. package/src/modules/compute.ts +0 -3348
  118. package/src/modules/database.ts +0 -554
  119. package/src/modules/deployment.ts +0 -1079
  120. package/src/modules/dns.ts +0 -337
  121. package/src/modules/email.ts +0 -1538
  122. package/src/modules/filesystem.ts +0 -515
  123. package/src/modules/index.ts +0 -32
  124. package/src/modules/messaging.ts +0 -486
  125. package/src/modules/monitoring.ts +0 -2086
  126. package/src/modules/network.ts +0 -664
  127. package/src/modules/parameter-store.ts +0 -325
  128. package/src/modules/permissions.ts +0 -1081
  129. package/src/modules/phone.ts +0 -494
  130. package/src/modules/queue.ts +0 -1260
  131. package/src/modules/redirects.ts +0 -464
  132. package/src/modules/registry.ts +0 -699
  133. package/src/modules/search.ts +0 -401
  134. package/src/modules/secrets.ts +0 -416
  135. package/src/modules/security.ts +0 -731
  136. package/src/modules/sms.ts +0 -389
  137. package/src/modules/storage.ts +0 -1120
  138. package/src/modules/workflow.ts +0 -680
  139. package/src/multi-account/config.ts +0 -521
  140. package/src/multi-account/index.ts +0 -7
  141. package/src/multi-account/manager.ts +0 -427
  142. package/src/multi-region/cross-region.ts +0 -410
  143. package/src/multi-region/index.ts +0 -8
  144. package/src/multi-region/manager.ts +0 -483
  145. package/src/multi-region/regions.ts +0 -435
  146. package/src/network-security/index.ts +0 -48
  147. package/src/observability/index.ts +0 -9
  148. package/src/observability/logs.ts +0 -522
  149. package/src/observability/metrics.ts +0 -460
  150. package/src/observability/observability.test.ts +0 -782
  151. package/src/observability/synthetics.ts +0 -568
  152. package/src/observability/xray.ts +0 -358
  153. package/src/phone/advanced/analytics.ts +0 -349
  154. package/src/phone/advanced/callbacks.ts +0 -428
  155. package/src/phone/advanced/index.ts +0 -8
  156. package/src/phone/advanced/ivr-builder.ts +0 -504
  157. package/src/phone/advanced/recording.ts +0 -310
  158. package/src/phone/handlers/__tests__/incoming-call.test.ts +0 -40
  159. package/src/phone/handlers/incoming-call.ts +0 -117
  160. package/src/phone/handlers/missed-call.ts +0 -116
  161. package/src/phone/handlers/voicemail.ts +0 -179
  162. package/src/phone/index.ts +0 -9
  163. package/src/presets/api-backend.ts +0 -134
  164. package/src/presets/data-pipeline.ts +0 -204
  165. package/src/presets/extend.test.ts +0 -295
  166. package/src/presets/extend.ts +0 -297
  167. package/src/presets/fullstack-app.ts +0 -144
  168. package/src/presets/index.ts +0 -27
  169. package/src/presets/jamstack.ts +0 -135
  170. package/src/presets/microservices.ts +0 -167
  171. package/src/presets/ml-api.ts +0 -208
  172. package/src/presets/nodejs-server.ts +0 -104
  173. package/src/presets/nodejs-serverless.ts +0 -114
  174. package/src/presets/realtime-app.ts +0 -184
  175. package/src/presets/static-site.ts +0 -64
  176. package/src/presets/traditional-web-app.ts +0 -339
  177. package/src/presets/wordpress.ts +0 -138
  178. package/src/preview/github.test.ts +0 -249
  179. package/src/preview/github.ts +0 -297
  180. package/src/preview/index.ts +0 -37
  181. package/src/preview/manager.test.ts +0 -440
  182. package/src/preview/manager.ts +0 -326
  183. package/src/preview/notifications.test.ts +0 -582
  184. package/src/preview/notifications.ts +0 -341
  185. package/src/queue/batch-processing.ts +0 -402
  186. package/src/queue/dlq-monitoring.ts +0 -402
  187. package/src/queue/fifo.ts +0 -342
  188. package/src/queue/index.ts +0 -9
  189. package/src/queue/management.ts +0 -428
  190. package/src/queue/queue.test.ts +0 -429
  191. package/src/resource-mgmt/index.ts +0 -39
  192. package/src/resource-naming.ts +0 -62
  193. package/src/s3/index.ts +0 -523
  194. package/src/schema/cloud-config.schema.json +0 -554
  195. package/src/schema/index.ts +0 -68
  196. package/src/security/certificate-manager.ts +0 -492
  197. package/src/security/index.ts +0 -9
  198. package/src/security/scanning.ts +0 -545
  199. package/src/security/secrets-manager.ts +0 -476
  200. package/src/security/secrets-rotation.ts +0 -456
  201. package/src/security/security.test.ts +0 -738
  202. package/src/sms/advanced/ab-testing.ts +0 -389
  203. package/src/sms/advanced/analytics.ts +0 -336
  204. package/src/sms/advanced/campaigns.ts +0 -523
  205. package/src/sms/advanced/chatbot.ts +0 -224
  206. package/src/sms/advanced/index.ts +0 -10
  207. package/src/sms/advanced/link-tracking.ts +0 -248
  208. package/src/sms/advanced/mms.ts +0 -308
  209. package/src/sms/handlers/__tests__/send.test.ts +0 -40
  210. package/src/sms/handlers/delivery-status.ts +0 -133
  211. package/src/sms/handlers/receive.ts +0 -162
  212. package/src/sms/handlers/send.ts +0 -174
  213. package/src/sms/index.ts +0 -9
  214. package/src/stack-diff.ts +0 -389
  215. package/src/static-site/index.ts +0 -85
  216. package/src/template-builder.ts +0 -110
  217. package/src/template-validator.ts +0 -574
  218. package/src/utils/cache.ts +0 -291
  219. package/src/utils/diff.ts +0 -269
  220. package/src/utils/hash.ts +0 -227
  221. package/src/utils/index.ts +0 -8
  222. package/src/utils/parallel.ts +0 -294
  223. package/src/validators/credentials.test.ts +0 -274
  224. package/src/validators/credentials.ts +0 -233
  225. package/src/validators/quotas.test.ts +0 -434
  226. package/src/validators/quotas.ts +0 -217
  227. package/test/ai.test.ts +0 -327
  228. package/test/api.test.ts +0 -511
  229. package/test/auth.test.ts +0 -632
  230. package/test/cache.test.ts +0 -406
  231. package/test/cdn.test.ts +0 -247
  232. package/test/compute.test.ts +0 -861
  233. package/test/database.test.ts +0 -523
  234. package/test/deployment.test.ts +0 -499
  235. package/test/dns.test.ts +0 -270
  236. package/test/email.test.ts +0 -439
  237. package/test/filesystem.test.ts +0 -382
  238. package/test/integration.test.ts +0 -350
  239. package/test/messaging.test.ts +0 -514
  240. package/test/monitoring.test.ts +0 -634
  241. package/test/network.test.ts +0 -425
  242. package/test/permissions.test.ts +0 -488
  243. package/test/queue.test.ts +0 -484
  244. package/test/registry.test.ts +0 -306
  245. package/test/security.test.ts +0 -462
  246. package/test/storage.test.ts +0 -463
  247. package/test/template-validator.test.ts +0 -559
  248. package/test/workflow.test.ts +0 -592
  249. package/tsconfig.json +0 -16
  250. package/tsconfig.tsbuildinfo +0 -1
@@ -1,1120 +0,0 @@
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
- }