How to access private API Gateway over on-prem/VPN?

余生长醉 提交于 2020-06-01 04:09:09

问题


I have created an API gateway to run a Lambda function. This is meant to be used as an internal service so my API gateway is private. In order to make the API gateway accessible privately I did the following:

  1. Created a VPC endpoint in a couple of subnets of our VPC
  2. Added the endpoint to a security group that only allows traffic from on-prem
  3. Added a resource policy to the API gateway that only allows requests from the VPC endpoint

These steps effectively block public requests and allow requests from on-prem through the DNS names created by the VPC endpoint.

The problem with this approach is in order to call the API one has to specify the either the Host or x-apigw-api-id in the request. The goal is for users to be able to go on their browsers, type in the URL and query string parameters, and get a response from the service.

Amazon API Gateway types, use cases and performance talks about an approach to avoid having to specify the API id or host, but it doesn't provide much detail. The relevant portion says the solution would be:

Place an Application Load Balancer with an SSL certificate (e.g. api.mydomain.com) in front of the IP addresses of your PrivateLink network interfaces. Also deploy a custom domain name for api.mydomain.com and a base path mapping for your API Gateway. Then add a Route 53 record that points api.mydomain.com as an alias to your ALB. This solution is quite complex, but we’ve tested it and it works. Describing the full solution is outside the scope of this post, but we might write a separate blog post about it later.

Does anyone know how to do this or a different approach?


回答1:


I found out the answer to this question so I thought I should share it in case anyone else is wondering how to do this. I reached out to the person who wrote the post above and he gave me some pointers. He later wrote a post explaining the solution, if my summation of it is confusing you can read his post here:

https://cloudbanshee.com/blog/connecting-to-private-api-over-vpn-or-vpc-peering

Essentially, once you have your private API Gateway VPC endpoint and ALB, this is what you need to do:

  • Create a custom domain name using the same certificate as the ALB listener
  • Add base path mappings for the desired stages of an API gateway you want to connect to
  • Add a route53 record with the custom domain name as the name and the ALB DNS as the target (or if you have your own DNS server add the record there
  • Create a target group of type IP for the ALB and add the IPs for the VPC endpoints
  • In the ALB listener, create rules that send traffic to the target group when the url matches the custom domain name and the path matches the base path mapping defined for the API gateway

The thing that I was missing and that makes this work is the base path mapping. That is how the ALB knows which API gateway to route traffic to without knowing any IDs or Host names.

The post I shared is more detailed and much better written. I'd recommend anyone interested in learning how to access an API Gateway privately to read it.



来源:https://stackoverflow.com/questions/56246494/how-to-access-private-api-gateway-over-on-prem-vpn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!