gpt4 book ai didi

amazon-web-services - VPC 中的 AWS Lambda 有时无法访问互联网

转载 作者:行者123 更新时间:2023-12-04 15:56:58 36 4
gpt4 key购买 nike

我有部署到 VPC 的 Lambda。

此部署有以下配置:

  • VPC (192.168.0.0/16)
  • 公共(public)子网 A (192.168.32.0/20) 具有 NAT 网关和到互联网网关的路由 0.0.0.0/0
  • 私有(private)子网 A (192.168.48.0/20) 具有到 NAT 网关的路由 0.0.0.0/0
  • 私有(private)子网 B (192.168.64.0/20)

Lambda 拥有自己的安全组以及对“私有(private)子网 A”和“私有(private)子网 B”的引用

我遇到了奇怪的问题:Lambda 有时无法访问互联网。第三方服务正常。

还有一件奇怪的事情,Lambda 获取的是 127.0.0.1、169.254.76.13、169.254.79.1 等 IP,而不是来自子网的 IP(192.168.48.0/20 和 192.168.64.0/20)。

错误:

Error: connect ETIMEDOUT x.x.x.x:443
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)

部署架构: enter image description here

这里是完整的 CloudFormation 模板:

    ---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Base Infrastructure'
Metadata:
'AWS::CloudFormation::Interface':
ParameterGroups:
- Label:
default: 'VPC Parameters'
Parameters:
- VpcId
- InternetGatewayId
Parameters:
VpcId:
Type: String
InternetGatewayId:
Type: String
Resources:
SubnetAPublic:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone: !Select [0, !GetAZs '']
CidrBlock: !Sub '192.168.32.0/20'
MapPublicIpOnLaunch: true
VpcId: !Sub '${VpcId}'
SubnetAPrivate:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone: !Select [0, !GetAZs '']
CidrBlock: !Sub '192.168.48.0/20'
VpcId: !Sub '${VpcId}'
SubnetBPrivate:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone: !Select [1, !GetAZs '']
CidrBlock: !Sub '192.168.64.0/20'
VpcId: !Sub '${VpcId}'
RouteTablePublic:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Sub '${VpcId}'
RouteTablePrivate:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Sub '${VpcId}'
RouteTableBPrivate:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Sub '${VpcId}'
RouteTableAssociationAPublic:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref SubnetAPublic
RouteTableId: !Ref RouteTablePublic
RouteTableAssociationAPrivate:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref SubnetAPrivate
RouteTableId: !Ref RouteTablePrivate
RouteTableAssociationBPrivate:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref SubnetBPrivate
RouteTableId: !Ref RouteTableBPrivate
EIP:
Type: 'AWS::EC2::EIP'
Properties:
Domain: vpc
NatGateway:
Type: 'AWS::EC2::NatGateway'
Properties:
AllocationId: !GetAtt 'EIP.AllocationId'
SubnetId: !Ref SubnetAPublic
RouteTablePublicInternetRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref RouteTablePrivate
DestinationCidrBlock: '0.0.0.0/0'
NatGatewayId: !Ref NatGateway
RouteTablePublicInternetRoute2:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref RouteTablePublic
DestinationCidrBlock: '0.0.0.0/0'
GatewayId: !Sub '${InternetGatewayId}'
ServerlessSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SecurityGroup for Serverless Functions
VpcId: !Sub '${VpcId}'
ServerlessSecurityGroupIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref ServerlessSecurityGroup
IpProtocol: -1
SourceSecurityGroupId: !Ref ServerlessSecurityGroup

有什么想法我做错了吗?

P.S.:我发现了类似的问题AWS VPC Lambda Function keeps losing internet accessWhy AWS lambda functions In a VPC sometimes timeout and sometimes work fine?

UPD

添加了路线,现在可以使用了

  RouteTableBPrivateInternetRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref RouteTableBPrivate
DestinationCidrBlock: '0.0.0.0/0'
NatGatewayId: !Ref NatGateway

最佳答案

为了使在 VPC 内部运行的 AWS Lambda 函数能够访问 VPC 外部的资源(例如 Internet),它必须位于具有 NAT 网关的私有(private)子网中。在您的实例中,私有(private)子网 A 是唯一具有适当配置以允许 Lambda 函数访问 Internet 的子网。因此,您需要编辑 Lambda 函数的配置,使其仅在该子网中运行。

关于amazon-web-services - VPC 中的 AWS Lambda 有时无法访问互联网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51380018/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com