簡體   English   中英

如何將 API 網關與內部 ALB 集成

[英]How to integrate API Gateway with internal ALB

According to https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-private.html it is possible to integrate API Gateway with an internal Application Load Balancer using a private VPC link.

但是我不能讓它工作。

我有一項可以通過 ALB 在內部訪問的服務。 ALB 沒有公共 IP,它平衡 AWS Fargate 集群中的請求(都在私有子網內)。

ubuntu@ip-10-0-40-89:~$ curl http://internal-sasw-alb-355535611.eu-west-1.elb.amazonaws.com/health
Assembly=Sasw.SampleService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, Environment=Production, CurrentTime=05/05/2021 08:36:00

所以內部一切都很好,我的服務可用於GET http://alb_url/health

我想訪問相同的服務,只是這次通過 API 網關。 所以我做了以下事情:

  1. 我創建了一個與 VPC 相關聯的10.0.50.0/24 API 類型的 VPC 鏈接foo ,特別是與 EC2 實例(Fargate 集群)為10.0.40.0/24的 3 個私有子網相關10.0.60.0/24
  2. 我創建了一個 HTTP API foo類型的 API 網關。
  3. I add an integration HTTP for method ANY and URL endpoint http://internal-rubiko-alb-355535611.eu-west-1.elb.amazonaws.com (I have also tried with http://internal-rubiko-alb-355535611.eu-west-1.elb.amazonaws.com/{proxy}http://internal-rubiko-alb-355535611.eu-west-1.elb.amazonaws.com/{proxy+}沒有成功)
  4. 我按照建議的方法 ANY 離開路線,資源路徑/%7Bproxy%7D和集成目標ANY http://internal-rubiko-alb-355535611.eu-west-1.elb.amazonaws.com/{proxy}
  5. 我留下舞台名稱$default和 Auto-deploy
  6. 創造。 它給了我調用 url https://7ki6gvrngl.execute-api.eu-west-1.amazonaws.com

A GET on https://7ki6gvrngl.execute-api.eu-west-1.amazonaws.com gets me a 404. A GET on https://7ki6gvrngl.execute-api.eu-west-1.amazonaws.com/health讓我在https://7ki6gvrngl.execute-api.eu-west-1.amazonaws.com/$default/health上給我一個 404 A GET

我究竟做錯了什么? 是路由問題嗎? 我的安全組接受來自任何地方的任何協議。 我的NACL也是如此。 我的公共子網路由表是

Destination   Target                Status   Propagated
10.0.0.0/16   local                 active   No 
0.0.0.0/0     igw-08d3973e93dd580cc active   No

對於私人的

Destination  Target                 Status   Propagated
10.0.0.0/16  local                  active   No 
0.0.0.0/0    nat-040aa996d0fb1a623  active   No

謝謝你的幫助。


更新 1 (2021-05-05)我一直在 VPC 上設置流日志,所有涉及的網絡接口都有大量跟蹤,但我在那里找不到任何有意義的東西。 它們看起來像這樣:

2 754027052283 eni-06b3871889a039d54 10.0.50.60 10.0.30.69 47472 80 6 5 399 1620233998 1620233999 ACCEPT OK
2 754027052283 eni-06b3871889a039d54 10.0.30.69 10.0.50.60 80 47472 6 5 582 1620233998 1620233999 ACCEPT OK
2 754027052283 eni-06b3871889a039d54 172.245.79.11 10.0.30.69 25610 23 6 1 40 1620233998 1620233999 ACCEPT OK
2 754027052283 eni-06b3871889a039d54 10.0.30.69 172.245.79.11 23 25610 6 1 40 1620233998 1620233999 ACCEPT OK
2 754027052283 eni-0ec2ff9b148264fc1 - - - - - - - 1620233999 1620234010 - NODATA
2 754027052283 eni-0c2d904cc281e7772 185.153.199.146 10.0.10.108 58226 10089 6 1 40 1620234001 1620234061 REJECT OK
2 754027052283 eni-0c2d904cc281e7772 92.63.197.95 10.0.10.108 55424 8820 6 1 40 1620234001 1620234061 REJECT OK
2 754027052283 eni-0c2d904cc281e7772 147.203.255.20 10.0.10.108 59414 161 17 1 71 1620234001 1620234061 REJECT OK
2 754027052283 eni-0c2d904cc281e7772 107.174.25.146 10.0.10.108 1406 23 6 1 40 1620234001 1620234061 REJECT OK
2 754027052283 eni-0c2d904cc281e7772 45.146.164.93 10.0.10.108 52329 3835 6 1 40 1620234001 1620234061 REJECT OK
2 754027052283 eni-0c2d904cc281e7772 192.241.159.103 10.0.10.108 38868 8088 6 1 40 1620234001 1620234061 REJECT OK
2 754027052283 eni-0c2d904cc281e7772 162.142.125.145 10.0.10.108 43260 49501 6 1 44 1620234001 1620234061 REJECT OK
2 754027052283 eni-0c2d904cc281e7772 104.206.128.38 10.0.10.108 57766 5985 6 1 44 1620234001 1620234061 REJECT OK
2 754027052283 eni-0c2d904cc281e7772 198.98.54.148 10.0.10.108 54300 8443 6 1 44 1620234001 1620234061 REJECT OK
2 754027052283 eni-0c2d904cc281e7772 162.142.125.146 10.0.10.108 33318 15006 6 1 44 1620234001 1620234061 REJECT OK
2 754027052283 eni-02884e94ae95359e6 10.0.60.101 10.0.30.69 37440 80 6 5 399 1620234003 1620234005 ACCEPT OK
2 754027052283 eni-02884e94ae95359e6 10.0.30.69 10.0.60.101 80 37440 6 5 582 1620234003 1620234005 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.50.218 10.0.40.89 57566 1113 6 66 4862 1620234008 1620234127 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.40.89 10.0.60.197 1113 38912 6 122 7664 1620234008 1620234127 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.40.89 10.0.50.218 1113 57566 6 131 8230 1620234008 1620234127 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.60.197 10.0.40.89 38912 1113 6 62 4544 1620234008 1620234127 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.40.89 10.0.50.218 52812 2113 6 271 70540 1620234008 1620234307 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.40.89 10.0.60.197 2113 34170 6 161 41607 1620234008 1620234307 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.60.197 10.0.40.89 2113 42962 6 158 44927 1620234008 1620234307 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.40.89 10.0.50.218 2113 49902 6 165 43038 1620234008 1620234307 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.50.218 10.0.40.89 2113 52812 6 163 43127 1620234008 1620234307 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.60.197 10.0.40.89 34170 2113 6 263 68432 1620234008 1620234307 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.40.89 10.0.60.197 42962 2113 6 281 73877 1620234008 1620234307 ACCEPT OK
2 754027052283 eni-05b8087c636058aad 10.0.50.218 10.0.40.89 49902 2113 6 270 70422 1620234008 1620234307 ACCEPT OK
2 754027052283 eni-06b3871889a039d54 10.0.30.69 10.0.50.60 80 47482 6 5 582 1620234010 1620234011 ACCEPT OK
2 754027052283 eni-06b3871889a039d54 10.0.50.60 10.0.30.69 47482 80 6 5 399 1620234010 1620234011 ACCEPT OK
2 754027052283 eni-0ae20fdd844224248 - - - - - - - 1620234012 1620234033 - NODATA
2 754027052283 eni-0ae20fdd844224248 - - - - - - - 1620234013 1620234026 - NODATA
2 754027052283 eni-030c3f3c47db36cb1 216.218.206.88 10.0.10.175 39712 30005 6 1 40 1620234014 1620234015 ACCEPT OK
2 754027052283 eni-030c3f3c47db36cb1 193.107.216.163 10.0.10.175 5065 5060 17 1 451 1620234015 1620234016 ACCEPT OK
2 754027052283 eni-0795bf1958db33aa7 - - - - - - - 1620234015 1620234032 - NODATA
2 754027052283 eni-0ae20fdd844224248 - - - - - - - 1620234016 1620234033 - NODATA
2 754027052283 eni-0ec2ff9b148264fc1 - - - - - - - 1620234017 1620234031 - NODATA

我什至不知道如何解釋這些。

我嘗試使用 cloudformation 生成私有鏈接、API 網關和私有 ALB。 也沒有運氣。 通過 Api 網關訪問服務 url 時一直找不到

AWSTemplateFormatVersion: 2010-09-09
Description: API Gateway
Parameters:
  prefix:
    Type: String
    Description: The prefix namespace or company name
    Default: sasw
Resources:
  apiGateway:
    Type: AWS::ApiGatewayV2::Api
    Properties:
      Name: !Sub ${prefix}-api-gateway
      Description: Api Gateway for http
      ProtocolType: HTTP
  apiRoute:
    Type: AWS::ApiGatewayV2::Route
    Properties:
      ApiId: !Ref apiGateway
      RouteKey: ANY /
      Target: !Join
        - /
        - - integrations
          - !Ref apiAlbIntegration
  privateLink:
    Type: AWS::ApiGatewayV2::VpcLink
    Properties:
      Name: !Sub ${prefix}-private-link
      SecurityGroupIds:
        - Fn::ImportValue: !Sub ${prefix}-web-sg-id
      SubnetIds:
        - Fn::ImportValue: !Sub ${prefix}-private-a-id
        - Fn::ImportValue: !Sub ${prefix}-private-b-id
        - Fn::ImportValue: !Sub ${prefix}-private-c-id
  apiAlbIntegration:
    Type: AWS::ApiGatewayV2::Integration
    Properties:
      ApiId: !Ref apiGateway
      Description: Private ALB integration
      IntegrationType: HTTP_PROXY
      IntegrationMethod: ANY
      ConnectionType: VPC_LINK
      ConnectionId: !Ref privateLink
      IntegrationUri: 
        Fn::ImportValue: !Sub ${prefix}-alb-http-listener-id
      PayloadFormatVersion: 1.0
  apiStage:
    Type: AWS::ApiGatewayV2::Stage
    Properties:
      StageName: $default
      AutoDeploy: true
      ApiId: !Ref apiGateway
Outputs:
  apiUrl:
    Description: Invoke URL
    Value: !Sub https://${apiGateway}.execute-api.${AWS::Region}.amazonaws.com/

沒有想法了。


更新 2 (2021-05-06)我在這篇文章中找到了黃金https://medium.com/swlh/aws-api-gateway-private-integration-with-http-api-and-a-vpc-link-602360a1cd84

如果我創建一個與我的私有 ALB 私有集成的路由/health ,我可以制作一個示例工作。 一旦我找到一種方法來獲得將任何 url/動詞轉發到 ALB 的通配符路由,我將回答我自己的問題。

我讓它工作了。 絕對可以使用 API 網關 http 與私有(即:面向內部的)ALB 集成,以平衡私有子網中的流量

The problem I had is that when I created the API in API Gateway through the console, there is an option to add integration, but that integration at that point only allows HTTP or Lambda, and I don't want that, I want a private使用我預先創建的 VPC 鏈接進行集成。

所以這里是步驟:

  1. 創建(如果不存在)一個安全組,允許 HTTP 流量在 80 上。該組稍后將關聯到 VPC 鏈接
  2. 創建與 VPC 關聯的 VPC 鏈接,並且明確地與 EC2services 或 Fargate 集群所在的私有子網關聯。 確保您的 select 是允許 HTTP 流量的安全組
  3. 在Apu網關中創建HTTP API。 在第一步給它一個名字,但不要創建一個集成。 跳過那個。 也跳過路線創建。 選擇一個階段名稱或保留$default (我使用$default並自動部署)。
  4. 創建路線。 如果您想接受任何內容,請選擇ANY和路徑/{proxy+}
  5. 最后,在這條路線上,附加一個集成。 這次您將看到有一個選項可以選擇私有資源,您可以在其中顯式 select 私有 ALB 及其 http 偵聽器和之前創建的 VPC 鏈接。

而已。 Http 對 Api 網關的請求將被定向到面向內部的專用 ALB。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM