简体   繁体   English

如何在 AWS S3 存储桶中使用 Angular 应用程序对 AWS Elastic Beanstalk 中的服务进行 API 调用

[英]How to have Angular app in AWS S3 bucket make API calls to service in AWS Elastic Beanstalk

I transferred a web site to AWS that has Angular on one server, Nodejs/Express on another server and using a PostgreSQL DB in RDB.我将一个网站转移到 AWS,该网站在一台服务器上有 Angular,在另一台服务器上有 Nodejs/Express,并在 RDB 中使用了 PostgreSQL 数据库。 This works great and is currently running but not great for scalability and availability.这很好用,目前正在运行,但在可扩展性和可用性方面不是很好。

So I have moved the Angular to be served from an S3 bucket accessed through CloudFront and updated with a WebHook into my GitHub repo.因此,我已将 Angular 从通过 CloudFront 访问的 S3 存储桶中提供服务,并使用 WebHook 更新到我的 GitHub 存储库中。 This all seems to work great.这一切似乎都很好。

I have the Nodejs/Express using Elastic Beanstalk and once again with using a WebHook it updates from the GitHub repo.我有使用 Elastic Beanstalk 的 Nodejs/Express,并再次使用 WebHook 从 GitHub 存储库更新。 Still have a couple items to work out on the deploy with this but I think they will be minor (which means they will bite me later).在部署时仍有一些项目需要解决,但我认为它们会很小(这意味着它们稍后会咬我)。

What I cannot seem to figure out is a way to allow the Angular to do API calls to the load balancer in Elastic Beanstalk.我似乎无法弄清楚是一种允许 Angular 对 Elastic Beanstalk 中的负载均衡器进行 API 调用的方法。

  • Do I still use proxy.config.json?我仍然使用 proxy.config.json 吗? If so what do I point it to?如果是这样,我指的是什么? I tried to create a CNAME record in Route 53 for api.myapp.com pointing to the value of the load balancer DNS.我尝试在 Route 53 中为 api.myapp.com 创建一个 CNAME 记录,指向负载均衡器 DNS 的值。 That didn't seem to work.那似乎不起作用。

  • Do I use AWS API Gateway.我是否使用 AWS API 网关。 I tried some different things in here by didn't seem to be able to get it to work.我在这里尝试了一些不同的东西,但似乎无法让它发挥作用。 The only thing that seemed a possibility would require me to define each of the APIs which if it is even possible would be my last resort.唯一似乎有可能的事情需要我定义每个 API,如果可能的话,这将是我最后的手段。

  • Is this something that cannot be done?这是不能做的事吗?

I have read a lot of articles of somewhat similar questions but none seemed to fit close enough to give me an answer.我已经阅读了很多有些类似问题的文章,但似乎没有一篇文章足够贴切地给我一个答案。 Maybe I am just looking at the incorrectly.也许我只是看错了。 Any help would be much appreciated.任何帮助将非常感激。


Edited已编辑

After chasing many squirrels the past couple days I think I have it narrowed down to configuration issue with CloudFront.在过去几天追逐许多松鼠之后,我想我已经将范围缩小到 CloudFront 的配置问题。

  • I have a route in Route 53 that points to my CloudFront as "dev.mydomain.com" and that works great.我在 Route 53 中有一条路由指向我的 CloudFront 为“dev.mydomain.com”,并且效果很好。
  • I have a route in Route 53 that points to my Elastic Beanstalk Load Balancer as "api.mydomain.com".我在 Route 53 中有一条路由指向我的 Elastic Beanstalk 负载均衡器,名称为“api.mydomain.com”。
  • I can make a call by entering the url into a browser such as " https://api.mydomain.com/api/getInfo " and it returns the information along with the cors headers.我可以通过将 url 输入到浏览器(例如“ https://api.mydomain.com/api/getInfo ”)来进行调用,它会随 cors 标头一起返回信息。 When I have the app make the same call that is being served from an S3 bucket by CloudFront I get "Access to XMLHttpRequest at ' https://api.mydomain.com/api/getInfo ' from origin ' https://dev.mydomain.com ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."当我让应用程序进行与 CloudFront 从 S3 存储桶提供服务的相同调用时,我得到“访问 XMLHttpRequest at ' https://api.mydomain.com/api/getInfo ' from origin ' https://dev。 mydomain.com ' 已被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。” and sure enough there is none of the cors headers in the response header.果然,响应头中没有 cors 头。

I have found a couple articles that seem to deal with this but they have not worked (or I am implementing them incorrectly).我发现了几篇文章似乎解决了这个问题,但它们没有奏效(或者我错误地实现了它们)。 I have tried to create an origin, I have tried to create a behavior.我试图创造一个起源,我试图创造一种行为。 Nothing has worked so far.到目前为止,一切都没有奏效。

I anyone has dealt with this or knows of an article that would help I would appreciate it greatly.我任何人都处理过这个问题,或者知道一篇可以帮助我的文章,我将不胜感激。

You can create a custom domain name for your load balancer url from Route53.您可以从 Route53 为负载均衡器 url 创建自定义域名。 You need to create an A record with Alias property set to yes pointing to the load balancer's dns URL.您需要创建一个A记录,将Alias属性设置为yes指向负载均衡器的 dns URL。

Amazon Route 53 also offers alias records, which are an Amazon Route 53-specific extension to DNS. Amazon Route 53 还提供别名记录,它们是 Amazon Route 53 特定的 DNS 扩展。 You can create alias records to route traffic to selected AWS resources, including Amazon Elastic Load Balancing load balancers, Amazon CloudFront distributions, AWS Elastic Beanstalk environments, API Gateways, VPC interface endpoints, and Amazon S3 buckets that are configured as websites您可以创建别名记录以将流量路由到选定的 AWS 资源,包括 Amazon Elastic Load Balancing 负载均衡器、Amazon CloudFront 分配、AWS Elastic Beanstalk 环境、API 网关、VPC 接口终端节点和配置为网站的 Amazon S3 存储桶

Hope this helps.希望这可以帮助。

Reference:参考:

Question "Which DNS record types does Amazon Route 53 support?"问题“Amazon Route 53 支持哪些 DNS 记录类型?” on https://aws.amazon.com/route53/faqs/https://aws.amazon.com/route53/faqs/

Solved解决了

The answer to this question got me close to where I wanted to be but it still was not responding to all my calls.这个问题的答案让我接近了我想去的地方,但它仍然没有回应我的所有电话。 Finally figured out that it was the database calls that were not responding.最后发现是数据库调用没有响应。 I am not setting the database up with the elastic beanstalk since I am doing a proof of concept for a production setup so the database needs to be separate.我没有使用弹性 beanstalk 设置数据库,因为我正在为生产设置进行概念验证,因此数据库需要分开。 Finally figured out that I had not added a rule in the security group on the database to allow the elastic beanstalk calls.终于想通了,我没有在数据库的安全组中添加规则来允许弹性beanstalk调用。 After adding that rule and adding the behavior and origin rules in CloudFront it seems to work like a charm.添加该规则并在 CloudFront 中添加行为和源规则后,它似乎很有魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Elastic Beanstalk NodeJS项目访问S3存储桶 - AWS Elastic Beanstalk NodeJS Project access S3 Bucket 如何从 aws-sdk 中的 s3 存储桶创建 Elastic Beanstalk 实例? - How to create Elastic Beanstalk instance from s3 bucket in the aws-sdk? 通过ACM和负载均衡器为AWS Nodejs弹性beantalk设置HTTPS,如何在s3存储桶中为angular设置HTTPS - HTTPS is set up for aws Nodejs elastic beanstalk throught ACM and load balancer, how do i set up the HTTPS for angular in s3 bucket 将 Angular 应用程序部署到 AWS Elastic beanstalk - Deploying angular app to AWS Elastic beanstalk 使用代理在 AWS S3 存储桶中托管 Angular 6 应用程序 - Hosting a Angular 6 app in AWS S3 bucket with proxy AWS S3 行为 - 本地主机与弹性 Beanstalk? - AWS S3 behaviour - Localhost v/s Elastic Beanstalk? AWS Elastic Beanstalk 中是否有 API 调用来获取正在运行的主机列表? (有 IP) - Is there an API call in AWS Elastic Beanstalk to have a list of running hosts? (with IPs) AWS S3:如何检索存储桶活动? - AWS S3: How to retrieve bucket activities? 如何将node.js应用程序部署到AWS Elastic Beanstalk中 - How to deploy a node.js app into AWS Elastic Beanstalk Angular Universal - 部署到 AWS Elastic Beanstalk - Angular Universal - Deploying to AWS Elastic Beanstalk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM