使用Cloudformation的AWS Api Gateway代理资源吗? [英] AWS Api Gateway proxy resource using Cloudformation?
问题描述
我正在尝试从API网关终结点代理配置为网站的S3存储桶。我已使用控制台成功配置了端点,但是无法使用Cloudformation重新创建配置。
I'm trying to proxy an S3 bucket configured as a website from an API Gateway endpoint. I configured an endpoint successfully using the console, but I am unable to recreate the configuration using Cloudformation.
经过大量的反复试验和猜测,我想到了下面的CF堆栈模板使我非常接近:
After lots of trial and error and guessing, I've come up with the following CF stack template that gets me pretty close:
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
使用此模板,我可以成功获取存储桶网站的根目录,但是代理资源给了我500:
Using this template I can successfully get the root of the bucket website, but the proxy resource gives me a 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"}
我不知道如何调试500。
I have no idea how to debug that 500.
要跟踪可能出了什么问题,我比较了 aws apigateway的输出获取资源urce
在我在控制台(正在运行)中手动创建的资源上,并且创建了一个Cloudformation(不是)。资源看起来完全一样。但是, get-method
的输出却有所不同,我不确定是否可以使用Cloudformation使它们完全相同。
To track down what may be wrong, I've compared the output of aws apigateway get-resource
on the resource I created manually in the console (which is working) with the one Cloudformation made (which isn't). The resources look exactly alike. The output of get-method
however, is subtly different, and I'm not sure it's possible to make them exactly the same using 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"
}
无效的配置:
{
"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"
}
差异:
- 工作配置的
responseTemplates
设置为application / json:空
。据我所知,无法使用Cloudformation将映射显式设置为null
。相反,我的CF方法只是在这里有一个空对象。 - 我的CF方法具有
responseParameters:{},
配置完全没有responseParameters
- 我的CF方法具有
requestModels:{},
,而工作配置根本没有requestModels
- The working configuration has
responseTemplates
set to"application/json": null
. As far as I can tell, there's no way to set a mapping explicitly tonull
using Cloudformation. My CF method instead just has an empty object here. - My CF method has
"responseParameters": {},
, while the working configuration does not haveresponseParameters
at all - My CF method has
"requestModels": {},
, while the working configuration does not haverequestModels
at all
在控制台中比较两者,它们似乎完全一样。
Comparing the two in the console, they are seemingly exactly the same.
我的智慧到此为止:我在做什么错?
I'm at my wits end here: what am I doing wrong? Is this possible to achieve using Cloudformation?
推荐答案
答案:以上是正确的。我通过一系列步骤得出了该解决方案,然后一遍又一遍地重新应用了模板。使用该配置删除堆栈并重新部署它具有预期的效果。
Answer: The above is correct. I had arrived at this solution through a series of steps, and re-applied the template over and over. Deleting the stack and deploying it anew with this configuration had the desired effect.
这篇关于使用Cloudformation的AWS Api Gateway代理资源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!