- JavaScript Cloud Native Development Cookbook
- John Gilbert
- 454字
- 2021-07-16 18:03:25
How it works...
The Serverless Framework handles the heavy lifting, which allows us to focus on writing the actual function code. The first thing to note is that we must define the runtime: nodejs8.10 in the serverless.yml file. Next, we define a function in the functions section with a name and a handler. All other settings have defaulted, following the configuration by exception approach. When you look at the generated CloudFormation template, you will see that over 100 lines were generated from just a handful of lines declared in the serverless.yml file. A large portion of the generated template is dedicated to defining boilerplate security policies. Dig into the .serverless/cloudformation-template-update-stack.json file to see the details.
We also define environment variables in the serverless.yml. This allows the functions to be parameterized per deployment stage. We will cover this in more detail in Chapter 6, Building a Continuous Deployment Pipeline. This also allows settings, such as the debug level, to be temporarily tweaked without redeploying the function.
When we deploy the project, the Serverless Framework packages the function along with its runtime dependencies, as specified in the package.json file, into a ZIP file. Then, it uploads the ZIP file to the ServerlessDeploymentBucket so that it can be accessed by CloudFormation. The output of the deployment command shows when this is happening. You can look at the content of the ZIP file in the .serverless directory or download it from the deployment bucket. We will cover advanced packaging options in Chapter 9, Optimizing Performance.
The signature of an AWS Lambda function is straightforward. It must export a function that accepts three arguments: an event object, a context object, and a callback function. Our first function will just log the event, content, and the environment variables so that we can peer into the execution environment a little bit. Finally, we must invoke the callback. It is a standard JavaScript callback. We pass an error to the first argument or the successful result to the second argument.
Logging is an important standard feature of Function as a Service (FaaS). Due to the ephemeral nature of cloud resources, logging in the cloud can be tedious, to put it lightly. In AWS Lambda, console logging is performed asynchronously and recorded in CloudWatch logs. It's a fully-managed logging solution built right in. Take the time to look at the details in the log statements that this function writes. The environment variables are particularly interesting. For example, we can see that each invocation of a function gets a new temporary access key.
Functions also provide a standard set of metrics out-of-the-box, such as invocation count, duration, errors, throttling, and so forth. We will cover this in detail in Chapter 7, Optimizing Observability.
- Photoshop移動UI設(shè)計完全實例教程
- Rust Standard Library Cookbook
- Altium Designer Summer 09電路設(shè)計與制作
- 從算法到電路:數(shù)字芯片算法的電路實現(xiàn)
- 電子工程師自學(xué)速成:設(shè)計篇(第2版)
- 智能電網(wǎng)技術(shù):面向供需互動能量優(yōu)化
- 高速數(shù)字電路設(shè)計入門
- 通信工程設(shè)計
- 整機(jī)裝聯(lián)工藝與技術(shù)
- 鯤鵬生態(tài)應(yīng)用開發(fā)
- 風(fēng)云三號氣象衛(wèi)星地面應(yīng)用系統(tǒng)工程技術(shù)和實踐
- 電子對抗原理
- iOS應(yīng)用軟件設(shè)計之道
- 軟件創(chuàng)富密碼:iPhone應(yīng)用程序開發(fā)攻略之深入淺出Objective-C 2.0
- 5G賦能:行業(yè)應(yīng)用與創(chuàng)新