如何使用 CloudFormation 模板更新现有 AWS API Gateway

Posted

技术标签:

【中文标题】如何使用 CloudFormation 模板更新现有 AWS API Gateway【英文标题】:How to update an existing AWS API Gateway using CloudFormation Template 【发布时间】:2019-07-12 01:08:58 【问题描述】:

我有以下 Swagger 定义文件,我可以通过 AWS 控制台中的“导入 API”选项将其导入现有的 AWS API Gateway。现在,我想使用 CloudFormation 模板做同样的事情。我想知道是否可以通过 CloudFormation 模板使用“PATHS”更新现有的 AWS API Gateway。我已阅读 AWS 中的文档,但找不到任何信息。 AWS::ApiGateway::RestApi 资源无法引用现有的 AWS API Gateway。现有 API Gateway 是从 AWS 控制台手动创建的(即,不是通过 CloudFormation 模板创建的)


    "openapi": "3.0.1",
    "info": 
        "title": "Common API",
        "description": "defaultDescription",
        "version": "0.3"
    ,
    "servers": [
        
            "url": "http://localhost:32780"
        
    ],
    "paths": 
        "/catalogs": 
            "get": 
                "description": "Auto generated using Swagger Inspector",
                "parameters": [
                    
                        "name": "language",
                        "in": "query",
                        "required": false,
                        "style": "form",
                        "explode": true,
                        "example": "en"
                    ,
                    
                        "name": "category",
                        "in": "query",
                        "required": false,
                        "style": "form",
                        "explode": true,
                        "example": "region"
                    ,
                    
                        "name": "subcategory",
                        "in": "query",
                        "required": false,
                        "style": "form",
                        "explode": true,
                        "example": "group"
                    
                ],
                "responses": 
                    "200": 
                        "description": "Auto generated using Swagger Inspector",
                        "content": 
                            "application/json;charset=UTF-8": 
                                "schema": 
                                    "type": "string"
                                ,
                                "examples": 
                            
                        
                    
                ,
                "servers": [
                    
                        "url": "http://localhost:32780"
                    
                ]
            ,
            "servers": [
                
                    "url": "http://localhost:32780"
                
            ]
        
    
    

【问题讨论】:

【参考方案1】:

因为您已经从控制台创建了 API 并尝试更新它。不确定 CFT 是否可以提供帮助,但您可能可以尝试一次。由于 CloudFormation 能够调整/更新部署在相同 API 名称或 API 密钥下的 API。 因此,您可能可以记下您从控制台创建的 API 的名称,并尝试通过 CloudFormation 创建/部署具有相同名称的 API。

RestAPI:
    Type: AWS::Serverless::Api
    Properties:
      Name: !Sub "your ApiName from the console"        
      StageName: !Sub "dev"
      DefinitionBody:
        "Fn::Transform":
          Name: "AWS::Include"
          Parameters:
            Location: !Sub "s3://$TemporaryBucket/openapi.yaml"

除了从 S3 调用 API,还可以在 Cloudformation 模板本身中轻松定义 API 定义/主体。

【讨论】:

我试过这个。它只是创建另一个同名的 API 网关。

以上是关于如何使用 CloudFormation 模板更新现有 AWS API Gateway的主要内容,如果未能解决你的问题,请参考以下文章

AWS CloudFormation 使用现有的安全组

如何在 CloudFormation 模板中描述 AWS Lambda 函数测试事件?

如何调试cloudformation模板?策略中的语法错误。 YAML 文件

使用带反斜杠的sed命令时出现AWS CloudFormation模板验证错误

使用CloudFormation模板更新AWS Athena工作组

cloudformation 堆栈更新和启动模板版本/自动缩放组的问题