@reventlessdev/reventless-aws 3.0.0-alpha.252 → 3.0.0-alpha.254

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 (38) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/package.json +8 -8
  3. package/src/Platform.res +159 -21
  4. package/src/Platform.res.mjs +120 -8
  5. package/src/adapter/Runtime/AggregateRuntime_Builder_Single.res +5 -0
  6. package/src/adapter/Runtime/AggregateRuntime_Builder_Single.res.mjs +3 -0
  7. package/src/adapter/Runtime/AggregateRuntime_Builder_Single_Async.res +5 -0
  8. package/src/adapter/Runtime/AggregateRuntime_Builder_Single_Async.res.mjs +3 -0
  9. package/src/adapter/Runtime/RuntimeEnvironment_Lambda.res +66 -8
  10. package/src/adapter/Runtime/RuntimeEnvironment_Lambda.res.mjs +16 -3
  11. package/src/adapter/Runtime/StateChangeSliceRuntime_Builder_Single.res +5 -0
  12. package/src/adapter/Runtime/StateChangeSliceRuntime_Builder_Single.res.mjs +3 -0
  13. package/src/adapter/Task/TaskBucket_S3.res +11 -0
  14. package/src/adapter/Task/TaskBucket_S3.res.mjs +3 -0
  15. package/src/adapter/Upload/Upload_Claim_S3.res +361 -0
  16. package/src/adapter/Upload/Upload_Claim_S3.res.mjs +203 -0
  17. package/src/adapter/Upload/Upload_Claim_S3_Ops.res +279 -0
  18. package/src/adapter/Upload/Upload_Claim_S3_Ops.res.mjs +261 -0
  19. package/src/adapter/Upload/Upload_PendingTag.res +41 -0
  20. package/src/adapter/Upload/Upload_PendingTag.res.mjs +15 -0
  21. package/src/adapter/Upload/Upload_Presign_S3.res +12 -2
  22. package/src/adapter/Upload/Upload_Presign_S3.res.mjs +1 -0
  23. package/src/adapter/Upload/Upload_Presign_S3_Ops.res +17 -1
  24. package/src/adapter/Upload/Upload_Presign_S3_Ops.res.mjs +3 -1
  25. package/src/capability/Capability_ObjectStore_S3.res +42 -1
  26. package/src/capability/Capability_ObjectStore_S3.res.mjs +19 -1
  27. package/src/components/Api/AppSync_Adapter.res +36 -0
  28. package/src/components/Api/AppSync_Adapter.res.mjs +14 -0
  29. package/src/util/Util_LogRetention.res +86 -0
  30. package/src/util/Util_LogRetention.res.mjs +43 -0
  31. package/src/util/Util_StoreLayout.res +62 -9
  32. package/src/util/Util_StoreLayout.res.mjs +20 -2
  33. package/tests/Upload_ClaimTest.res +132 -0
  34. package/tests/Upload_ClaimTest.res.mjs +101 -0
  35. package/tests/Util_LogRetentionTest.res +122 -0
  36. package/tests/Util_LogRetentionTest.res.mjs +95 -0
  37. package/tests/Util_StoreLayoutTest.res +79 -3
  38. package/tests/Util_StoreLayoutTest.res.mjs +47 -2
@@ -115,7 +115,9 @@ let makeFromCodeAsset: (
115
115
  ~logRetentionDays: int=?,
116
116
  /** Provision the DCB append-retry/conflict CloudWatch metric filters on this
117
117
  Lambda's log group (command-handler Lambdas only). Requires a managed log
118
- group, so it only takes effect when `~logRetentionDays` is also set. */
118
+ group, so it only takes effect when one is created — i.e. when
119
+ `~logRetentionDays` is set or the stack tier manages its groups
120
+ (`Util_LogRetention.managesLogGroup`). */
119
121
  ~dcbMetrics: bool=?,
120
122
  /** Place the Lambda in a VPC (needed to reach RDS/managed Postgres). When set,
121
123
  the execution role also gets the EC2 network-interface permissions AWS
@@ -143,6 +145,18 @@ let makeFromCodeAsset: (
143
145
  let opts =
144
146
  opts->Option.map(ReventlessCore.Util.Pulumi.ComponentResourceOptions.toCustomResourceOptions)
145
147
 
148
+ // The environment tier drives retention, default log level and whether the log
149
+ // group is Pulumi-managed. Resolved once from the stack name and the shared
150
+ // prod allow-list (the same notion of "production" that names the host-shell
151
+ // domain and the store layout).
152
+ let stack = Pulumi.Pulumi.getStackName()
153
+ let prodStacks = Util_HostUiDomain.resolveProdStacks()
154
+ // Stacks explicitly held on auto-created log groups pending a `pulumi import`
155
+ // (none today — the escape hatch for adopting a pre-existing stack later).
156
+ let unmanagedStacks = Util_LogRetention.parseUnmanagedStacks(
157
+ Util_LocalConfig.get("unmanagedLogGroupStacks")->Option.getOr(""),
158
+ )
159
+
146
160
  // One attribution identity for every resource provisioned here: same owning
147
161
  // component and kind throughout, distinct piece roles — the function itself,
148
162
  // the execution identity it assumes, and its log group are three pieces of one
@@ -219,9 +233,7 @@ let makeFromCodeAsset: (
219
233
  ->Option.getOr([])
220
234
  ->Pulumi.Input.make
221
235
 
222
- let variables = Dict.fromArray([
223
- ("Environment", Pulumi.Pulumi.getStackName()->Pulumi.Input.make),
224
- ])
236
+ let variables = Dict.fromArray([("Environment", stack->Pulumi.Input.make)])
225
237
  envVars->Dict.forEachWithKey((value, key) => {
226
238
  variables->Dict.set(key, value)
227
239
  })
@@ -229,6 +241,21 @@ let makeFromCodeAsset: (
229
241
  variables->Dict.set(key, value)
230
242
  })
231
243
 
244
+ // Default the logger's minimum level per environment tier when nothing already
245
+ // pinned one (caller `envVars` or a consumer-registered `additionalEnvVars`
246
+ // win). Verbose on disposable dev stacks, quiet on prod/beta — for every
247
+ // Lambda, not just the command handlers.
248
+ if variables->Dict.get("LOG_LEVEL")->Option.isNone {
249
+ variables->Dict.set(
250
+ "LOG_LEVEL",
251
+ Util_LogRetention.logLevelFor(
252
+ ~stack,
253
+ ~prodStacks,
254
+ ~configOverride=?Util_LocalConfig.get("logLevel"),
255
+ )->Pulumi.Input.make,
256
+ )
257
+ }
258
+
232
259
  // ESM self-containment (Option C): every code archive built by
233
260
  // Util_Bundle.buildCodeArchive ships register-hook.mjs + layer-resolver.mjs at
234
261
  // /var/task. --import registers the resolve hook so the ESM entry point (and the
@@ -263,13 +290,44 @@ let makeFromCodeAsset: (
263
290
  ~opts?,
264
291
  )
265
292
 
266
- // Optional CloudWatch log-group with explicit retention. When not set, Lambda
267
- // auto-creates a log group with no retention (logs accumulate indefinitely).
268
- logRetentionDays->Option.forEach(days => {
293
+ // Managed CloudWatch log-group with explicit retention. Created when the caller
294
+ // pins `~logRetentionDays` (the app-developer opt-in) OR the stack manages its
295
+ // groups — every stack by default, bar any named in `unmanagedLogGroupStacks`
296
+ // (see `Util_LogRetention.managesLogGroup`). When neither holds, Lambda
297
+ // auto-creates an unmanaged group with no retention.
298
+ //
299
+ // Precedence for the retention value: explicit `~logRetentionDays` → the
300
+ // `logRetentionDays` config key → the environment tier default.
301
+ //
302
+ // The group name derives from the function's *physical* name output, not the
303
+ // logical `name`. `Lambda.Function` has no `name` arg, so its physical name
304
+ // always carries Pulumi's `-<7hex>` suffix; a literal `/aws/lambda/${name}`
305
+ // would create an empty group beside the real one, leave orphan accumulation
306
+ // unchanged, and collide across stacks sharing an account.
307
+ let managedRetention = switch (
308
+ logRetentionDays,
309
+ Util_LogRetention.managesLogGroup(~stack, ~unmanagedStacks),
310
+ ) {
311
+ | (Some(days), _) => Some(days)
312
+ | (None, true) =>
313
+ Some(
314
+ Util_LogRetention.retentionDaysFor(
315
+ ~stack,
316
+ ~prodStacks,
317
+ ~configOverride=?Util_LocalConfig.get("logRetentionDays")->Option.flatMap(s =>
318
+ Int.fromString(s)
319
+ ),
320
+ ),
321
+ )
322
+ | (None, false) => None
323
+ }
324
+ managedRetention->Option.forEach(days => {
269
325
  let logGroup = Cloudwatch.LogGroup.make(
270
326
  ~name=`${name}LogGroup`,
271
327
  ~args={
272
- name: `/aws/lambda/${name}`->Pulumi.Input.make,
328
+ name: lambda.name
329
+ ->Pulumi.Output.apply(n => `/aws/lambda/${n}`)
330
+ ->Pulumi.Output.asInput,
273
331
  retentionInDays: days->Pulumi.Input.make,
274
332
  tags: tagsFor(~resourceName=`${name}LogGroup`, ~role=Logs),
275
333
  },
@@ -20,6 +20,9 @@ import * as Util_Bundle$ReventlessAws from "../../util/Util_Bundle.res.mjs";
20
20
  import * as Util_Lambda$ReventlessAws from "../../util/Util_Lambda.res.mjs";
21
21
  import * as Util_Pulumi$ReventlessCore from "@reventlessdev/reventless-core/src/util/Util_Pulumi.res.mjs";
22
22
  import * as Util_IAM_Role$ReventlessAws from "../../util/Util_IAM_Role.res.mjs";
23
+ import * as Util_LocalConfig$ReventlessAws from "../../util/Util_LocalConfig.res.mjs";
24
+ import * as Util_HostUiDomain$ReventlessAws from "../../util/Util_HostUiDomain.res.mjs";
25
+ import * as Util_LogRetention$ReventlessAws from "../../util/Util_LogRetention.res.mjs";
23
26
 
24
27
  let additionalEnvVars = {};
25
28
 
@@ -50,6 +53,9 @@ function makeFromCodeAsset(name, unitKind, componentKind, code, sourceCodeHash,
50
53
  let timeout = timeoutOpt !== undefined ? timeoutOpt : Runtime$ReventlessCore.CommandHandlerDefaults.timeout;
51
54
  let dcbMetrics = dcbMetricsOpt !== undefined ? dcbMetricsOpt : false;
52
55
  let opts$1 = Stdlib_Option.map(opts, Util_Pulumi$ReventlessCore.ComponentResourceOptions.toCustomResourceOptions);
56
+ let stack = Pulumi.getStack();
57
+ let prodStacks = Util_HostUiDomain$ReventlessAws.resolveProdStacks();
58
+ let unmanagedStacks = Util_LogRetention$ReventlessAws.parseUnmanagedStacks(Stdlib_Option.getOr(Util_LocalConfig$ReventlessAws.get("unmanagedLogGroupStacks"), ""));
53
59
  let tagsFor = (resourceName, role) => AWS_Tags$ReventlessAws.make(resourceName, componentKind, role, undefined, name, undefined, undefined, undefined);
54
60
  let lambdaRole = IAM$PulumiAws.Role.makeWithDefaultPolicy(name, Pulumi.output(AWS$ReventlessAws.Lambda.principal), tagsFor(name, "Identity"), opts$1);
55
61
  Stdlib_Option.forEach(vpcConfig, param => {
@@ -87,7 +93,7 @@ function makeFromCodeAsset(name, unitKind, componentKind, code, sourceCodeHash,
87
93
  let layers = Stdlib_Option.getOr(Stdlib_Option.map(Lambda$PulumiAws.reventlessLayerArn, arn => [arn]), []);
88
94
  let variables = Object.fromEntries([[
89
95
  "Environment",
90
- Pulumi.getStack()
96
+ stack
91
97
  ]]);
92
98
  Stdlib_Dict.forEachWithKey(envVars, (value, key) => {
93
99
  variables[key] = value;
@@ -95,6 +101,9 @@ function makeFromCodeAsset(name, unitKind, componentKind, code, sourceCodeHash,
95
101
  Stdlib_Dict.forEachWithKey(additionalEnvVars, (value, key) => {
96
102
  variables[key] = value;
97
103
  });
104
+ if (Stdlib_Option.isNone(variables["LOG_LEVEL"])) {
105
+ variables["LOG_LEVEL"] = Util_LogRetention$ReventlessAws.logLevelFor(stack, prodStacks, Util_LocalConfig$ReventlessAws.get("logLevel"));
106
+ }
98
107
  variables["NODE_OPTIONS"] = Util_Bundle$ReventlessAws.esmLoaderNodeOptions;
99
108
  variables["ESM_FALLBACK_DIRS"] = Util_Bundle$ReventlessAws.esmFallbackDirs;
100
109
  let tags = tagsFor(name, "Runtime");
@@ -117,9 +126,13 @@ function makeFromCodeAsset(name, unitKind, componentKind, code, sourceCodeHash,
117
126
  })),
118
127
  vpcConfig: vpcConfig
119
128
  }, opts$1 !== undefined ? Primitive_option.valFromOption(opts$1) : undefined);
120
- Stdlib_Option.forEach(logRetentionDays, days => {
129
+ let match = Util_LogRetention$ReventlessAws.managesLogGroup(stack, unmanagedStacks);
130
+ let managedRetention = logRetentionDays !== undefined ? logRetentionDays : (
131
+ match ? Util_LogRetention$ReventlessAws.retentionDaysFor(stack, prodStacks, Stdlib_Option.flatMap(Util_LocalConfig$ReventlessAws.get("logRetentionDays"), s => Stdlib_Int.fromString(s, undefined))) : undefined
132
+ );
133
+ Stdlib_Option.forEach(managedRetention, days => {
121
134
  let logGroup = new (Aws.cloudwatch.LogGroup)(name + `LogGroup`, {
122
- name: `/aws/lambda/` + name,
135
+ name: lambda.name.apply(n => `/aws/lambda/` + n),
123
136
  retentionInDays: days,
124
137
  tags: tagsFor(name + `LogGroup`, "Logs")
125
138
  }, opts$1 !== undefined ? Primitive_option.valFromOption(opts$1) : undefined);
@@ -107,6 +107,11 @@ let forDcbCommandTopic = (
107
107
  }
108
108
  })
109
109
  )
110
+ // First-class log-level override → LOG_LEVEL, winning over any generic
111
+ // envVars entry. When unset, makeFromCodeAsset applies the tier default.
112
+ cfg.logLevel->Option.forEach(level =>
113
+ envVars->Dict.set("LOG_LEVEL", level->Pulumi.Output.make->Pulumi.Output.asInput)
114
+ )
110
115
 
111
116
  // Build HANDLER_CONFIG JSON: array of {spec, behavior} objects so the entry point
112
117
  // can dynamically import both modules and apply the curried StateChangeSlice_Callback.Make
@@ -53,6 +53,9 @@ function forDcbCommandTopic(slicePaths, inboundSlicesOpt, dcbTableName, pluginNa
53
53
  return;
54
54
  }
55
55
  }));
56
+ Stdlib_Option.forEach(cfg.logLevel, level => {
57
+ envVars["LOG_LEVEL"] = Pulumi.output(level);
58
+ });
56
59
  let sliceModulesJson = slicePaths.map(param => {
57
60
  let s = Stdlib_Option.getOr(JSON.stringify(param[0]), `""`);
58
61
  let b = Stdlib_Option.getOr(JSON.stringify(param[1]), `""`);
@@ -125,10 +125,21 @@ let connect = (
125
125
  let make: ReventlessCore.Task_Adapter.bucketMaker<bucketParts> = (~name, ~opts) => {
126
126
  let opts = opts->ReventlessCore.Util.Pulumi.ComponentResourceOptions.toCustomResourceOptions
127
127
 
128
+ // A task bucket holds transient input a slice consumes, so disposability
129
+ // follows the stack, exactly as it does for declared stores. It matters
130
+ // because a bucket name change is a *replace*, and a replace of a non-empty
131
+ // bucket fails the deploy with `BucketNotEmpty` — which would wedge precisely
132
+ // the throwaway stacks that are recreated most often. Protected stacks keep
133
+ // the safe default and drain by hand.
134
+ let forceDestroy =
135
+ Util_StoreLayout.protectionFor(~stack=Pulumi.Pulumi.getStackName()) ==
136
+ Util_StoreLayout.Unprotected
137
+
128
138
  let bucket = {
129
139
  PulumiAws.S3.Bucket.make(
130
140
  ~name,
131
141
  ~args={
142
+ forceDestroy: forceDestroy->Pulumi.Input.make,
132
143
  corsRules: [
133
144
  {
134
145
  PulumiAws.S3.Bucket.allowedHeaders: ["*"],
@@ -11,6 +11,7 @@ import * as Adapter$ReventlessCore from "@reventlessdev/reventless-core/src/adap
11
11
  import * as PolicyDocument$PulumiAws from "@reventlessdev/rescript-pulumi-aws/src/IAM/PolicyDocument.res.mjs";
12
12
  import * as Util_Pulumi$ReventlessCore from "@reventlessdev/reventless-core/src/util/Util_Pulumi.res.mjs";
13
13
  import * as Adapter_Helpers$ReventlessAws from "../Adapter_Helpers.res.mjs";
14
+ import * as Util_StoreLayout$ReventlessAws from "../../util/Util_StoreLayout.res.mjs";
14
15
  import * as TaskBucket_S3_Runtime$ReventlessAws from "./TaskBucket_S3_Runtime.res.mjs";
15
16
 
16
17
  function subscribeLambda2S3Bucket(lambda, name, bucket, opts) {
@@ -75,6 +76,7 @@ function connect(name, bucket, bucketMode, commandTopics, runtime, opts) {
75
76
 
76
77
  function make(name, opts) {
77
78
  let opts$1 = Util_Pulumi$ReventlessCore.ComponentResourceOptions.toCustomResourceOptions(opts);
79
+ let forceDestroy = Util_StoreLayout$ReventlessAws.protectionFor(Pulumi.getStack(), undefined) === "Unprotected";
78
80
  let bucket = new (Aws.s3.Bucket)(name, {
79
81
  corsRules: [{
80
82
  allowedHeaders: ["*"],
@@ -93,6 +95,7 @@ function make(name, opts) {
93
95
  ],
94
96
  maxAgeSeconds: 3000
95
97
  }],
98
+ forceDestroy: forceDestroy,
96
99
  tags: AWS_Tags$ReventlessAws.make(name, Task$ReventlessCore.componentType, {
97
100
  TAG: "Other",
98
101
  _0: "Bucket"
@@ -0,0 +1,361 @@
1
+ // The claim component: an uploaded object stops being provisional when a
2
+ // committed event references it.
3
+ //
4
+ // Deploy-time half of [docs/plans/done/upload-pending-claim-and-expiry.md] Step 2.
5
+ // `make` registers one event log whose events declare at least one
6
+ // `@storageRef` field; `finish` builds the single Lambda that serves all of
7
+ // them, its stream subscriptions, its IAM, and the alarm on how far behind it
8
+ // is. Registered from the platform's subscription hook and finished from inside
9
+ // it, for the same reason `StateTopic_AppSync` is: the hook fires from within a
10
+ // `Pulumi.Output.apply` chain, so a `finish` called after `P.make()` returns
11
+ // would drain an empty registry.
12
+ //
13
+ // It is not a plugin's concern in the domain sense — a ref minted by one plugin
14
+ // can be referenced by another's event, and no plugin owns a store's contents —
15
+ // but it is provisioned per plugin stack, because that is where the event log
16
+ // tables and their streams exist. The store side (bucket and served prefix)
17
+ // crosses in from the platform stack, which is where stores are provisioned.
18
+ //
19
+ // Input breadth is the thing to guard. Only event logs with a declared
20
+ // ref-bearing field are registered, and within one only the declared fields are
21
+ // read; if this ever grows toward "read every event" it has become a projection
22
+ // and belongs in that machinery instead.
23
+
24
+ open PulumiAws
25
+
26
+ // ── Registry ─────────────────────────────────────────────────────────────────
27
+
28
+ type entry = {
29
+ /** The event log's DynamoDB table. Doubles as the routing key: the handler
30
+ recovers it from each record's `eventSourceARN`. */
31
+ tableName: Pulumi.Output.t<string>,
32
+ streamArn: Pulumi.Output.t<string>,
33
+ refFields: array<ReventlessCore.StorageRefFields.eventRefFields>,
34
+ }
35
+
36
+ /** One registry per plugin — one claimer Lambda per plugin stack. `make`
37
+ appends, `finish` drains. */
38
+ let registry: dict<array<entry>> = Dict.make()
39
+
40
+ /** Event logs that declare a ref-bearing field but publish through a channel
41
+ this component cannot read (an SNS-backed event topic rather than a
42
+ DynamoDB stream). Their objects stay tagged forever, which is safe while the
43
+ lifecycle rule is off and data loss once it is on — so the deploy says so. */
44
+ let unreachable: dict<array<string>> = Dict.make()
45
+
46
+ let log = ReventlessCore.Logger.fromEnv()
47
+
48
+ let appendTo = (reg: dict<array<'a>>, key: string, value: 'a) =>
49
+ reg->Dict.set(key, reg->Dict.get(key)->Option.getOr([])->Array.concat([value]))
50
+
51
+ // ── Registration ─────────────────────────────────────────────────────────────
52
+
53
+ /**
54
+ Register one event log with the plugin's claimer.
55
+
56
+ `~eventSchema` is read for `@storageRef` declarations; an event log with none is
57
+ not registered at all, so a platform whose plugins declare no store provisions
58
+ no claimer. `~plugin` qualifies a store the annotation left unqualified, which
59
+ is the same resolution the deploy used when it provisioned the store.
60
+
61
+ `~isStreamBacked` says whether the event log's topic publishes through a
62
+ DynamoDB stream — the default for both aggregate and DCB logs — rather than
63
+ SNS. Decided by the caller because the platform already makes exactly that
64
+ call synchronously from `EventTopicPublisher_SNS`'s registry; asking the
65
+ resource here would only get the answer inside an `apply`, which is after
66
+ `finish` has drained the registry.
67
+ */
68
+ let make = (
69
+ ~plugin: string,
70
+ ~eventLogName: string,
71
+ ~eventSchema: S.t<unknown>,
72
+ ~eventTopicOutputs: ReventlessInfra.EventTopic.outputs,
73
+ ~isStreamBacked: bool,
74
+ ) => {
75
+ let refFields = ReventlessCore.StorageRefFields.fromEventSchema(~plugin, eventSchema)
76
+ if refFields->Array.length > 0 {
77
+ // An SNS-backed topic carries the same events, but not on a channel this
78
+ // Lambda subscribes to. Recorded as a gap rather than skipped in silence:
79
+ // its objects would keep the pending tag forever, which reads as "working"
80
+ // right up until a store enables expiry.
81
+ switch isStreamBacked ? eventTopicOutputs.resources->Array.get(0) : None {
82
+ | None => unreachable->appendTo(plugin, eventLogName)
83
+ | Some(streamResource) =>
84
+ registry->appendTo(
85
+ plugin,
86
+ {
87
+ tableName: streamResource.name,
88
+ streamArn: streamResource->Util_DynamoDbStream.streamArnFromDynamoDbTableResource,
89
+ refFields,
90
+ },
91
+ )
92
+ }
93
+ }
94
+ }
95
+
96
+ // ── Finalize ─────────────────────────────────────────────────────────────────
97
+
98
+ /** A store the claimer may untag in, resolved to what the handler needs. */
99
+ type storeConfig = {
100
+ qualified: string,
101
+ bucketName: string,
102
+ servedPrefix: string,
103
+ }
104
+
105
+ /**
106
+ Build the plugin's claimer from everything `make` registered.
107
+
108
+ `~stores` is the platform's declared object stores — in a plugin stack they
109
+ arrive from the platform stack's `objectStores` export, which is why it is an
110
+ Output. A thunk rather than the Output itself because most plugins register
111
+ nothing here, and resolving another stack's export to build a claimer that is
112
+ not going to exist is work with no reader.
113
+
114
+ `~iteratorAgeAlarmMs` is the lag the alarm fires at. Lag is the one failure mode
115
+ that deletes data: if this Lambda stops and nobody notices, referenced objects
116
+ keep the tag and an enabled lifecycle rule expires them. The default is an hour
117
+ against an expiry measured in days, so a stall is noticed with room to fix it.
118
+ */
119
+ let finish = (
120
+ ~plugin: string,
121
+ ~stores: unit => Pulumi.Output.t<array<storeConfig>>,
122
+ ~iteratorAgeAlarmMs: float=3_600_000.,
123
+ ~opts: Pulumi.CustomResourceOptions.t,
124
+ ) => {
125
+ switch unreachable->Dict.get(plugin)->Option.filter(logs => logs->Array.length > 0) {
126
+ | Some(logs) =>
127
+ log.warn(
128
+ ~comp="Upload_Claim",
129
+ `event log(s) ${logs->Array.join(", ")} declare storage-ref fields but publish through an ` ++
130
+ `SNS event topic, which the claimer does not subscribe to — objects those events reference ` ++
131
+ `keep the pending tag. Do not enable a store's expiry rule while this is true.`,
132
+ )
133
+ unreachable->Dict.set(plugin, [])
134
+ | _ => ()
135
+ }
136
+
137
+ switch registry->Dict.get(plugin) {
138
+ | None | Some([]) => ()
139
+ | Some(entries) =>
140
+ let name = plugin ++ "UploadClaim"
141
+ let stores = stores()
142
+
143
+ let lambdaRole = IAM.Role.makeWithDefaultPolicy(
144
+ ~name=name ++ "Role",
145
+ ~servicePrincipal=AWS.Lambda.principal->Pulumi.Output.make,
146
+ ~tags=AWS.Tags.make(
147
+ ~name=name ++ "Role",
148
+ ~kind=ReventlessCore.ComponentType.Plugin,
149
+ ~role=Identity,
150
+ ~component=name,
151
+ ),
152
+ ~opts,
153
+ )
154
+
155
+ // Read every registered stream; tag and untag under every declared store's
156
+ // served prefix, never a whole bucket. `GetObjectTagging` is what makes the
157
+ // untag a read-modify-write that preserves a deployment's own tags rather
158
+ // than a `DeleteObjectTagging` that would take them with it.
159
+ let _policy =
160
+ (entries->Array.map(e => e.streamArn)->Pulumi.Output.all, stores)
161
+ ->Pulumi.Output.all2
162
+ ->Pulumi.Output.apply(((streamArns, stores)) => {
163
+ open PolicyDocument
164
+ let objectArns =
165
+ stores->Array.map(s => `arn:aws:s3:::${s.bucketName}/${s.servedPrefix}/*`)
166
+ let _rolePolicy = IAM.RolePolicy.make(
167
+ ~name=name ++ "Policy",
168
+ ~args={
169
+ policy: PolicyDocument.make(
170
+ ~id=name ++ "Policy",
171
+ ~statements=[
172
+ {
173
+ sid: "AllowLambdaLogging",
174
+ effect: Allow,
175
+ actions: Actions([
176
+ "logs:CreateLogGroup",
177
+ "logs:CreateLogStream",
178
+ "logs:PutLogEvents",
179
+ ]),
180
+ resources: Resource("arn:aws:logs:*:*:*"),
181
+ },
182
+ {
183
+ sid: "AllowReadEventLogStream",
184
+ effect: Allow,
185
+ actions: Actions([
186
+ "dynamodb:DescribeStream",
187
+ "dynamodb:GetRecords",
188
+ "dynamodb:GetShardIterator",
189
+ "dynamodb:ListStreams",
190
+ ]),
191
+ resources: Resources(streamArns),
192
+ },
193
+ {
194
+ sid: "AllowClaimObjectTagging",
195
+ effect: Allow,
196
+ actions: Actions(["s3:GetObjectTagging", "s3:PutObjectTagging"]),
197
+ resources: Resources(objectArns),
198
+ },
199
+ ],
200
+ )
201
+ ->PolicyDocument.toJsonString
202
+ ->Pulumi.Input.make,
203
+ role: lambdaRole.id->Pulumi.Output.asInput,
204
+ },
205
+ ~opts,
206
+ )
207
+ })
208
+
209
+ // `CLAIM_REF_FIELDS` — `{ <tableName>: { <eventType>: [refField] } }`. The
210
+ // whole of the handler's input: a table absent here is a table it reads no
211
+ // record from.
212
+ let refFieldsJson =
213
+ entries
214
+ ->Array.map(e => e.tableName)
215
+ ->Pulumi.Output.all
216
+ ->Pulumi.Output.apply(tableNames => {
217
+ let byTable = Dict.make()
218
+ tableNames->Array.forEachWithIndex((tableName, i) =>
219
+ byTable->Dict.set(
220
+ tableName,
221
+ ReventlessCore.StorageRefFields.toJson((entries->Array.getUnsafe(i)).refFields),
222
+ )
223
+ )
224
+ byTable->JSON.Encode.object->JSON.stringify
225
+ })
226
+
227
+ // `UPLOAD_STORES` — the same qualified-name → {bucket, prefix} shape the
228
+ // presign service reads, so mint and claim resolve a store identically.
229
+ let uploadStoresJson =
230
+ stores->Pulumi.Output.apply(stores =>
231
+ stores
232
+ ->Array.map(s => (
233
+ s.qualified,
234
+ Dict.fromArray([
235
+ ("bucket", JSON.Encode.string(s.bucketName)),
236
+ ("prefix", JSON.Encode.string(s.servedPrefix)),
237
+ ])->JSON.Encode.object,
238
+ ))
239
+ ->Dict.fromArray
240
+ ->JSON.Encode.object
241
+ ->JSON.stringify
242
+ )
243
+
244
+ // Compiled EntryPoint rather than a serialized closure, for the reason the
245
+ // presign service is one: the handler reaches the AWS SDK v3 S3 client, and
246
+ // a serialized closure bakes the deploy machine's SDK internals into an
247
+ // archive that then resolves its transitives from independently-versioned
248
+ // layer sources that disagree at cold start. The bundled `_Ops` module keeps
249
+ // bare `@aws-sdk/*` imports and resolves them through the resolve-hook, so
250
+ // one internally consistent SDK loads.
251
+ let packageDirs = Dict.fromArray([
252
+ (
253
+ "@reventlessdev/reventless-aws",
254
+ Util_Bundle.resolvePackageRoot("@reventlessdev/reventless-aws"),
255
+ ),
256
+ ])
257
+ let {code, sourceCodeHash} = Util_Bundle.buildCodeArchive(
258
+ ~entryPointModule="@reventlessdev/reventless-aws/src/adapter/Upload/Upload_Claim_S3_Ops.res.mjs",
259
+ ~packageDirs,
260
+ )
261
+
262
+ let layers =
263
+ Lambda.reventlessLayerArn
264
+ ->Option.map(arn => [arn->Pulumi.Input.make])
265
+ ->Option.getOr([])
266
+ ->Pulumi.Input.make
267
+
268
+ let lambda = Lambda.Function.make(
269
+ ~name,
270
+ ~args={
271
+ handler: "index.handler"->Pulumi.Input.make,
272
+ runtime: "nodejs22.x"->Pulumi.Input.make,
273
+ code: code->Pulumi.Input.make,
274
+ sourceCodeHash: sourceCodeHash->Pulumi.Input.make,
275
+ role: lambdaRole.arn->Pulumi.Output.asInput,
276
+ memorySize: 256->Pulumi.Input.make,
277
+ timeout: 60->Pulumi.Input.make,
278
+ layers,
279
+ tags: AWS.Tags.make(
280
+ ~name,
281
+ ~kind=ReventlessCore.ComponentType.Plugin,
282
+ ~role=Runtime,
283
+ ~component=name,
284
+ ),
285
+ environment: (
286
+ {
287
+ Lambda.Function.variables: Dict.fromArray([
288
+ ("Environment", Pulumi.Pulumi.getStackName()->Pulumi.Input.make),
289
+ ("UPLOAD_STORES", uploadStoresJson->Pulumi.Output.asInput),
290
+ ("CLAIM_REF_FIELDS", refFieldsJson->Pulumi.Output.asInput),
291
+ ("NODE_OPTIONS", Util_Bundle.esmLoaderNodeOptions->Pulumi.Input.make),
292
+ ("ESM_FALLBACK_DIRS", Util_Bundle.esmFallbackDirs->Pulumi.Input.make),
293
+ ]),
294
+ }: Lambda.Function.functionEnvironment
295
+ )->Pulumi.Input.make,
296
+ },
297
+ ~opts,
298
+ )
299
+
300
+ // One mapping per event log stream, all targeting the one Lambda.
301
+ // `maximumRetryAttempts` bounds a permanently failing record so it cannot
302
+ // wedge the shard — the default of -1 retries forever, which would turn one
303
+ // bad record into unbounded claim lag, which is the failure that deletes
304
+ // data. `bisectBatchOnFunctionError` isolates it across those retries.
305
+ entries->Array.forEachWithIndex((entry, i) => {
306
+ let esmName = `${name}Stream${i->Int.toString}`
307
+ let _esm = EventSourceMapping.make(
308
+ ~name=esmName,
309
+ ~args={
310
+ EventSourceMapping.functionName: lambda.arn->Pulumi.Output.asInput,
311
+ eventSourceArn: entry.streamArn->Pulumi.Output.asInput,
312
+ startingPosition: LATEST,
313
+ maximumRetryAttempts: 3,
314
+ bisectBatchOnFunctionError: true,
315
+ tags: AWS.Tags.make(
316
+ ~name=esmName,
317
+ ~kind=ReventlessCore.ComponentType.Plugin,
318
+ ~role=EventSourceMapping,
319
+ ~component=name,
320
+ ),
321
+ },
322
+ ~opts=Some(opts),
323
+ )
324
+ })
325
+
326
+ // How far behind the claimer is, alarmed rather than merely emitted.
327
+ // `IteratorAge` is the stream consumer's own lag and needs no custom metric;
328
+ // `treatMissingData: "notBreaching"` keeps a quiet platform — no events, so
329
+ // no data points — out of alarm.
330
+ let _alarm = Cloudwatch.MetricAlarm.make(
331
+ ~name=name ++ "Lag",
332
+ ~args={
333
+ comparisonOperator: "GreaterThanThreshold"->Pulumi.Input.make,
334
+ evaluationPeriods: 3->Pulumi.Input.make,
335
+ metricName: "IteratorAge"->Pulumi.Input.make,
336
+ namespace: "AWS/Lambda"->Pulumi.Input.make,
337
+ period: 300->Pulumi.Input.make,
338
+ statistic: "Maximum"->Pulumi.Input.make,
339
+ threshold: iteratorAgeAlarmMs->Pulumi.Input.make,
340
+ dimensions: lambda.name
341
+ ->Pulumi.Output.apply(fn => Dict.fromArray([("FunctionName", fn)]))
342
+ ->Pulumi.Output.asInput,
343
+ treatMissingData: "notBreaching"->Pulumi.Input.make,
344
+ alarmDescription: (`${name} is behind on committed events. While it is behind, objects an ` ++
345
+ `event already references still carry the pending tag — and a store with an expiry rule ` ++
346
+ `enabled will delete them.`)->Pulumi.Input.make,
347
+ tags: AWS.Tags.make(
348
+ ~name=name ++ "Lag",
349
+ ~kind=ReventlessCore.ComponentType.Plugin,
350
+ ~role=Other("Alarm"),
351
+ ~component=name,
352
+ ),
353
+ },
354
+ ~opts,
355
+ )
356
+
357
+ // Drain, so a second construction in the same process (tests, dev reload)
358
+ // starts from empty rather than re-registering every stream.
359
+ registry->Dict.set(plugin, [])
360
+ }
361
+ }