要在本地启动AWS SAM API而不使用授权程序,您可以使用AWS CLI的临时凭证。以下是一个解决方法,包括代码示例:
$ pip install awscli
$ aws configure
sam-template.yaml
的文件,并将以下内容添加到文件中:AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambdaHandler
Runtime: nodejs14.x
CodeUri: ./src
Events:
HelloWorldApi:
Type: Api
Properties:
Path: /hello
Method: GET
app.js
的文件,并将以下内容添加到文件中:exports.lambdaHandler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Hello, SAM!'
})
};
};
start-api.sh
的shell脚本文件,并将以下内容添加到文件中:#!/bin/bash
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export AWS_DEFAULT_REGION=
sam local start-api
确保替换
,
和
为您的实际凭证和区域。
$ chmod +x start-api.sh
$ ./start-api.sh
现在,您的AWS SAM API将在本地启动,并且您可以使用临时凭证进行访问。在浏览器中访问http://localhost:3000/hello
,您应该能够看到返回的消息"Hello, SAM!"。
请注意,这种方法仅适用于本地开发和测试。在生产环境中,建议使用AWS授权程序来提供有效的凭证。