如何通过CloudFormation创建符合预期的多路径AWS API Gateway Stage
CloudFormation代码修改方案
1. 新增/process-twilio-message路径及对应方法
在API Gateway的Resources节点下,添加该路径的资源定义和对应的POST方法(匹配Twilio webhook的常用请求方式):
Resources: # 保留原有资源配置,新增以下内容 ProcessTwilioMessageResource: Type: AWS::ApiGateway::Resource Properties: ParentId: !Ref ApiRootResource # 替换为你的API根资源的逻辑ID PathPart: process-twilio-message RestApiId: !Ref YourRestApi # 替换为你的RestApi资源的逻辑ID ProcessTwilioMessagePostMethod: Type: AWS::ApiGateway::Method Properties: AuthorizationType: NONE # 根据你的实际授权需求调整,比如使用AWS_IAM等 HttpMethod: POST ResourceId: !Ref ProcessTwilioMessageResource RestApiId: !Ref YourRestApi Integration: # 根据你的后端类型调整集成配置,以下是Lambda代理集成示例 Type: AWS_PROXY IntegrationHttpMethod: POST Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${YourTwilioLambda.Arn}/invocations
2. 修正/sendmessage路径的HTTP方法
找到现有代码中对应/sendmessage路径的AWS::ApiGateway::Method资源,将HttpMethod字段从GET改为POST,同时同步调整集成部分的IntegrationHttpMethod(确保与后端期望的请求方法一致):
# 原/sendmessage方法配置修改后示例 SendMessagePostMethod: Type: AWS::ApiGateway::Method Properties: AuthorizationType: NONE HttpMethod: POST # 替换原有的GET配置 ResourceId: !Ref SendMessageResource # 你的sendmessage路径资源逻辑ID RestApiId: !Ref YourRestApi Integration: Type: AWS_PROXY IntegrationHttpMethod: POST # 若后端为Lambda,此值固定为POST;若为HTTP后端,需匹配后端接口方法 Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${YourSendMessageLambda.Arn}/invocations
3. 更新部署依赖确保配置生效
检查AWS::ApiGateway::Deployment资源的DependsOn列表,将新增和修改的方法资源加入其中,避免部署时未加载最新的API配置:
ApiDevDeployment: Type: AWS::ApiGateway::Deployment DependsOn: - ProcessTwilioMessagePostMethod - SendMessagePostMethod # 保留其他已有的方法依赖 Properties: RestApiId: !Ref YourRestApi StageName: dev
内容的提问来源于stack exchange,提问作者Brian G
相关产品推荐
相关产品推荐

