在AWS SAM(Serverless Application Model)中,可以使用查询字符串参数来传递数据到Lambda函数。下面是一个使用AWS SAM必需的查询字符串参数的解决方法的代码示例:
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: myfunction/
Handler: app.lambda_handler
Runtime: python3.8
Events:
GetEvent:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /myfunction
Method: get
RequestParameters:
querystring:
myparam: true
上述代码示例中,创建了一个API网关(MyApi)和一个Lambda函数(MyFunction)。在Lambda函数的事件配置中,使用了RequestParameters属性来定义必需的查询字符串参数(myparam)。
在Python的Lambda函数代码中,可以通过event['queryStringParameters']来获取传递的查询字符串参数。以下是一个示例的Python代码:
import json
def lambda_handler(event, context):
myparam = event['queryStringParameters']['myparam']
return {
'statusCode': 200,
'body': json.dumps({'myparam': myparam})
}
在上述示例代码中,使用event['queryStringParameters']['myparam']来获取传递的查询字符串参数,并将其返回给调用方。
请注意,必需的查询字符串参数需要在API网关的事件配置中定义,并且在Lambda函数的代码中进行处理。
上一篇:AWS SAM 本地和环境参数
下一篇:AWS SAM 部署配置问题