AWS Lambda
创建访问密钥
copy Key和Secret,key见下图,secret只有在创建时可见
创建Nodejs项目
app.js
exports.handler = async (event, context) => {
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/plain',
},
body: 'Hello World\n',
};
return response;
};
package.json
{
"name": "hackernoon",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}
serverless.yml
service: my-test
provider:
name: aws
runtime: nodejs18.x
stage: dev
region: cn-northwest-1
functions:
hello:
handler: app.handler
events:
- http:
path: hello
method: get
npm install -g serverless
cd到项目文件夹下,执行以下命令
serverless config credentials --provider aws --key
执行完成后可在个人路径下见到.aws/credentials文件
执行serverless deploy
出现错误:
原因是APIGateway默认用Edge(边缘计算),但region:cn-northwest-1不支持Edge模式,所以,修改 serverless.yml,增加 endpointType: REGIONAL
service: my-test
provider:
name: aws
endpointType: REGIONAL
runtime: nodejs18.x
stage: dev
region: cn-northwest-1
functions:
hello:
handler: app.handler
events:
- http:
path: hello
method: get
再次执行serverless deploy,部署成功
查看APIGateway,多出了一条记录
访问地址:
https://01kxbvj4wl.execute-api.cn-northwest-1.amazonaws.com.cn/dev/hello
参考:
https://hackernoon.com/deploying-a-simple-serverless-nodejs-application-on-aws-lambda-functions