Use aws lambda to develop serverless programs

Use aws lambda to develop serverless programs

Using aws lambda, you only need to write the code in its format, and then publish it to aws, you can run it, no server is required, it is completely hosted by Amazon. And it is charged by the number of runs, and it is super cheap. That way, you don't need to purchase EC2 in advance. Lambda Will handle all the work required to run and scale high-availability code. The code can be set to trigger from S3, Kinesis, or directly called from any web or mobile application.

In fact, this is the concept of serverless

Below, use examples to illustrate how to develop lambda programs

Create function

1, Log in to the management background

2. The simple way to start creation is to select a blueprint, select a template example, enter "S3" in the search bar, and select s3-get-object-python. The function of this template is that when S3 uploads a file, it triggers the lambda program

3, fill in sequentially, name, role name

4. Select the S3 bucket to be monitored

5. Finally, the creation is successful, as shown in the figure below

6, edit the code, lambda provides 3 ways to edit the code. When the code is relatively simple, you can directly use its online editor to edit. When the project is relatively large and needs to rely on a lot of third-party packages, then you must upload the compressed package the way

7, S3 trigger switch, when our program is ready, turn on this switch. Then remember to click the save button in the upper right corner

Run function

1. Upload a file named lambda-test.txt to S3

2. Go back to the lambda background, click on the monitor, and open the monitor page. You can see that the lambda function is called once, and the running time is about 200 milliseconds. Just click on a "jump to log" to go to the next page to see the specific log

3. Select the log of the last 5 minutes, you can see the output of this line of "CONTENT TYPE", which is the output of the print statement in the python code

Concluding remarks

A simple lambda program is completed in this way, of course, more and more functions need to be studied in depth, in order to play the greatest role of lambda