使用Cloudformation的AWS Api Gateway代理资源?

Chr*_*sen 8 proxy amazon-s3 aws-cloudformation aws-api-gateway

我正在尝试从API网关端点代理配置为网站的S3存储桶.我使用控制台成功配置了端点,但我无法使用Cloudformation重新创建配置.

经过大量的试验和错误猜测后,我想出了以下CF堆栈模板,让我非常接近:

Resources:
  Api:
    Type: 'AWS::ApiGateway::RestApi'
    Properties:
      Name: ApiDocs

  Resource:
    Type: 'AWS::ApiGateway::Resource'
    Properties:
      ParentId: !GetAtt Api.RootResourceId
      RestApiId: !Ref Api
      PathPart: '{proxy+}'

  RootMethod:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      HttpMethod: ANY
      ResourceId: !GetAtt Api.RootResourceId
      RestApiId: !Ref Api
      AuthorizationType: NONE
      Integration:
        IntegrationHttpMethod: ANY
        Type: HTTP_PROXY
        Uri: 'http://my-bucket.s3-website-${AWS::Region}.amazonaws.com/'
        PassthroughBehavior: WHEN_NO_MATCH
        IntegrationResponses:
          - StatusCode: 200

  ProxyMethod:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      HttpMethod: ANY
      ResourceId: !Ref Resource
      RestApiId: !Ref Api
      AuthorizationType: NONE
      RequestParameters:
        method.request.path.proxy: true
      Integration:
        CacheKeyParameters:
          - 'method.request.path.proxy'
        RequestParameters:
          integration.request.path.proxy: 'method.request.path.proxy'
        IntegrationHttpMethod: ANY
        Type: HTTP_PROXY
        Uri: 'http://my-bucket.s3-website-${AWS::Region}.amazonaws.com/{proxy}'
        PassthroughBehavior: WHEN_NO_MATCH
        IntegrationResponses:
          - StatusCode: 200

  Deployment:
    DependsOn:
      - RootMethod
      - ProxyMethod
    Type: 'AWS::ApiGateway::Deployment'
    Properties:
      RestApiId: !Ref Api
      StageName: dev
Run Code Online (Sandbox Code Playgroud)

使用此模板我可以成功获取存储桶网站的根目录,但代理资源为我提供了500:

curl -i https://abcdef.execute-api.eu-west-1.amazonaws.com/dev/index.html
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
Content-Length: 36
Connection: keep-alive
Date: Mon, 11 Dec 2017 16:36:02 GMT
x-amzn-RequestId: 6014a809-de91-11e7-95e4-dda6e24d156a
X-Cache: Error from cloudfront
Via: 1.1 8f6f9aba914cc74bcbbf3c57e10df26a.cloudfront.net (CloudFront)
X-Amz-Cf-Id: TlOCX3eemHfY0aiVk9MLCp4qFzUEn5I0QUTIPkh14o6-nh7YAfUn5Q==

{"message": "Internal server error"}
Run Code Online (Sandbox Code Playgroud)

我不知道如何调试500.

为了追踪可能出错的内容,我将aws apigateway get-resource在控制台中手动创建的资源(正在运行)的输出与一个Cloudformation(不是)进行了比较.资源看起来完全相同.get-method然而,输出略有不同,我不确定使用Cloudformation使它们完全相同是可能的.

工作方法配置:

{
  "apiKeyRequired": false,
  "httpMethod": "ANY",
  "methodIntegration": {
    "integrationResponses": {
      "200": {
        "responseTemplates": {
          "application/json": null
        },
        "statusCode": "200"
      }
    },
    "passthroughBehavior": "WHEN_NO_MATCH",
    "cacheKeyParameters": [
      "method.request.path.proxy"
    ],
    "requestParameters": {
      "integration.request.path.proxy": "method.request.path.proxy"
    },
    "uri": "http://muybucket.s3-website-eu-west-1.amazonaws.com/{proxy}",
    "httpMethod": "ANY",
    "cacheNamespace": "abcdefg",
    "type": "HTTP_PROXY"
  },
  "requestParameters": {
    "method.request.path.proxy": true
  },
  "authorizationType": "NONE"
}
Run Code Online (Sandbox Code Playgroud)

配置不起作用:

{
    "apiKeyRequired": false,
    "httpMethod": "ANY",
    "methodIntegration": {
        "integrationResponses": {
            "200": {
                "responseParameters": {},
                "responseTemplates": {},
                "statusCode": "200"
            }
        },
        "passthroughBehavior": "WHEN_NO_MATCH",
        "cacheKeyParameters": [
            "method.request.path.proxy"
        ],
        "requestParameters": {
            "integration.request.path.proxy": "method.request.path.proxy"
        },
        "uri": "http://mybucket.s3-website-eu-west-1.amazonaws.com/{proxy}",
        "httpMethod": "ANY",
        "requestTemplates": {},
        "cacheNamespace": "abcdef",
        "type": "HTTP_PROXY"
    },
    "requestParameters": {
        "method.request.path.proxy": true
    },
    "requestModels": {},
    "authorizationType": "NONE"
}
Run Code Online (Sandbox Code Playgroud)

差异:

  • 工作配置已responseTemplates设置为"application/json": null.据我所知,没有办法明确地将映射设置为null使用Cloudformation.我的CF方法只是在这里有一个空对象.
  • 我的CF方法"responseParameters": {},,而工作配置没有responseParameters在所有
  • 我的CF方法"requestModels": {},,而工作配置没有requestModels在所有

比较控制台中的两个,它们看起来完全一样.

我的智慧在这里结束:我做错了什么?这可能是使用Cloudformation实现的吗?

Chr*_*sen 4

答:以上是正确的。我通过一系列步骤得出了这个解决方案,并一遍又一遍地重新应用该模板。删除堆栈并使用此配置重新部署它达到了预期的效果。

  • 这是因为对现有 API 网关的任何“更新”都要求您重新部署 API 网关 - 即使在使用 CloudFormation 时也是如此。CloudFormation 仅会部署全新的 API。 (3认同)