【问题标题】:Access AWS Resource Outside of VPC from Within VPC - Serverless Framework从 VPC 内部访问 VPC 外部的 AWS 资源 - 无服务器框架
【发布时间】:2019-12-07 07:43:08
【问题描述】:

我正在尝试从 VPC 内部的 lambda 函数访问 VPC 外部的运动流。当前,当执行写入运动流的代码时,它将挂起然后超时。当我将 lambda 从 VPC 中取出时,写入流的代码可以正常工作。但我需要访问 VPC 中的资源,然后写入流。有人知道怎么修这个东西吗?

这是我在 VPC 中的函数

functions:
  handleChanges:
    handler: functions/handlers.handleChanges
    timeout: 10
    package:
      include:
        - functions/utils/**
    events:
      - http:
          method: POST
          path: "/"
          integration: lambda
    vpc:
      securityGroupIds:
        - ${file(./private.yml):variables.securityGroup}
      subnetIds:
        - ${file(./private.yml):variables.subnetID}

这是我的政策

iamRoleStatements:
  - Effect: "Allow"
    Action:
      - "kinesis:PutRecord"
      - "kinesis:GetRecords"
      - "kinesis:GetShardIterator"
      - "kinesis:DescribeStream"
      - "kinesis:ListStreams"
    Resource:
      Fn::GetAtt:
        - KinesisStream
        - Arn
  - Effect: "Allow"
    Action:
      - "cognito-idp:AdminGetUser"
    Resource: "*"
  - Effect: "Allow"
    Action:
      - "logs:CreateLogGroup"
      - "logs:CreateLogStream"
      - "logs:PutLogEvents"
      - "ec2:CreateNetworkInterface"
      - "ec2:DescribeNetworkInterfaces"
      - "ec2:DeleteNetworkInterface"
    Resource: "*"

最后这是我的运动流资源

KinesisStream:
  Type: AWS::Kinesis::Stream
  Properties:
    Name: ${self:provider.environment.STREAM_NAME}
    ShardCount: 1

【问题讨论】:

    标签: amazon-web-services aws-lambda serverless-framework aws-vpc


    【解决方案1】:

    唯一的解决方案是将NAT Gateway(或NAT instance)添加到您的VPC,以便您的私有子网中的Lambda函数等资源可以访问VPC之外的资源。

    【讨论】:

    • 是的,谢谢您的回答。我刚刚创建了一个 NAT 网关,现在它似乎可以工作了。我偶然发现了这个解释过程的要点:gist.github.com/reggi/dc5f2620b7b4f515e68e46255ac042a7
    • 你知道这样做有没有潜在的安全隐患吗?
    • @realseanp VPC NAT 网关仅允许内部发起的连接,而不是外部发起的连接,因此您的内部资源不会使用此设置公开。基本上没有办法使用本质上不安全的配置来设置 NAT 网关。
    【解决方案2】:

    不需要 NAT,您也可以使用 VPC 端点进行此操作: https://docs.aws.amazon.com/vpc/latest/userguide/vpc-endpoints.html 这就是如何对 Kinesis 进行操作: https://docs.aws.amazon.com/streams/latest/dev/vpc.html

    为我工作 :) 并且匹配更便宜。 确保您设置了正确的安全组(私有 VPC 的 sg 而不是默认 VPC)

    如果您将阅读 NAT 定价文档,他们也建议您这样做: https://aws.amazon.com/vpc/pricing/ 阅读最后的注释:

    Note: To avoid the NAT Gateway Data Processing charge in this example, you could setup a Gateway Type VPC endpoint and route the traffic to/from S3 through the VPC endpoint instead of going through the NAT Gateway. There is no data processing or hourly charges for using Gateway Type VPC endpoints. For details on how to use VPC endpoints, please visit VPC Endpoints Documentation.
    

    【讨论】:

      猜你喜欢
      • 2019-11-07
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 2016-10-30
      相关资源
      最近更新 更多