官术网_书友最值得收藏!

Wiring handler.py to Lambda via API Gateway

Next, we need to wire up our API endpoints to Lambda and our handler.py entry point. This wiring looks like this in a serverless.yml configuration file:

functions:
HandleSession:
handler: handler.session
events:
- http:
path: session
method: get
cors: true
- http:
path: session
method: post
cors: true
HandleSessionDetail:
handler: handler.session_detail
events:
- http:
path: session/{id}
method: get
cors: true
request:
parameters:
paths:
id: true
- http:
path: session/{id}
method: delete
cors: true
request:
parameters:
paths:
id: true

We define two Lambda functions that have different configuration options, HandleSession and HandleSessionDetail.

Under each function's name, there are multiple statements that control configuration. Look at both sections and you'll notice the handler: statement, which instructs Lambda what code to call when the Lambda function is executed. For both, we'll be running one of the Python functions in handler.py that we covered in the preceding code snippet.

But what calls these Lambda functions in the first place? The events: section is responsible for setting up invocation points and making the connection between a particular event and our Lambda function. Across the FaaS landscape, functions are invoked in response to an event. In the AWS landscape, the number of events that can trigger a Lambda function is quite large. In this scenario, we are configuring events to be HTTP endpoints with a particular path and HTTP method. API Gateway is the proxy that will provide us with unique HTTPS URLs, which get wired up to our Lambda functions according to our configuration. As you read through the configuration, our design and intent should be apparent. Again, there are a seemingly infinite number of ways to set up an API with these technologies and this example just scratches the surface to discuss the overall pattern.

Because the frontend JavaScript code will be making HTTP requests to the serverless backend, which is hosted on a different domain, CORS will need to be set up for each API endpoint. Controlling CORS is simple to do by adding cors: true for each endpoint in serverless.yml. In addition to this setting, the application code will explicitly need to return the proper headers in the responses.
主站蜘蛛池模板: 洛扎县| 栾城县| 阿鲁科尔沁旗| 永昌县| 开封市| 东港市| 融水| 温州市| 邮箱| 桂阳县| 尼勒克县| 镇平县| 乡城县| 尉氏县| 镇坪县| 桑植县| 霍山县| 巩留县| 仁怀市| 于田县| 黔南| 利川市| 精河县| 东乌| 新乐市| 新疆| 临武县| 平谷区| 宝应县| 满洲里市| 新津县| 蓬莱市| 天柱县| 抚顺市| 太湖县| 莎车县| 馆陶县| 东辽县| 安岳县| 永安市| 泗洪县|