https://pulumi.com logo
#general
Title
# general
f

full-dress-10026

08/03/2023, 10:05 PM
Has anyone managed to get a function using Zod serialized with Pulumi? I’ve been trying with no luck…🧵
Say I have a sns topic with an onEvent handler…
Copy code
const AlarmEventSchema = z.object({
  AlarmName: z.string(),
  AWSAccountId: z.string(),
  NewStateValue: z.enum(['ALARM', 'OK']),
  NewStateReason: z.string(),
  StateChangeTime: z.string(),
  Region: z.string(),
  OldStateValue: z.enum(['ALARM', 'OK']),
  Trigger: z.object({
    MetricName: z.string(),
    Namespace: z.string(),
  }),
});

const alarmTopic = new aws.sns.Topic(`${name}-alarm-topic`, {}, { provider, parent: this });

alarmTopic.onEvent(
      `${name}-handle-event`,
      async (ev) => {
        const proms = ev.Records.map(async (record) => {
          const alarmEvent = AlarmEventSchema.parse(JSON.parse(record.Sns.Message));
        });
        await Promise.all(proms);
      },
      undefined,
      { provider, parent: this },
    )
If I try and create this, I get the following error.
Copy code
error: Error serializing '(ev) => __awaiter(this, void 0, void ...': slack-alerts.ts(57,51)

    '(ev) => __awaiter(this, void 0, void ...': slack-alerts.ts(57,51): captured
      variable 'AlarmEventSchema' which indirectly referenced
        function 'bound parse': which could not be serialized because
          it was a native code function.

    Function code:
      function () { [native code] }
I’ve seen this workaround, but it does not seem to work for me. Wrapping the schema in a function like suggested there results in the following error.
Copy code
error: Running program '/Users/kenny/work/kwllc/deeporigin/infrastructure/stacks/environment/cur-changes-processor/' failed with an unhandled exception:
    <ref *1> Error: package.json export path for ".pnpm/zod@3.21.4/node_modules/zod/lib/index.js" not found
        at ModuleMap.get (/Users/kenny/work/kwllc/deeporigin/infrastructure/common/temp/node_modules/.pnpm/@pulumi+pulumi@3.72.2/node_modules/@pulumi/runtime/closure/package.ts:220:19)
        at Object.getModuleFromPath (/Users/kenny/work/kwllc/deeporigin/infrastructure/common/temp/node_modules/.pnpm/@pulumi+pulumi@3.72.2/node_modules/@pulumi/runtime/closure/package.ts:273:35)
        at /Users/kenny/work/kwllc/deeporigin/infrastructure/common/temp/node_modules/.pnpm/@pulumi+pulumi@3.72.2/node_modules/@pulumi/runtime/closure/createClosure.ts:1242:19
        at Generator.next (<anonymous>)
        at /Users/kenny/work/kwllc/deeporigin/infrastructure/common/temp/node_modules/.pnpm/@pulumi+pulumi@3.72.2/node_modules/@pulumi/pulumi/runtime/closure/createClosure.js:21:71
        at new Promise (<anonymous>)
        at __awaiter (/Users/kenny/work/kwllc/deeporigin/infrastructure/common/temp/node_modules/.pnpm/@pulumi+pulumi@3.72.2/node_modules/@pulumi/pulumi/runtime/closure/createClosure.js:17:12)
        at captureModuleAsync (/Users/kenny/work/kwllc/deeporigin/infrastructure/common/temp/node_modules/.pnpm/@pulumi+pulumi@3.72.2/node_modules/@pulumi/pulumi/runtime/closure/createClosure.js:882:20)
        at /Users/kenny/work/kwllc/deeporigin/infrastructure/common/temp/node_modules/.pnpm/@pulumi+pulumi@3.72.2/node_modules/@pulumi/runtime/closure/createClosure.ts:935:19
        at Generator.next (<anonymous>) {
      promise: Promise { <rejected> [Circular *1] }
    }
e

elegant-window-55250

08/04/2023, 7:51 AM
You probably need to move the Zod function into the handler