在了解AWS的Serverless服务之前,需要知道Serverless的概念,可以参考上一篇文章:什么是Serverless
在AWS中,负责Serverless的服务叫做Lambda,下面一步步手动操作来讲解Lambda的使用方法。
1. 登陆AWS控制台
进入AWS官网后,点击右上角登陆按钮,输入用户名和密码后登陆进入Dashboard

2. 进入Lambda服务
搜索Lambda服务,并进入

3. 创建Lambda函数

4.选择从模版创建
输入函数名称,选择语言环境,点击创建即可
创建后里面的环境就自带相应的语言,这里以Node16为例

5. 编辑代码
由于是模版创建的,所以会自带一些示例代码,尝试把返回的Hello from Lambda改为Hello World

6. 测试
点击测试,即可看到返回值

7. 创建API Gateway
为了让外面可以调用这个Lambda函数,我们需要配置AWS的API Gateway,找到API Gateway服务

点击APIs,选择REST API的Build按钮

选择New API模式,输入API的名称,点击Create API

8. 创建资源和方法
在/路由下选择创建资源

在资源下创建方法

选择映射到刚刚创建好的Lambda函数

即可完成资源和方法的创建
9. 部署API Gateway
在方法上点击部署API

切换到Stage选项卡,即可看到部署好的API Gateway

可以看到右侧有个链接,在Postman调用这个链接即可通过API Gateway调用Lambda函数

10. 修改代码后
比如将返回的hello world改为hello lambda,点击部署即可马上调用到最新的接口

Postman查看到最新的接口

网友评论