The details of the whole network use the Amazon cloud lamdba node to connect to the database and realize addition, deletion, modification and query

This article took a lot of time and effort to get to you, so please read it! ! !

1. Understanding Amazon Cloud

What is the Amazon cloud? Amazon Cloud is similar to Alibaba Cloud and Tencent Cloud. They are all server leasing platforms. Users can register accounts on this platform to buy servers to meet their daily needs. There are many services on the Amazon cloud, and some of the most used ones are: LAMBDA, API GATEWAY, RDS, EC2, and S3.
insert image description here
I won't introduce these things one by one here, you can find out what these things are by Baidu. Next, I will focus on lambda.

2. Preparations before creating lambda

Before creating the lambda function, you also need to create an RDS (database). Of course, you also need to create an Amazon cloud account before this.
(1) Create a database
insert image description here
(2) Select mysql
insert image description here
(3) Select the template as the free package. Remember to choose this
insert image description here
one. This is relatively simple, just look at its options, especially the template option. It defaults to generating templates , you want to change to a free package, remember to change it, you must change it, you must change it, the landlord has suffered a loss here, and Amazon Cloud is still quite pitiful, so I won’t tell you if you want to
consume The picture shows the key knowledge of the created database instance
insert image description here
: You can’t create tables on other webpages after you create a good instance here. You need to use tools, such as navicat for mysql and other tools, after connecting
insert image description here
insert image description here
externally , you can create a table, here I built a demo database, and then built a userInfo table, which will be used for testing at that time.
So far, all preparations are over! ! !

2. Understand and create lambda

What are lambds? You can go to Baidu, Baidu should be more detailed than me. If you are lazy to find it, you can click on this link and click me . In a word, this is no service, which is more high-end than microservices, and it is also similar to cloud development of small programs. ,
Lambad writes functions, here supports the use of node, py, java and other languages, so we use our big front-end language node here, the front-end is invincible, hahaha! ! !
If you don’t have the foundation, I suggest you read this article: click me This article is very detailed, and the code we wrote in the follow-up is also written according to his method.

Step 1: Create a function
insert image description here
insert image description here
Generally, after entering the name, click to create a function
Step 2: Write code
insert image description here
You can write any js code here, but the format must be like this, export const handler = async (event)= >{}, otherwise he will report an error.
After writing in this way, you can click the test in the figure below:
insert image description here
insert image description here
such a simple function is written, and then after you modify the function every time and save it, remember to deploy it, otherwise when testing, it will still be the previous one function
insert image description here

Step 3: Change the code
The above is just a relatively simple example. I need to connect to mysql now. At this time, you need to use the knowledge points of node. At the same time, you also need to understand how node connects to mysql, which is not the same here. Let me talk about it in detail. Here
insert image description here
I adopt the idea of ​​the above article and upload zip. The picture below is a package I created on my local computer.
insert image description here
Note that the name in package.json must be the same as the function name you created. To be consistent
After creating the package, upload the package in the form of zip
insert image description here
insert image description here
Here are some codes to connect to the database, here is a simple query statement

Step 4: Test again
Same as the above steps, this will appear after clicking, so the data will be obtained, happy! ! !
insert image description here

3. Supplementary use of layers (layers) of lamdba

This layer is like a unified resource management tool, similar to some public methods and resource storage places on the front end, so that when we create a function, we don't need to introduce resources and methods into the function again.

(1) Click on the layer on the left to start creating
insert image description here
(2) Create a layer
insert image description here
(3) Fill in the layer name and create a compressed package
insert image description here
Tip: Special attention here, this compressed package is actually some of your node_modules, and then some public methods, see My picture below:
insert image description here
In this way, your package is roughly fine, but you still need to wrap a layer of nodejs files outside. As for why, please see the picture below. My translation
insert image description here
is: you need a nodejs package, let me tell you lamdba is now in the node environment. So this package becomes like this:
insert image description here
(4) Upload compressed package
Compress this package and upload it to create

(5) Use the compressed package
Click on the layers of the function, it will automatically add a layer below
insert image description here
insert image description here
(6) Add a layer
Select a layer you just created, and then add it, so that it is associated
insert image description here

(7) Optimize the code and function structure
so that you don't need to introduce the same thing again every time you create a function, which is very convenient.
Why use opt, this is hard-coded, because the operating environment of the lamdba function is under the opt folder.

insert image description here
(8) Test
insert image description here
You can see that I have added code and msg here because I encapsulated this method uniformly.

4. Associate lamdba with api geateway, support post, get, and pass parameters

In the above steps, we only formed an internal test, and we still need to form an interface for the front-end to call later. emmm, what I said, it feels like a big front end! ! !
The association is also very simple.
(1) Create
insert image description here
insert image description here
other types of api first, and you can also learn about it. Baidu, hahaha

(2) Create a resource
insert image description here
insert image description here
(3) Create a method under the resource, you can choose different types such as post, get, any, etc.
insert image description here
The picture below is the api I created
insert image description here

5. Test interface

insert image description here
I thought I was full of confidence, but it turned out that this 403 has no authority. I found an article later, which said that Amazon cloud in China needs to be filed, and there is no such problem abroad. The link to the article created a work order: the current position
is
insert image description here
still In processing, waiting. . . .
The follow-up is as shown in the figure below, and I feel that it will not be handled by me, so I can only find
insert image description here
another way. The other way is to test in the api gateWay:
insert image description here
insert image description here
the figure below is with parameters,
insert image description here
which is how it is obtained and used in the lamdba function
insert image description here

6. Summary

, The configuration of Amazon Cloud is really cumbersome at the beginning. Many things have not been touched before. It is more complicated than the cloud development of small programs, but there is only technical support from the official website and some customer service, so it only takes a little time. It can be solved! ! !

Guess you like

Origin blog.csdn.net/weixin_43929450/article/details/131800903