Best practice Lambda function

Function Code

Separating Lambda handler (entry point) from the core logic.  This allows you to more easily create a function of unit testing.

  • Re-using the context to improve execution performance of a function.  Make sure that your code is retrieved externalize configuration or dependency after the initial implementation of local storage and references. Constraint variables / object reinitialized at each call, but the use of static initialization / constructor, the global / static variables and a single embodiment. Remains active and re-use the connection (HTTP, database, etc.) on the establishment of a single call.

  • Use  AWS Lambda environment variable  operating parameters passed to your function.  For example, when writing your Amazon S3 bucket, bucket name should not be written to the hard-coded, and bucket name should be configured as an environment variable.

  • Deployment control function in dependency package. AWS Lambda execution environment includes several libraries, such as for Node.js and Python runtime AWS Development Kit (complete list is located here: AWS Lambda runtime ). Lambda regularly update these libraries to support the latest security updates and feature set. These updates may cause subtle changes in the function of the Lambda behavior. To fully control your function dependencies used, it is recommended that all package dependencies in the deployment package.

  • The deployment package size is necessary to streamline contains only part of the time run.  This will reduce the time prior to the call to download and decompress the deployment packages you need. For functions written in Java or .NET Core, please do the entire AWS SDK library to upload the deployment as part of the package, but to selectively choose the development kit components (such as DynamoDB according to the desired module, Amazon S3 module development kit and  Lambda core library ).

  • The dependence of  .jar the files in separate / lib directory, can reduce deployment package Lambda decompressor (written in Java) the time required . This has the large ratio of all the function code is placed  .class in the same jar file faster. For instructions, see the  AWS Lambda deployment package in Java .

  • The dependence of complexity to a minimum.  Preferred execution context more simple framework can be quickly loaded at startup. For example, more preferred simple Java dependency injection (IoC) framework, such as  Dagger  or  Guice , rather than the more complex  the Spring Framework .

  • Avoid using recursive code Lambda function , because if the code using a recursive function calls itself automatically, until meet some arbitrary criteria. This may lead to an unexpected amount of function calls and upgrade costs. If you do so accidentally, please immediately execute the function limits the number of concurrent set  0 to restrict all calls to the function, while updating the code.

Guess you like

Origin www.cnblogs.com/cloudrivers/p/11617938.html