如何针对同一个域并排使用AWS CloudFront和API网关? [英] How can I use AWS CloudFront and API Gateway side by side for the same domain?

查看:123
本文介绍了如何针对同一个域并排使用AWS CloudFront和API网关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将网站的静态资产放在S3上,并设置CloudFront进行分发。这些基本上保留了用户对我的网站上的任何GET请求所需的内容,即到存在错误的综合路径。

I'm putting that static assets of my website on S3, and setting up CloudFront to distribute them. These essentially holds the content users would need for any GET request on my site, to existing paths that is, with a catchall for errors.

我也有一些POST请求需要处理。表单提交,发送电子邮件,通知,与数据库交互。

I also have some POST requests I need to handle. Form submissions, sending emails, notifications, interacting with the database.

我如何与CloudFront并排为同一域设置Lambda(或API网关),以便CloudFront处理GET请求,而API Gateway处理带有正文或POST请求的请求。还是我可以通过单个URL做到这一点?

How can I set up Lambda (or API Gateway) side by side with CloudFront for the same domain so that CloudFront handles GET requests, and API Gateway handles requests with a body or POST requests. Or can I do it by individual URL somehow?

推荐答案

在CloudFront中创建分发并使其与S3一起使用。

Create the distribution in CloudFront and get it working with S3.

然后添加第二个来源,指向在API网关中分配的主机名。

Then add a second origin, pointing to the hostname assigned in API Gateway.

然后在CloudFront中创建第二个缓存行为,使用API​​网关的来源,将其设置为API网关期望的适当路径模式(例如 / api / * ),并将其配置为转发所有方法(GET, POST,PUT等...默认值只有GET和HEAD,但是有一个单选按钮可以启用所有方法。您可能需要转发一些标头,因此请选择这些标头...但不要转发原始的Host标头,因为这将无法正常工作。您可能还需要转发查询字符串或cookie,并且需要在同一屏幕上启用这些查询。

Then create a second Cache Behavior in CloudFront, using the API Gateway origin, setting it for the appropriate Path Pattern (such as /api/*) that API Gateway expects, and configure it to forward all methods (GET, POST, PUT, etc... the default is only GET and HEAD but there's a radio button to enable all methods). You'll probably want to forward some headers, so select those... but don't forward the original Host header, because that won't work. You may also want to forward query string or cookies, and those need to be enabled on that same screen.

CloudFront根据路径匹配将请求发送到适当的后端。

That's pretty much it. CloudFront sends the requests to the appropriate backend, based on path matching.

这篇关于如何针对同一个域并排使用AWS CloudFront和API网关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆