The SMS API requires 2 requests… one to their /token endpoint to get an auth token valid for 20 mins, and another to actually send the SMS (passing in the auth token).Ĭurrently I am making both calls one after the other on every lambda invocation which feels wasteful and inefficient. I’m currently making a call to an external service for sending SMS messages, from a lambda function. I’m trying to figure out if Lambda execution context is the “right” solution for my issue. I actually found this article while searching for a solution to something, got halfway through the article before realised what/who’s site I was on… I was actually in the audience for your presentation at #BFSServerless just a couple of days ago! This would allow you to only connect to the database when needed and not for every call. You could then call nnect() and that would actually make the database connection for you. The module could return a object that stores the connection and a method to create a connection. Something like const myPostgres = require('./myPostgresModule.js'). So for instance, you could create a module that handled all of your Postgres db interactions and require that outside of the main handler function. Therefore, requiring a module (outside of the main handler) that uses pg-promise to create a connection should behave as you noted above: reuse the connection until Lambda expires, and then create a new connection on the next cold start.Īlso note, the connection doesn’t actually need to be created outside of the main handler, just a global variable that will store the reference. The serverless-mocha-plugin doesn’t provide the IS_LOCAL environment variable by default, but you can assign it to one of your stages and then run sls invoke test -f myFunction -s local.ĪWS Lambda will “freeze” any variable or connection that is outside of your main handler function’s scope. I put this at the end of my main handler: The best solution I’ve found to overcome the context.callbackWaitsForEmptyEventLoop = false issue when running tests locally is to conditionally execute code if I’m in the test environment. This will allow you to freeze the connection while still having control over when it connects. I’m not sure what you’re using for a database connection, but you should instantiate your Class outside of your handler, and then create a connect() method that gets triggered inside your handler. To answer your Stack Overflow question, try this post I wrote about stubbing AWS services: The Lambda documentation tells you to keep your variable declarations inside your handler function. Lambda does handle setting up DB connections really well under heavy load, but I still favor connection reuse as it cuts several milliseconds off your execution time. Cold starts still average greater than 100ms. Update : After running some new tests, it appears that “warm” functions now average anywhere between 4 and 20ms to connect to RDS instances in the same VPC. Luckily, Lambda lets us “freeze” and then “thaw” these types of connections. Add that to your queries and whatever additional processing you need to perform and it becomes unusable under normal circumstance. If we have to reconnect to the database every time we run our Lambda functions (especially if we’re responding to an API request) then we are already adding over 200ms to the total response time. In my experience it typically takes more than 200ms. Setting up a new database connection is relatively expensive. However, since Lambda is stateless, you’ll most likely need to query a persistent datastore in order for it to do anything exciting. There’s no longer the need to run several underutilized processing servers just waiting for someone to request a large job.ĪWS Lambda is event-driven, so it’s also possible to have it respond to API requests through AWS’s API Gateway. FaaS is great for a number of use cases (like processing images) because it will scale immediately and near infinitely when there are spikes in traffic. This is what’s known as a “ Serverless” architecture since we do not need to provision any servers in order to run these functions. The ability to use this Functions-as-a-Service (FaaS) has dramatically reduced the complexity and hardware needs of the apps I work on. Update : I wrote an NPM module that manages MySQL connections for you in serverless environments.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |