用golang + Lambda + API Gateway创建了一个REST API
首先
最近,AWS Lambda已经支持了Golang。
Golang的吸引力之一是其执行速度。我想很多人都希望可以将它用于API的开发吧?以前我们就可以利用apex,使用Golang构建Lambda(apex)+ API Gateway的REST API开发。
这次,由于lambda正式支持golang的原因,我决定不使用apex来创建一个试验性的功能,即将GET参数返回。
1. 高性能编程语言
准备环境
如果还没有Go的开发环境,请参考以下链接:
http://golang-jp.org/doc/install
请安装所需的库。
$ go get github.com/aws/aws-lambda-go/lambda
准备用lambda运行的代码。
将下面的代码保存在适当的位置。
package main
import (
"context"
"github.com/aws/aws-lambda-go/lambda"
)
func HandleRequest(ctx context.Context, params interface{}) (interface{}, error) {
// TODO your processing
return params, nil
}
func main() {
lambda.Start(HandleRequest)
}
准备一个要上传到Lambda的ZIP文件。
$ GOOS=linux GOARCH=amd64 go build -o apitest api-test.go
$ zip apitest.zip apitest
2. 亚马逊 AWS Lambda
将在第1章中创建的Go二进制文件上传到Lambda。
创建lambda函数




考试



3. API 网关
将第二章中创建的lambda函数与API Gateway连接起来。
创建资源方法
Note: This translation assumes that the original phrase “リソース・メソッドの作成” is in Japanese, not Chinese.






编辑请求




模板的内容就是这样的感觉。
#set($inputRoot = $input.path('$'))
{
"hoge" : "$input.params('hoge')"
}
考试
API的功能部分已经完成,现在需要进行测试以确保其正常运行。
在“方法执行”界面的左上角选择测试。

部署

結束
如果要创建REST API,在EC2上建立Web服务器也可以,但是使用无服务器架构确实更容易维护。