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

white-balloon-205

10/02/2018, 8:21 PM
AFAIK one has to
require
any modules inside the callback.
This is no longer true actually (as of a couple releases ago).
How do I make instances reusable between requests then, e.g. a request with a static agent?
What do you have in mind here exactly? Code that only runs once per warm-up, not once per request?
t

tall-librarian-49374

10/02/2018, 8:22 PM
Oh... I thought I got an error when tried to use
request
from the top of the file the other day. Yes, the code that runs once on warm-up, to reuse HTTP connections.
l

lemon-spoon-91807

10/02/2018, 8:23 PM
there are a couple of things that you can do here.
first, you can import the module like normal, then reference it inside your functions.
second, if you want to run warmup code, we have a mechanism ofr that as well, but i'd need to see your scenario/code to give advice on how to deal with this
basically, we have something we call a "factoryFunction" which runs once when the Lambda/Module loads and has time to set things up and then define the actual entrypoint function that will trigger each time the Lambda is hit.
t

tall-librarian-49374

10/02/2018, 8:26 PM
The scenario is simple:
Copy code
// do once
const http = require("http");
const httpAgent = new http.Agent({ keepAlive: true });
// do at invoke
request({ url: url, pool: httpAgent }...
Ah, nice, I'll have a look at factories
l

lemon-spoon-91807

10/02/2018, 8:26 PM
right. perfect scenario for this
let me show an example
i'm also landing in a plane
so i may lose connection and be offline a while
this is no-arg function that, whne invoked, will produce the actual Lambda entry function.
you can see we pass that no-arg function to createFactoryFunction
which basically just passes hte isFactoryFunc bool down
t

tall-librarian-49374

10/02/2018, 8:29 PM
seems clear
l

lemon-spoon-91807

10/02/2018, 8:29 PM
as you descend downwards into the lower level pulumi code, it understands how to properly generate this so you basically get:
module.exports = factoryFunc();
instead of
module.exports = func;