Scenario: You need to develop an application with a separate front and back end. RESTful apis are most convenient for the back end, but what is the easiest architecture to use if the back end service is likely to have high concurrency at certain times of day?

When I first thought about this problem, I thought of several possible solutions:

  • Use CDN content delivery network to reduce stress on primary servers

  • Use LVS server load balancing

  • Use the cache

  • The hardware layer uses higher bandwidth, SSDS, and better servers

  • Code layer, optimize the code (use a better performance language, etc

However, all of these methods require attention to the storage and computing resources of the server so that they can be adjusted to meet higher performance, and high concurrent requests are timed, so a server configured with higher performance wastes resources when access volume is low.

FaaS (Functions as a Service) architectures can be used for this purpose. Unlike traditional architectures, they run in stateless containers, can be triggered by events, are transient, and are completely managed by third parties. Functionally, FaaS do not care about back-end servers or application services. Just care about your own code. AWS Lambda is one of the best FaaS implementations available.

AWS Lambda

AWS Lambda is a computing service that runs code without having to preconfigure or manage a server. AWS Lambda executes code only when needed and scales automatically. With AWS Lambda, you can run code for almost any type of application or back-end service without performing any administration. AWS Lambda now supports node.js, Java, C#, and Python.

Usage scenarios

The common application scenarios of Lambda are as follows:

  • Use Lambda as an event source for AWS services (e.g. after audio is uploaded to S3, trigger Lambda audio transcoding service, transcoding audio files
  • On-demand Lambda function calls over HTTPS (Amazon API Gateway) (with API Gateway to create simple microservices)
  • On-demand Lambda function calls (build your own event sources with custom applications)
  • Planned events (such as generating reports to be sent to the specified mailbox at 12 PM every day

Here is an execution flowchart of using Lambda as an event source for an AWS service case:

  1. Description A user uploaded an object to an S3 bucket (object creation event).
  2. Amazon S3 detected an object creation event. Procedure
  3. Amazon S3 calls the Lambda function specified in the bucket notification configuration.
  4. AWS Lambda executes Lambda functions by substituting in the executing role that you specified when you created the Lambda function.
  5. Lambda function execution.

This article focuses on using Lambda as an event source for AWS services and creating simple microservices in conjunction with the API Gateway.

How to Use Lambda

The following is an example of how to use Lambda.

Use AWS Lambda with Amazon API Gateway (on demand and over HTTPS)

Step 1: Set up the AWS account and THE AWS CLI
  • Register an AWS account and create an administrator user in that account
  • Set the AWS Command Line Interface (AWS CLI)
Step 2: Create the HelloWorld Lambda function and explore the console
Create the Hello World Lambda function
  1. Log in to the AWS Administrative Console and open the AWS Lambda console.
  2. Select Get Started Now. The console displays the Get Started Now page only if no Lambda functions have been created. If you have created a function, you will see the Lambda > Functions page. On the list page, select Create a Lambda Function to go to the Lambda > New Function page. Here’s what happens

  1. Select Create from scratch, fill in the function name, select the role, and click Create Function
  2. Configure the created Lambda function

Note that the filling part of the handler is the code file name + the function name in the file. Here we fill in the file name lambda_function, the function name is lambda_handler, and the filling part of the handler is lambda_function.lambda_handler.

  1. To add the trigger, here we select API Gateway, in the configuration section before the configured API, click Add. And I’ll save the function

Test the AWS Lambda + Amazon API Gateway

Log in to the AWS console, open the API Gateway, select the API we just selected, click Test, and we will see the following output

Details you can refer to the official document (https://docs.aws.amazon.com/zh_cn/lambda/latest/dg/getting-started.html)

Now that we’ve seen how to use a Lambda function, let’s look at how to build a Lambda function.

How to Build a Lambda

Creating a Lambda function

When you create a Lambda function, you need to specify a handler (which is a function in the code) that AWS Lambda can call when the service executes the code. When you create a handler function in Python, use the following general syntax structure.

def handler_name(event, context):.return some_value
Copy the code

In this syntax, note the following:

  • Event-aws Lambda uses this parameter to pass event data to the handler. This parameter is usually of the Python dict type. It can also be of type list, STR, int, float, or NoneType.

  • Context-aws Lambda uses this parameter to provide runtime information to the handler. This parameter is of type LambdaContext.

  • (Optional) The handler can return a value. What happens to the returned value depends on the type of call used when calling the Lambda function:

    • If the RequestResponse invocation type is used (executed synchronously), AWS Lambda returns the result of the Python function call to the client that called the Lambda function (serialized as JSON in the HTTP response to the invocation request). For example, the AWS Lambda console uses the RequestResponse call type, so when you call a function using the console, the console displays the returned value.

      If the handler returns NONE, AWS Lambda returns null.

    • If the Event invocation type (asynchronous execution) is used, the value is discarded.

The context object

While executing Lambda functions, it can interact with AWS Lambda services to obtain useful runtime information, such as:

  • The amount of time left before AWS Lambda terminates your Lambda function (timeout is one of the Lambda function configuration properties).
  • The CloudWatch log group and log flow associated with the Lambda function being executed.
  • Returns the AWS request ID of the client that called the Lambda function. You can use this request ID for any follow-up queries to AWS Support.
  • If Lambda functions are called through the AWS Mobile Software development Kit, you can learn more about the mobile application that calls Lambda functions.
Context object method (Python)

The context object provides the following methods:

get_remaining_time_in_millis()

Returns the execution time (in milliseconds) remaining before the AWS Lambda terminating function.

Context object properties (Python)

The Context object provides the following properties:

function_name

The name of the Lambda function being executed.

function_version

The version of the Lambda function being executed. If an alias is used to call a function, function_version will be the version to which the alias points.

invoked_function_arn

ARN is used to call this function. It can be a function ARN or an alias ARN. The unqualified ARN executes the $LATEST version, and the alias executes the version of the function it points to.

memory_limit_in_mb

The memory limit (in MB) configured for Lambda functions. You set the memory limit when you create the Lambda function, and you can change the limit later.

aws_request_id

The AWS request ID associated with the request. This is the ID returned to the client that invoked the invoke method. Note that if AWS Lambda retries the call (for example, in the case of an exception thrown by a Lambda function that handles Kinesis records), the request ID remains the same.

log_group_name

The name of the CloudWatch log group from which you can find logs written by the Lambda function.

log_stream_name

The name of the CloudWatch log flow from which to find the logs written by the Lambda function. The log flow may or may not change each time the Lambda function is called. This value is null if the Lambda function cannot create the log flow. This can occur when the executing role that grants the necessary permissions to the Lambda function does not include permissions for CloudWatch Logs operations.

identity

Information about the Amazon Cognito identity provider for calls made through the AWS Mobile Software Development Kit. It can be null. identity.cognito_identity_ididentity.cognito_identity_pool_id

client_context

Information about client applications and devices at the time of invocation through the AWS Mobile Software Development Kit. It can be null. client_context.client.installation_idclient_context.client.app_titleclient_context.client.app_version_nameclient_context Client.app_version_codeclient_context.client.app_package_nameclient_context.custom Dict of custom values set by the mobile client application. Client_context. env DICT of environment information provided by the AWS Mobile Software development Kit.

The sample

Check out the following Python examples. It has a function, which is also a handler. Handlers receive runtime information through a Context object passed as a parameter.

from __future__ import print_function

import time
def get_my_log_stream(event, context):       
    print("Log stream name:", context.log_stream_name)
    print("Log group name:",  context.log_group_name)
    print("Request ID:",context.aws_request_id)
    print("Mem. limits(MB):", context.memory_limit_in_mb)
    # Code will execute quickly, so we add a 1 second intentional delay so you can see that in time remaining value.
    time.sleep(1) 
    print("Time remaining (MS):", context.get_remaining_time_in_millis())
Copy the code

The handler code in this example prints only part of the runtime information. Each print statement creates a log entry in CloudWatch. If you call a function using the Lambda console, the console displays the log.

logging

Your Lambda function can contain logging statements. AWS Lambda writes these logs to CloudWatch. If you call a Lambda function using the Lambda console, the console displays the same log.

The following Python statements generate log entries:

  • printStatements.
  • loggingIn the moduleLoggerFunction (for example,logging.Logger.infologging.Logger.error).

The print and logging.* functions write Logs to CloudWatch Logs, while the logging.* function writes additional information to each log entry, such as the timestamp and log level.

Find logs

The log written by the Lambda function can be found as follows:

  • These logs are displayed in the AWS Lambda console – the ** Log Output ** section of the AWS Lambda console.

  • In the response header, when you call the Lambda function programmatically – if you call the Lambda function programmatically, you can add the LogType parameter to retrieve the last 4 KB of log data that has been written to the CloudWatch log. AWS Lambda returns this log information in the X-AMZ-log-Results header of the response. See Invoke for more information.

    If you call this function using the AWS CLI, you can specify a –log-type parameter with the value Tail to retrieve the same information.

  • In CloudWatch Logs – To find your logs in CloudWatch, you need to know the log group name and the log flow name. You can use the context.logGroupName and context.logStreamName properties in your code to get this information. When running the Lambda function, the logs generated in the console or CLI will show you the log group name and the log flow name.

Function error

If a Lambda function throws an exception, AWS Lambda recognizes the failure, serializes the exception information into JSON, and returns it. Consider the following example:

def always_failed_handler(event, context):
    raise Exception('I failed! ')
Copy the code

When this Lambda function is called, it raises an exception, and AWS Lambda returns the following error message:

{
  "errorMessage": "I failed!"."stackTrace": [["/var/task/lambda_function.py".3."my_always_fails_handler"."raise Exception('I failed! ')"]],"errorType": "Exception"
}
Copy the code

Details refer to the official document: https://docs.aws.amazon.com/zh_cn/lambda/latest/dg/lambda-app.html

Matters needing attention

AWS Lambda limits

AWS Lambda imposes some limitations in usage, such as the size of a package or the amount of memory allotted to a Lambda function per call.

AWS Lambda resource limits per call

resources limit
Memory allocation range Minimum = 128 MB/ Maximum = 1536 MB (increments of 64 MB). If the maximum memory usage is exceeded, the function call will terminate.
Temporary disk capacity (“/TMP “space) 512MB
Number of file descriptors 1024
Number of processes and threads (combined total) 1024
Maximum execution time per request 300 seconds
InvokeRequest body payload size (RequestResponse/ synchronous call) 6MB
InvokeRequest body payload size (Event/ asynchronous call) 128 K

AWS Lambda account limits for each region

resources The default limit
Number of concurrent executions 1000

Concurrent execution is the number of executions of your function’s code at any given time. You can estimate the concurrent execution count, but it will vary depending on whether the Lambda function processes events from a stream-based event source.

  • Stream-based event sources – If you create Lambda functions to handle events from stream-based services (Amazon Kinesis Data Streams or DynamoDB Streams), the number of partitions per stream is a unit of concurrency. If your flow has 100 active partitions, at most 100 Lambda function calls will run concurrently. Each Lambda function then processes the events in the order in which the partitions arrive.

  • Non-stream-based event sources – If you create Lambda functions to handle events from event sources that are not stream-based (for example, Amazon S3 or API gateways), then each published event is a unit of work. Therefore, the number of events (or requests) published by these event sources affects concurrency.

    You can use the following formula to estimate the number of concurrent Lambda function calls.

    events (or requests) per second * function duration
    Copy the code

    For example, consider a Lambda function that handles an API Gateway. Assume that Lambda functions take an average of 0.3 seconds and the API Gateway requests 1000 times per second. Thus, the Lambda function has 300 concurrent executions.

Refer to the parallel execution of Lambda functions for details

**AWS Lambda deployment restrictions **

project The default limit
Lambda function deployable package size (compressed.zip/.jar file) 50 MB
The total size of all deployment packages that can be uploaded for each region 75GB
Size of code/dependencies that can be compressed into the deployment package (uncompressed.zip/.jar size). Note that each Lambda function will be in its/tmpAn additional 500 MB of non-persistent disk space was received in the directory. the/tmpDirectories can be used to load additional resources, such as dependency libraries or datasets, during function initialization. 250MB
Total size of the set of environment variables 4 KB

This article mainly refer to AWS Lambda official documentation, please visit https://docs.aws.amazon.com/zh_cn/lambda/latest/dg/welcome.html for more information

Refer to the link

Introduction to AWS Lambda development

Creating the deployment Package (Python)

Lambda functions are executed in parallel

High concurrency solution

How to optimize the site for high concurrent access?

High concurrency solution

Serverless develops programming ideas

A simple Serverless architecture example

Architectural advantages of using lambda


Finally, thank your girlfriend for her support.

Welcome to follow (April_Louisa) Buy me a fanta