要解决AWS SAM部署的Lambda函数出现CORS问题,您可以按照以下步骤进行操作:
def lambda_handler(event, context):
# 处理您的Lambda函数逻辑
response = {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'OPTIONS, POST, GET'
},
'body': json.dumps('Hello from Lambda!')
}
return response
Resources:
YourLambdaFunction:
Type: AWS::Serverless::Function
Properties:
# 其他配置项...
Events:
YourApiEvent:
Type: Api
Properties:
Path: /your-api-path
Method: your-http-method
RestApiId: your-rest-api-id
# 添加以下CORS配置
Cors:
AllowMethods: "'OPTIONS, POST, GET'"
AllowHeaders: "'Content-Type'"
AllowOrigin: "'*'"
sam deploy --template-file template.yaml --stack-name your-stack-name --capabilities CAPABILITY_IAM
确保将template.yaml替换为您的AWS SAM模板文件的路径,并将your-stack-name替换为您想要为堆栈命名的名称。
这些步骤将为您的Lambda函数配置CORS头部,允许来自任何来源的请求。请根据需要调整CORS配置,以满足您的特定要求。
上一篇:AWS SAM部署错误 - 验证错误 | 创建变更集操作错误
下一篇:AWS SAM部署:错误fork/exec /var/task/myfunction:没有这样的文件或目录:PathError