@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.
Files changed (251) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +321 -0
  3. package/package.json +31 -0
  4. package/src/advanced-features.test.ts +465 -0
  5. package/src/aws/cloudformation.ts +421 -0
  6. package/src/aws/cloudfront.ts +158 -0
  7. package/src/aws/credentials.test.ts +132 -0
  8. package/src/aws/credentials.ts +545 -0
  9. package/src/aws/index.ts +87 -0
  10. package/src/aws/s3.test.ts +188 -0
  11. package/src/aws/s3.ts +1088 -0
  12. package/src/aws/signature.test.ts +670 -0
  13. package/src/aws/signature.ts +1155 -0
  14. package/src/backup/disaster-recovery.test.ts +726 -0
  15. package/src/backup/disaster-recovery.ts +500 -0
  16. package/src/backup/index.ts +34 -0
  17. package/src/backup/manager.test.ts +498 -0
  18. package/src/backup/manager.ts +432 -0
  19. package/src/cicd/circleci.ts +430 -0
  20. package/src/cicd/github-actions.ts +424 -0
  21. package/src/cicd/gitlab-ci.ts +255 -0
  22. package/src/cicd/index.ts +8 -0
  23. package/src/cli/history.ts +396 -0
  24. package/src/cli/index.ts +10 -0
  25. package/src/cli/progress.ts +458 -0
  26. package/src/cli/repl.ts +454 -0
  27. package/src/cli/suggestions.ts +327 -0
  28. package/src/cli/table.test.ts +319 -0
  29. package/src/cli/table.ts +332 -0
  30. package/src/cloudformation/builder.test.ts +327 -0
  31. package/src/cloudformation/builder.ts +378 -0
  32. package/src/cloudformation/builders/api-gateway.ts +449 -0
  33. package/src/cloudformation/builders/cache.ts +334 -0
  34. package/src/cloudformation/builders/cdn.ts +278 -0
  35. package/src/cloudformation/builders/compute.ts +485 -0
  36. package/src/cloudformation/builders/database.ts +392 -0
  37. package/src/cloudformation/builders/functions.ts +343 -0
  38. package/src/cloudformation/builders/messaging.ts +140 -0
  39. package/src/cloudformation/builders/monitoring.ts +300 -0
  40. package/src/cloudformation/builders/network.ts +264 -0
  41. package/src/cloudformation/builders/queue.ts +147 -0
  42. package/src/cloudformation/builders/security.ts +399 -0
  43. package/src/cloudformation/builders/storage.ts +285 -0
  44. package/src/cloudformation/index.ts +30 -0
  45. package/src/cloudformation/types.ts +173 -0
  46. package/src/compliance/aws-config.ts +543 -0
  47. package/src/compliance/cloudtrail.ts +376 -0
  48. package/src/compliance/compliance.test.ts +423 -0
  49. package/src/compliance/guardduty.ts +446 -0
  50. package/src/compliance/index.ts +66 -0
  51. package/src/compliance/security-hub.ts +456 -0
  52. package/src/containers/build-optimization.ts +416 -0
  53. package/src/containers/containers.test.ts +508 -0
  54. package/src/containers/image-scanning.ts +360 -0
  55. package/src/containers/index.ts +9 -0
  56. package/src/containers/registry.ts +293 -0
  57. package/src/containers/service-mesh.ts +520 -0
  58. package/src/database/database.test.ts +762 -0
  59. package/src/database/index.ts +9 -0
  60. package/src/database/migrations.ts +444 -0
  61. package/src/database/performance.ts +528 -0
  62. package/src/database/replicas.ts +534 -0
  63. package/src/database/users.ts +494 -0
  64. package/src/dependency-graph.ts +143 -0
  65. package/src/deployment/ab-testing.ts +582 -0
  66. package/src/deployment/blue-green.ts +452 -0
  67. package/src/deployment/canary.ts +500 -0
  68. package/src/deployment/deployment.test.ts +526 -0
  69. package/src/deployment/index.ts +61 -0
  70. package/src/deployment/progressive.ts +62 -0
  71. package/src/dns/dns.test.ts +641 -0
  72. package/src/dns/dnssec.ts +315 -0
  73. package/src/dns/index.ts +8 -0
  74. package/src/dns/resolver.ts +496 -0
  75. package/src/dns/routing.ts +593 -0
  76. package/src/email/advanced/analytics.ts +445 -0
  77. package/src/email/advanced/index.ts +11 -0
  78. package/src/email/advanced/rules.ts +465 -0
  79. package/src/email/advanced/scheduling.ts +352 -0
  80. package/src/email/advanced/search.ts +412 -0
  81. package/src/email/advanced/shared-mailboxes.ts +404 -0
  82. package/src/email/advanced/templates.ts +455 -0
  83. package/src/email/advanced/threading.ts +281 -0
  84. package/src/email/analytics.ts +467 -0
  85. package/src/email/bounce-handling.ts +425 -0
  86. package/src/email/email.test.ts +431 -0
  87. package/src/email/handlers/__tests__/inbound.test.ts +38 -0
  88. package/src/email/handlers/__tests__/outbound.test.ts +37 -0
  89. package/src/email/handlers/converter.ts +227 -0
  90. package/src/email/handlers/feedback.ts +228 -0
  91. package/src/email/handlers/inbound.ts +169 -0
  92. package/src/email/handlers/outbound.ts +178 -0
  93. package/src/email/index.ts +15 -0
  94. package/src/email/reputation.ts +303 -0
  95. package/src/email/templates.ts +352 -0
  96. package/src/errors/index.test.ts +434 -0
  97. package/src/errors/index.ts +416 -0
  98. package/src/health-checks/index.ts +40 -0
  99. package/src/index.ts +360 -0
  100. package/src/intrinsic-functions.ts +118 -0
  101. package/src/lambda/concurrency.ts +330 -0
  102. package/src/lambda/destinations.ts +345 -0
  103. package/src/lambda/dlq.ts +425 -0
  104. package/src/lambda/index.ts +11 -0
  105. package/src/lambda/lambda.test.ts +840 -0
  106. package/src/lambda/layers.ts +263 -0
  107. package/src/lambda/versions.ts +376 -0
  108. package/src/lambda/vpc.ts +399 -0
  109. package/src/local/config.ts +114 -0
  110. package/src/local/index.ts +6 -0
  111. package/src/local/mock-aws.ts +351 -0
  112. package/src/modules/ai.ts +340 -0
  113. package/src/modules/api.ts +478 -0
  114. package/src/modules/auth.ts +805 -0
  115. package/src/modules/cache.ts +417 -0
  116. package/src/modules/cdn.ts +1062 -0
  117. package/src/modules/communication.ts +1094 -0
  118. package/src/modules/compute.ts +3348 -0
  119. package/src/modules/database.ts +554 -0
  120. package/src/modules/deployment.ts +1079 -0
  121. package/src/modules/dns.ts +337 -0
  122. package/src/modules/email.ts +1538 -0
  123. package/src/modules/filesystem.ts +515 -0
  124. package/src/modules/index.ts +32 -0
  125. package/src/modules/messaging.ts +486 -0
  126. package/src/modules/monitoring.ts +2086 -0
  127. package/src/modules/network.ts +664 -0
  128. package/src/modules/parameter-store.ts +325 -0
  129. package/src/modules/permissions.ts +1081 -0
  130. package/src/modules/phone.ts +494 -0
  131. package/src/modules/queue.ts +1260 -0
  132. package/src/modules/redirects.ts +464 -0
  133. package/src/modules/registry.ts +699 -0
  134. package/src/modules/search.ts +401 -0
  135. package/src/modules/secrets.ts +416 -0
  136. package/src/modules/security.ts +731 -0
  137. package/src/modules/sms.ts +389 -0
  138. package/src/modules/storage.ts +1120 -0
  139. package/src/modules/workflow.ts +680 -0
  140. package/src/multi-account/config.ts +521 -0
  141. package/src/multi-account/index.ts +7 -0
  142. package/src/multi-account/manager.ts +427 -0
  143. package/src/multi-region/cross-region.ts +410 -0
  144. package/src/multi-region/index.ts +8 -0
  145. package/src/multi-region/manager.ts +483 -0
  146. package/src/multi-region/regions.ts +435 -0
  147. package/src/network-security/index.ts +48 -0
  148. package/src/observability/index.ts +9 -0
  149. package/src/observability/logs.ts +522 -0
  150. package/src/observability/metrics.ts +460 -0
  151. package/src/observability/observability.test.ts +782 -0
  152. package/src/observability/synthetics.ts +568 -0
  153. package/src/observability/xray.ts +358 -0
  154. package/src/phone/advanced/analytics.ts +349 -0
  155. package/src/phone/advanced/callbacks.ts +428 -0
  156. package/src/phone/advanced/index.ts +8 -0
  157. package/src/phone/advanced/ivr-builder.ts +504 -0
  158. package/src/phone/advanced/recording.ts +310 -0
  159. package/src/phone/handlers/__tests__/incoming-call.test.ts +40 -0
  160. package/src/phone/handlers/incoming-call.ts +117 -0
  161. package/src/phone/handlers/missed-call.ts +116 -0
  162. package/src/phone/handlers/voicemail.ts +179 -0
  163. package/src/phone/index.ts +9 -0
  164. package/src/presets/api-backend.ts +134 -0
  165. package/src/presets/data-pipeline.ts +204 -0
  166. package/src/presets/extend.test.ts +295 -0
  167. package/src/presets/extend.ts +297 -0
  168. package/src/presets/fullstack-app.ts +144 -0
  169. package/src/presets/index.ts +27 -0
  170. package/src/presets/jamstack.ts +135 -0
  171. package/src/presets/microservices.ts +167 -0
  172. package/src/presets/ml-api.ts +208 -0
  173. package/src/presets/nodejs-server.ts +104 -0
  174. package/src/presets/nodejs-serverless.ts +114 -0
  175. package/src/presets/realtime-app.ts +184 -0
  176. package/src/presets/static-site.ts +64 -0
  177. package/src/presets/traditional-web-app.ts +339 -0
  178. package/src/presets/wordpress.ts +138 -0
  179. package/src/preview/github.test.ts +249 -0
  180. package/src/preview/github.ts +297 -0
  181. package/src/preview/index.ts +37 -0
  182. package/src/preview/manager.test.ts +440 -0
  183. package/src/preview/manager.ts +326 -0
  184. package/src/preview/notifications.test.ts +582 -0
  185. package/src/preview/notifications.ts +341 -0
  186. package/src/queue/batch-processing.ts +402 -0
  187. package/src/queue/dlq-monitoring.ts +402 -0
  188. package/src/queue/fifo.ts +342 -0
  189. package/src/queue/index.ts +9 -0
  190. package/src/queue/management.ts +428 -0
  191. package/src/queue/queue.test.ts +429 -0
  192. package/src/resource-mgmt/index.ts +39 -0
  193. package/src/resource-naming.ts +62 -0
  194. package/src/s3/index.ts +523 -0
  195. package/src/schema/cloud-config.schema.json +554 -0
  196. package/src/schema/index.ts +68 -0
  197. package/src/security/certificate-manager.ts +492 -0
  198. package/src/security/index.ts +9 -0
  199. package/src/security/scanning.ts +545 -0
  200. package/src/security/secrets-manager.ts +476 -0
  201. package/src/security/secrets-rotation.ts +456 -0
  202. package/src/security/security.test.ts +738 -0
  203. package/src/sms/advanced/ab-testing.ts +389 -0
  204. package/src/sms/advanced/analytics.ts +336 -0
  205. package/src/sms/advanced/campaigns.ts +523 -0
  206. package/src/sms/advanced/chatbot.ts +224 -0
  207. package/src/sms/advanced/index.ts +10 -0
  208. package/src/sms/advanced/link-tracking.ts +248 -0
  209. package/src/sms/advanced/mms.ts +308 -0
  210. package/src/sms/handlers/__tests__/send.test.ts +40 -0
  211. package/src/sms/handlers/delivery-status.ts +133 -0
  212. package/src/sms/handlers/receive.ts +162 -0
  213. package/src/sms/handlers/send.ts +174 -0
  214. package/src/sms/index.ts +9 -0
  215. package/src/stack-diff.ts +389 -0
  216. package/src/static-site/index.ts +85 -0
  217. package/src/template-builder.ts +110 -0
  218. package/src/template-validator.ts +574 -0
  219. package/src/utils/cache.ts +291 -0
  220. package/src/utils/diff.ts +269 -0
  221. package/src/utils/hash.ts +227 -0
  222. package/src/utils/index.ts +8 -0
  223. package/src/utils/parallel.ts +294 -0
  224. package/src/validators/credentials.test.ts +274 -0
  225. package/src/validators/credentials.ts +233 -0
  226. package/src/validators/quotas.test.ts +434 -0
  227. package/src/validators/quotas.ts +217 -0
  228. package/test/ai.test.ts +327 -0
  229. package/test/api.test.ts +511 -0
  230. package/test/auth.test.ts +632 -0
  231. package/test/cache.test.ts +406 -0
  232. package/test/cdn.test.ts +247 -0
  233. package/test/compute.test.ts +861 -0
  234. package/test/database.test.ts +523 -0
  235. package/test/deployment.test.ts +499 -0
  236. package/test/dns.test.ts +270 -0
  237. package/test/email.test.ts +439 -0
  238. package/test/filesystem.test.ts +382 -0
  239. package/test/integration.test.ts +350 -0
  240. package/test/messaging.test.ts +514 -0
  241. package/test/monitoring.test.ts +634 -0
  242. package/test/network.test.ts +425 -0
  243. package/test/permissions.test.ts +488 -0
  244. package/test/queue.test.ts +484 -0
  245. package/test/registry.test.ts +306 -0
  246. package/test/security.test.ts +462 -0
  247. package/test/storage.test.ts +463 -0
  248. package/test/template-validator.test.ts +559 -0
  249. package/test/workflow.test.ts +592 -0
  250. package/tsconfig.json +16 -0
  251. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,399 @@
1
+ import type { CloudFormationBuilder } from '../builder'
2
+ import { Fn } from '../types'
3
+
4
+ export interface SecurityConfig {
5
+ certificate?: {
6
+ domain: string
7
+ subdomains?: string[]
8
+ validationMethod?: 'DNS' | 'EMAIL'
9
+ }
10
+ waf?: {
11
+ enabled: boolean
12
+ rules?: string[]
13
+ rateLimit?: number
14
+ scope?: 'REGIONAL' | 'CLOUDFRONT'
15
+ }
16
+ securityGroups?: Record<string, {
17
+ ingress?: Array<{
18
+ port: number
19
+ protocol: string
20
+ cidr?: string
21
+ source?: string
22
+ }>
23
+ egress?: Array<{
24
+ port: number
25
+ protocol: string
26
+ cidr?: string
27
+ }>
28
+ }>
29
+ }
30
+
31
+ /**
32
+ * Add security resources (ACM certificates, WAF, security groups) to CloudFormation template
33
+ */
34
+ export function addSecurityResources(
35
+ builder: CloudFormationBuilder,
36
+ config: SecurityConfig,
37
+ ): void {
38
+ // ACM Certificate
39
+ if (config.certificate) {
40
+ addCertificate(builder, config.certificate)
41
+ }
42
+
43
+ // WAF Web ACL
44
+ if (config.waf?.enabled) {
45
+ addWAF(builder, config.waf)
46
+ }
47
+
48
+ // Additional Security Groups
49
+ if (config.securityGroups) {
50
+ for (const [name, sgConfig] of Object.entries(config.securityGroups)) {
51
+ addSecurityGroup(builder, name, sgConfig)
52
+ }
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Add ACM SSL/TLS Certificate
58
+ */
59
+ function addCertificate(
60
+ builder: CloudFormationBuilder,
61
+ config: SecurityConfig['certificate'],
62
+ ): void {
63
+ if (!config) return
64
+
65
+ const domains = [config.domain]
66
+ if (config.subdomains) {
67
+ domains.push(...config.subdomains)
68
+ }
69
+
70
+ builder.addResource('Certificate', 'AWS::CertificateManager::Certificate', {
71
+ DomainName: config.domain,
72
+ SubjectAlternativeNames: config.subdomains,
73
+ ValidationMethod: config.validationMethod || 'DNS',
74
+ DomainValidationOptions: domains.map(domain => ({
75
+ DomainName: domain,
76
+ HostedZoneId: Fn.ref('HostedZone'),
77
+ })),
78
+ Tags: [
79
+ { Key: 'Name', Value: Fn.sub(`\${AWS::StackName}-certificate`) },
80
+ ],
81
+ })
82
+
83
+ // Output
84
+ builder.addOutputs({
85
+ CertificateArn: {
86
+ Description: 'ACM Certificate ARN',
87
+ Value: Fn.ref('Certificate'),
88
+ Export: {
89
+ Name: Fn.sub('${AWS::StackName}-certificate-arn'),
90
+ },
91
+ },
92
+ })
93
+ }
94
+
95
+ /**
96
+ * Add AWS WAF Web ACL
97
+ */
98
+ function addWAF(
99
+ builder: CloudFormationBuilder,
100
+ config: SecurityConfig['waf'],
101
+ ): void {
102
+ if (!config) return
103
+
104
+ const rules: any[] = []
105
+ const ruleNames = config.rules || ['rateLimit', 'sqlInjection', 'xss']
106
+
107
+ // Rate limiting rule
108
+ if (ruleNames.includes('rateLimit')) {
109
+ rules.push({
110
+ Name: 'RateLimitRule',
111
+ Priority: 1,
112
+ Statement: {
113
+ RateBasedStatement: {
114
+ Limit: config.rateLimit || 2000,
115
+ AggregateKeyType: 'IP',
116
+ },
117
+ },
118
+ Action: {
119
+ Block: {},
120
+ },
121
+ VisibilityConfig: {
122
+ SampledRequestsEnabled: true,
123
+ CloudWatchMetricsEnabled: true,
124
+ MetricName: 'RateLimitRule',
125
+ },
126
+ })
127
+ }
128
+
129
+ // AWS Managed Rules
130
+ let priority = rules.length + 1
131
+
132
+ if (ruleNames.includes('sqlInjection')) {
133
+ rules.push({
134
+ Name: 'AWSManagedRulesSQLi',
135
+ Priority: priority++,
136
+ Statement: {
137
+ ManagedRuleGroupStatement: {
138
+ VendorName: 'AWS',
139
+ Name: 'AWSManagedRulesSQLiRuleSet',
140
+ },
141
+ },
142
+ OverrideAction: {
143
+ None: {},
144
+ },
145
+ VisibilityConfig: {
146
+ SampledRequestsEnabled: true,
147
+ CloudWatchMetricsEnabled: true,
148
+ MetricName: 'AWSManagedRulesSQLi',
149
+ },
150
+ })
151
+ }
152
+
153
+ if (ruleNames.includes('xss')) {
154
+ rules.push({
155
+ Name: 'AWSManagedRulesXSS',
156
+ Priority: priority++,
157
+ Statement: {
158
+ ManagedRuleGroupStatement: {
159
+ VendorName: 'AWS',
160
+ Name: 'AWSManagedRulesKnownBadInputsRuleSet',
161
+ },
162
+ },
163
+ OverrideAction: {
164
+ None: {},
165
+ },
166
+ VisibilityConfig: {
167
+ SampledRequestsEnabled: true,
168
+ CloudWatchMetricsEnabled: true,
169
+ MetricName: 'AWSManagedRulesXSS',
170
+ },
171
+ })
172
+ }
173
+
174
+ if (ruleNames.includes('knownBadInputs')) {
175
+ rules.push({
176
+ Name: 'AWSManagedRulesKnownBadInputs',
177
+ Priority: priority++,
178
+ Statement: {
179
+ ManagedRuleGroupStatement: {
180
+ VendorName: 'AWS',
181
+ Name: 'AWSManagedRulesKnownBadInputsRuleSet',
182
+ },
183
+ },
184
+ OverrideAction: {
185
+ None: {},
186
+ },
187
+ VisibilityConfig: {
188
+ SampledRequestsEnabled: true,
189
+ CloudWatchMetricsEnabled: true,
190
+ MetricName: 'AWSManagedRulesKnownBadInputs',
191
+ },
192
+ })
193
+ }
194
+
195
+ if (ruleNames.includes('coreRuleSet')) {
196
+ rules.push({
197
+ Name: 'AWSManagedRulesCoreRuleSet',
198
+ Priority: priority++,
199
+ Statement: {
200
+ ManagedRuleGroupStatement: {
201
+ VendorName: 'AWS',
202
+ Name: 'AWSManagedRulesCommonRuleSet',
203
+ },
204
+ },
205
+ OverrideAction: {
206
+ None: {},
207
+ },
208
+ VisibilityConfig: {
209
+ SampledRequestsEnabled: true,
210
+ CloudWatchMetricsEnabled: true,
211
+ MetricName: 'AWSManagedRulesCoreRuleSet',
212
+ },
213
+ })
214
+ }
215
+
216
+ if (ruleNames.includes('linuxRuleSet')) {
217
+ rules.push({
218
+ Name: 'AWSManagedRulesLinuxRuleSet',
219
+ Priority: priority++,
220
+ Statement: {
221
+ ManagedRuleGroupStatement: {
222
+ VendorName: 'AWS',
223
+ Name: 'AWSManagedRulesLinuxRuleSet',
224
+ },
225
+ },
226
+ OverrideAction: {
227
+ None: {},
228
+ },
229
+ VisibilityConfig: {
230
+ SampledRequestsEnabled: true,
231
+ CloudWatchMetricsEnabled: true,
232
+ MetricName: 'AWSManagedRulesLinuxRuleSet',
233
+ },
234
+ })
235
+ }
236
+
237
+ if (ruleNames.includes('apiProtection')) {
238
+ rules.push({
239
+ Name: 'AWSManagedRulesAPIProtection',
240
+ Priority: priority++,
241
+ Statement: {
242
+ ManagedRuleGroupStatement: {
243
+ VendorName: 'AWS',
244
+ Name: 'AWSManagedRulesAmazonIpReputationList',
245
+ },
246
+ },
247
+ OverrideAction: {
248
+ None: {},
249
+ },
250
+ VisibilityConfig: {
251
+ SampledRequestsEnabled: true,
252
+ CloudWatchMetricsEnabled: true,
253
+ MetricName: 'AWSManagedRulesAPIProtection',
254
+ },
255
+ })
256
+ }
257
+
258
+ if (ruleNames.includes('geoBlock')) {
259
+ // Example: Block traffic from certain countries
260
+ rules.push({
261
+ Name: 'GeoBlockRule',
262
+ Priority: priority++,
263
+ Statement: {
264
+ GeoMatchStatement: {
265
+ CountryCodes: ['CN', 'RU'], // Example countries to block
266
+ },
267
+ },
268
+ Action: {
269
+ Block: {},
270
+ },
271
+ VisibilityConfig: {
272
+ SampledRequestsEnabled: true,
273
+ CloudWatchMetricsEnabled: true,
274
+ MetricName: 'GeoBlockRule',
275
+ },
276
+ })
277
+ }
278
+
279
+ if (ruleNames.includes('connectionLimit')) {
280
+ rules.push({
281
+ Name: 'ConnectionLimitRule',
282
+ Priority: priority++,
283
+ Statement: {
284
+ RateBasedStatement: {
285
+ Limit: 100,
286
+ AggregateKeyType: 'IP',
287
+ },
288
+ },
289
+ Action: {
290
+ Block: {},
291
+ },
292
+ VisibilityConfig: {
293
+ SampledRequestsEnabled: true,
294
+ CloudWatchMetricsEnabled: true,
295
+ MetricName: 'ConnectionLimitRule',
296
+ },
297
+ })
298
+ }
299
+
300
+ // Web ACL
301
+ builder.addResource('WebACL', 'AWS::WAFv2::WebACL', {
302
+ Name: Fn.sub('${AWS::StackName}-waf'),
303
+ Scope: config.scope || 'REGIONAL',
304
+ DefaultAction: {
305
+ Allow: {},
306
+ },
307
+ Rules: rules,
308
+ VisibilityConfig: {
309
+ SampledRequestsEnabled: true,
310
+ CloudWatchMetricsEnabled: true,
311
+ MetricName: Fn.sub('${AWS::StackName}-waf'),
312
+ },
313
+ Tags: [
314
+ { Key: 'Name', Value: Fn.sub('${AWS::StackName}-waf') },
315
+ ],
316
+ })
317
+
318
+ // Associate WAF with ALB (if exists)
319
+ if (builder.hasResource('LoadBalancer')) {
320
+ builder.addResource('WebACLAssociation', 'AWS::WAFv2::WebACLAssociation', {
321
+ ResourceArn: Fn.ref('LoadBalancer'),
322
+ WebACLArn: Fn.getAtt('WebACL', 'Arn'),
323
+ }, {
324
+ dependsOn: ['WebACL', 'LoadBalancer'],
325
+ })
326
+ }
327
+
328
+ // Output
329
+ builder.addOutputs({
330
+ WebACLId: {
331
+ Description: 'WAF Web ACL ID',
332
+ Value: Fn.ref('WebACL'),
333
+ Export: {
334
+ Name: Fn.sub('${AWS::StackName}-waf-id'),
335
+ },
336
+ },
337
+ WebACLArn: {
338
+ Description: 'WAF Web ACL ARN',
339
+ Value: Fn.getAtt('WebACL', 'Arn'),
340
+ Export: {
341
+ Name: Fn.sub('${AWS::StackName}-waf-arn'),
342
+ },
343
+ },
344
+ })
345
+ }
346
+
347
+ /**
348
+ * Add Security Group
349
+ */
350
+ function addSecurityGroup(
351
+ builder: CloudFormationBuilder,
352
+ name: string,
353
+ config: NonNullable<SecurityConfig['securityGroups']>[string] | undefined,
354
+ ): void {
355
+ if (!config) return
356
+
357
+ const logicalId = builder.toLogicalId(`${name}-security-group`)
358
+
359
+ const ingressRules = config.ingress?.map((rule: NonNullable<NonNullable<SecurityConfig['securityGroups']>[string]['ingress']>[number]) => ({
360
+ IpProtocol: rule.protocol,
361
+ FromPort: rule.port,
362
+ ToPort: rule.port,
363
+ CidrIp: rule.cidr,
364
+ SourceSecurityGroupId: rule.source ? Fn.ref(rule.source) : undefined,
365
+ })) || []
366
+
367
+ const egressRules = config.egress?.map((rule: NonNullable<NonNullable<SecurityConfig['securityGroups']>[string]['egress']>[number]) => ({
368
+ IpProtocol: rule.protocol,
369
+ FromPort: rule.port,
370
+ ToPort: rule.port,
371
+ CidrIp: rule.cidr || '0.0.0.0/0',
372
+ })) || [{
373
+ IpProtocol: '-1',
374
+ CidrIp: '0.0.0.0/0',
375
+ }]
376
+
377
+ builder.addResource(logicalId, 'AWS::EC2::SecurityGroup', {
378
+ GroupDescription: `Security group for ${name}`,
379
+ VpcId: Fn.ref('VPC'),
380
+ SecurityGroupIngress: ingressRules,
381
+ SecurityGroupEgress: egressRules,
382
+ Tags: [
383
+ { Key: 'Name', Value: Fn.sub(`\${AWS::StackName}-${name}-sg`) },
384
+ ],
385
+ }, {
386
+ dependsOn: 'VPC',
387
+ })
388
+
389
+ // Output
390
+ builder.addOutputs({
391
+ [`${logicalId}Id`]: {
392
+ Description: `${name} security group ID`,
393
+ Value: Fn.ref(logicalId),
394
+ Export: {
395
+ Name: Fn.sub(`\${AWS::StackName}-${name}-sg-id`),
396
+ },
397
+ },
398
+ })
399
+ }
@@ -0,0 +1,285 @@
1
+ import type { CloudFormationBuilder } from '../builder'
2
+ import { Arn, Fn } from '../types'
3
+
4
+ export interface StorageConfig {
5
+ [bucketName: string]: {
6
+ public?: boolean
7
+ versioning?: boolean
8
+ website?: boolean
9
+ encryption?: boolean
10
+ intelligentTiering?: boolean
11
+ cors?: Array<{
12
+ allowedOrigins: string[]
13
+ allowedMethods: string[]
14
+ allowedHeaders?: string[]
15
+ maxAge?: number
16
+ }>
17
+ lifecycleRules?: Array<{
18
+ id: string
19
+ enabled: boolean
20
+ expirationDays?: number
21
+ transitions?: Array<{
22
+ days: number
23
+ storageClass: 'STANDARD_IA' | 'ONEZONE_IA' | 'INTELLIGENT_TIERING' | 'GLACIER' | 'DEEP_ARCHIVE'
24
+ }>
25
+ }>
26
+ type?: 'efs'
27
+ performanceMode?: 'generalPurpose' | 'maxIO'
28
+ throughputMode?: 'bursting' | 'provisioned'
29
+ lifecyclePolicy?: {
30
+ transitionToIA?: number
31
+ transitionToPrimaryStorageClass?: number
32
+ }
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Add S3 and EFS storage resources to CloudFormation template
38
+ */
39
+ export function addStorageResources(
40
+ builder: CloudFormationBuilder,
41
+ config: StorageConfig,
42
+ ): void {
43
+ for (const [bucketName, bucketConfig] of Object.entries(config)) {
44
+ // Check if this is an EFS configuration
45
+ if (bucketConfig.type === 'efs') {
46
+ addEFSResource(builder, bucketName, bucketConfig)
47
+ continue
48
+ }
49
+
50
+ // Otherwise, create S3 bucket
51
+ addS3Bucket(builder, bucketName, bucketConfig)
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Add S3 bucket resource
57
+ */
58
+ function addS3Bucket(
59
+ builder: CloudFormationBuilder,
60
+ bucketName: string,
61
+ config: StorageConfig[string],
62
+ ): void {
63
+ const logicalId = builder.toLogicalId(`${bucketName}-bucket`)
64
+ const properties: Record<string, any> = {
65
+ BucketName: Fn.sub(`\${AWS::StackName}-${bucketName}`),
66
+ Tags: [
67
+ { Key: 'Name', Value: Fn.sub(`\${AWS::StackName}-${bucketName}`) },
68
+ ],
69
+ }
70
+
71
+ // Versioning
72
+ if (config.versioning) {
73
+ properties.VersioningConfiguration = {
74
+ Status: 'Enabled',
75
+ }
76
+ }
77
+
78
+ // Encryption
79
+ if (config.encryption) {
80
+ properties.BucketEncryption = {
81
+ ServerSideEncryptionConfiguration: [{
82
+ ServerSideEncryptionByDefault: {
83
+ SSEAlgorithm: 'AES256',
84
+ },
85
+ }],
86
+ }
87
+ }
88
+
89
+ // Public access
90
+ if (!config.public) {
91
+ properties.PublicAccessBlockConfiguration = {
92
+ BlockPublicAcls: true,
93
+ BlockPublicPolicy: true,
94
+ IgnorePublicAcls: true,
95
+ RestrictPublicBuckets: true,
96
+ }
97
+ }
98
+
99
+ // Website configuration
100
+ if (config.website) {
101
+ properties.WebsiteConfiguration = {
102
+ IndexDocument: 'index.html',
103
+ ErrorDocument: 'index.html', // For SPA routing
104
+ }
105
+ }
106
+
107
+ // CORS configuration
108
+ if (config.cors && config.cors.length > 0) {
109
+ properties.CorsConfiguration = {
110
+ CorsRules: config.cors.map(rule => ({
111
+ AllowedOrigins: rule.allowedOrigins,
112
+ AllowedMethods: rule.allowedMethods,
113
+ AllowedHeaders: rule.allowedHeaders || ['*'],
114
+ MaxAge: rule.maxAge || 3600,
115
+ })),
116
+ }
117
+ }
118
+
119
+ // Lifecycle rules
120
+ if (config.lifecycleRules && config.lifecycleRules.length > 0) {
121
+ properties.LifecycleConfiguration = {
122
+ Rules: config.lifecycleRules.map(rule => ({
123
+ Id: rule.id,
124
+ Status: rule.enabled ? 'Enabled' : 'Disabled',
125
+ ExpirationInDays: rule.expirationDays,
126
+ Transitions: rule.transitions?.map(t => ({
127
+ TransitionInDays: t.days,
128
+ StorageClass: t.storageClass,
129
+ })),
130
+ })),
131
+ }
132
+ }
133
+
134
+ // Intelligent tiering
135
+ if (config.intelligentTiering) {
136
+ properties.IntelligentTieringConfigurations = [{
137
+ Id: 'EntireBucket',
138
+ Status: 'Enabled',
139
+ Tierings: [
140
+ {
141
+ AccessTier: 'ARCHIVE_ACCESS',
142
+ Days: 90,
143
+ },
144
+ {
145
+ AccessTier: 'DEEP_ARCHIVE_ACCESS',
146
+ Days: 180,
147
+ },
148
+ ],
149
+ }]
150
+ }
151
+
152
+ builder.addResource(logicalId, 'AWS::S3::Bucket', properties, {
153
+ deletionPolicy: config.versioning ? 'Retain' : 'Delete',
154
+ })
155
+
156
+ // Bucket policy for public access if needed
157
+ if (config.public) {
158
+ builder.addResource(`${logicalId}Policy`, 'AWS::S3::BucketPolicy', {
159
+ Bucket: Fn.ref(logicalId),
160
+ PolicyDocument: {
161
+ Version: '2012-10-17',
162
+ Statement: [{
163
+ Sid: 'PublicReadGetObject',
164
+ Effect: 'Allow',
165
+ Principal: '*',
166
+ Action: 's3:GetObject',
167
+ Resource: Fn.join('', [Arn.s3Bucket(Fn.ref(logicalId) as any), '/*']),
168
+ }],
169
+ },
170
+ }, {
171
+ dependsOn: logicalId,
172
+ })
173
+ }
174
+
175
+ // Output bucket name and ARN
176
+ builder.addOutputs({
177
+ [`${logicalId}Name`]: {
178
+ Description: `${bucketName} bucket name`,
179
+ Value: Fn.ref(logicalId),
180
+ Export: {
181
+ Name: Fn.sub(`\${AWS::StackName}-${bucketName}-bucket`),
182
+ },
183
+ },
184
+ [`${logicalId}Arn`]: {
185
+ Description: `${bucketName} bucket ARN`,
186
+ Value: Fn.getAtt(logicalId, 'Arn'),
187
+ Export: {
188
+ Name: Fn.sub(`\${AWS::StackName}-${bucketName}-bucket-arn`),
189
+ },
190
+ },
191
+ })
192
+
193
+ if (config.website) {
194
+ builder.addOutputs({
195
+ [`${logicalId}WebsiteURL`]: {
196
+ Description: `${bucketName} website URL`,
197
+ Value: Fn.getAtt(logicalId, 'WebsiteURL'),
198
+ },
199
+ })
200
+ }
201
+ }
202
+
203
+ /**
204
+ * Add EFS file system resource
205
+ */
206
+ function addEFSResource(
207
+ builder: CloudFormationBuilder,
208
+ name: string,
209
+ config: StorageConfig[string],
210
+ ): void {
211
+ const logicalId = builder.toLogicalId(`${name}-efs`)
212
+
213
+ // EFS File System
214
+ const properties: Record<string, any> = {
215
+ Encrypted: config.encryption !== false,
216
+ PerformanceMode: config.performanceMode || 'generalPurpose',
217
+ ThroughputMode: config.throughputMode || 'bursting',
218
+ FileSystemTags: [
219
+ { Key: 'Name', Value: Fn.sub(`\${AWS::StackName}-${name}`) },
220
+ ],
221
+ }
222
+
223
+ // Lifecycle policy
224
+ if (config.lifecyclePolicy) {
225
+ properties.LifecyclePolicies = []
226
+
227
+ if (config.lifecyclePolicy.transitionToIA) {
228
+ properties.LifecyclePolicies.push({
229
+ TransitionToIA: `AFTER_${config.lifecyclePolicy.transitionToIA}_DAYS`,
230
+ })
231
+ }
232
+
233
+ if (config.lifecyclePolicy.transitionToPrimaryStorageClass) {
234
+ properties.LifecyclePolicies.push({
235
+ TransitionToPrimaryStorageClass: 'AFTER_1_ACCESS',
236
+ })
237
+ }
238
+ }
239
+
240
+ builder.addResource(logicalId, 'AWS::EFS::FileSystem', properties, {
241
+ deletionPolicy: 'Retain',
242
+ })
243
+
244
+ // EFS Mount Targets (one per AZ/subnet)
245
+ // Note: Assumes VPC and subnets are already created
246
+ // In a real implementation, you'd get the subnet IDs from the VPC configuration
247
+ const availabilityZones = 2 // Should come from network config
248
+ for (let i = 0; i < availabilityZones; i++) {
249
+ builder.addResource(`${logicalId}MountTarget${i + 1}`, 'AWS::EFS::MountTarget', {
250
+ FileSystemId: Fn.ref(logicalId),
251
+ SubnetId: Fn.ref(`PrivateSubnet${i + 1}`),
252
+ SecurityGroups: [Fn.ref('EFSSecurityGroup')],
253
+ }, {
254
+ dependsOn: [logicalId, `PrivateSubnet${i + 1}`],
255
+ })
256
+ }
257
+
258
+ // Security group for EFS
259
+ builder.addResource('EFSSecurityGroup', 'AWS::EC2::SecurityGroup', {
260
+ GroupDescription: 'Security group for EFS mount targets',
261
+ VpcId: Fn.ref('VPC'),
262
+ SecurityGroupIngress: [{
263
+ IpProtocol: 'tcp',
264
+ FromPort: 2049,
265
+ ToPort: 2049,
266
+ SourceSecurityGroupId: Fn.ref('AppSecurityGroup'), // Assumes app security group exists
267
+ }],
268
+ Tags: [
269
+ { Key: 'Name', Value: Fn.sub('${AWS::StackName}-efs-sg') },
270
+ ],
271
+ }, {
272
+ dependsOn: 'VPC',
273
+ })
274
+
275
+ // Output EFS ID
276
+ builder.addOutputs({
277
+ [`${logicalId}Id`]: {
278
+ Description: `${name} EFS file system ID`,
279
+ Value: Fn.ref(logicalId),
280
+ Export: {
281
+ Name: Fn.sub(`\${AWS::StackName}-${name}-efs`),
282
+ },
283
+ },
284
+ })
285
+ }
@@ -0,0 +1,30 @@
1
+ // CloudFormation template builder
2
+ export {
3
+ CloudFormationBuilder,
4
+ buildCloudFormationTemplate,
5
+ } from './builder'
6
+
7
+ // CloudFormation types
8
+ export type {
9
+ CloudFormationTemplate,
10
+ CloudFormationResource,
11
+ CloudFormationParameter,
12
+ CloudFormationOutput,
13
+ CloudFormationCondition,
14
+ CloudFormationIntrinsicFunction,
15
+ } from './types'
16
+
17
+ export {
18
+ Fn,
19
+ Arn,
20
+ AWS_PSEUDO_PARAMETERS,
21
+ } from './types'
22
+
23
+ // Resource builders
24
+ export { addNetworkResources } from './builders/network'
25
+ export { addStorageResources } from './builders/storage'
26
+ export { addComputeResources } from './builders/compute'
27
+ export { addDatabaseResources } from './builders/database'
28
+ export { addFunctionResources } from './builders/functions'
29
+ export { addQueueResources } from './builders/queue'
30
+ export type { QueueConfig } from './builders/queue'