Hi, everyone :wave: We're using Pulumi to set up ...
# general
b
Hi, everyone 👋 We're using Pulumi to set up a new project (and we absolutely love it). Though, we hit an issue that's giving us a bit of gray hair (something that was easily achieved using the Serverless framework). We have a lambda that is very read-extensive, thus, "caching" some data in-memory between invocations is what we're trying to achieve. Simplified example:
Copy code
let cachedValue = '';

exports.handler = async () => {
  if (cachedValue) {
    return cachedValue;
  }

  const newValue = await getSomeNewValue();
  cachedValue = newValue;
  return newValue;
}
But this doesn't seem to work with Pulumi 😬 We have a callback defined like:
Copy code
let cachedValue = '';

export const handler: aws.lambda.Callback<Event, unknown> = async (event, context, callback) => {
  // ...
}
And we're using it as an AppSync resolver:
Copy code
const cbFunction = new aws.lambda.CallbackFunction('cb', {
  runtime: 'nodejs14.x',
  callback: handler,
}

const dataSource = new aws.appsync.DataSource('some-data-source', {
  ...
  lambdaConfig: {
    functionArn: cbFunction.arn,
  },
});
But we cannot make this mechanism work. The
cachedValue
is refreshed on each invocation. I know Pulumi does function serialization, which is a concept I'm still trying to wrap my head around. Any quick tips on how to access the execution environment in a lambda like this? (Also, yes, I'm aware it's not a reliable way to do caching, lambdas are supposed to be stateless, data is not safe, etc etc). We do have a use case for this, though. Any help would be appreciated 😁
In case anyone is wondering: Use the
callbackFactory
field instead of
callback
to scope whatever should be cached. Thanks.