![](/img/trans.png)
[英]Can anyone provide a working example of an AWS SAM template that has CORS setup in a HttpApi resource?
[英]AWS SAM - Simple example of HttpApi with Lambda Function Integration
我是 AWS SAM 的新手,我正在尝试修改 Hello World 示例模板以使用打开的 api swagger 文件来定义 API 和 lambda function 集成。 当我运行“sam build”和“sam deploy --guided”时,一切都会顺利进行。 但是,当我检查 api-gateway 时,我发现路由 /helloGet 没有附加集成(应该是 lambda 函数)。 似乎无法弄清楚问题是什么。 这是我的 sam template.yaml 和 openapi swagger.yaml。
#template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Sample SAM Template
Globals:
Function:
Timeout: 3
Resources:
MyHttpApi:
Type: AWS::Serverless::HttpApi
Properties:
DefinitionUri: ./swagger.yaml
Domain:
DomainName: apitest.mydomain.com
CertificateArn: arn:aws:acm:*******:*********:certificate/*****-****-***-****-**********
EndpointConfiguration: REGIONAL
Route53:
HostedZoneId: ********************
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.9
Architectures: ['x86_64']
Events:
HelloWorldEvent1:
Type: HttpApi
Properties:
Path: /helloGet
Method: get
ApiId:
Ref: MyHttpApi
还有我的其他文件:
#swagger.yaml
openapi: "3.0.1"
info:
title: "Test SAM API"
version: "1.0.0"
paths:
/helloGet:
get:
responses:
default:
description: "Default response for GET /helloGet"
x-amazon-apigateway-integration:
payloadFormatVersion: "2.0"
type: "aws_proxy"
httpMethod: "GET"
uri:
Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloWorldFunction.Arn}/invocations"
connectionType: "INTERNET"
x-amazon-apigateway-cors:
allowMethods:
- "*"
allowHeaders:
- "*"
exposeHeaders:
- "*"
maxAge: 0
allowCredentials: false
allowOrigins:
- "https://1.0.0.127:4443"
x-amazon-apigateway-importexport-version: "1.0"
集成请求中使用的 HTTP 方法。 对于 Lambda function 次调用,该值必须为 POST。
因此,为了使其正常工作,我将 httpMethod: "GET" 更改为 httpMethod: "POST"。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.