私有网络中的Lambda函数不能访问Internet [英] Lambda Function in VPC has no Internet Access
本文介绍了私有网络中的Lambda函数不能访问Internet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的lambda函数可以访问私有网络中的其他资源,但是一旦它尝试向Internet发出请求,它就会超时。我有以下内容:
- 两个私有子网
- 将0.0.0.0/0路由到Internet网关的路由表。
- 几条ACL规则和一个Lambda安全组
任何帮助都会很有帮助。以下是我的CF模板:
AWSTemplateFormatVersion: 2010-09-09
Description: VPC Stack
Resources:
VPC:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsHostnames: true
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
Subnet1:
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.3.0/24
AvailabilityZone: 'us-east-1b'
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
Subnet2:
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.4.0/24
AvailabilityZone: 'us-east-1e'
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
InternetGateway:
Type: 'AWS::EC2::InternetGateway'
Properties:
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
AttachGateway:
Type: 'AWS::EC2::VPCGatewayAttachment'
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
RouteTable:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref VPC
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
Route:
Type: 'AWS::EC2::Route'
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref RouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
SubnetRouteTableAssociation1:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref Subnet1
RouteTableId: !Ref RouteTable
SubnetRouteTableAssociation2:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref Subnet2
RouteTableId: !Ref RouteTable
NetworkAcl:
Type: 'AWS::EC2::NetworkAcl'
Properties:
VpcId: !Ref VPC
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
InboundHTTPNetworkAclEntry:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAcl
RuleNumber: '100'
Protocol: '6'
RuleAction: allowAWSTemplateFormatVersion: 2010-09-09
Description: VPC Stack
Resources:
VPC:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsHostnames: true
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
Subnet1:
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.3.0/24
AvailabilityZone: 'us-east-1b'
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
Subnet2:
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.4.0/24
AvailabilityZone: 'us-east-1e'
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
InternetGateway:
Type: 'AWS::EC2::InternetGateway'
Properties:
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
AttachGateway:
Type: 'AWS::EC2::VPCGatewayAttachment'
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
RouteTable:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref VPC
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
Route:
Type: 'AWS::EC2::Route'
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref RouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
SubnetRouteTableAssociation1:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref Subnet1
RouteTableId: !Ref RouteTable
SubnetRouteTableAssociation2:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref Subnet2
RouteTableId: !Ref RouteTable
NetworkAcl:
Type: 'AWS::EC2::NetworkAcl'
Properties:
VpcId: !Ref VPC
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
InboundHTTPNetworkAclEntry:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAcl
RuleNumber: '100'
Protocol: '6'
RuleAction: allow
Egress: 'false'
CidrBlock: 0.0.0.0/0
PortRange:
From: '80'
To: '80'
InboundSSHNetworkAclEntry:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAcl
RuleNumber: '101'
Protocol: '6'
RuleAction: allow
Egress: 'false'
CidrBlock: 0.0.0.0/0
PortRange:
From: '22'
To: '22'
InboundResponsePortsNetworkAclEntry:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAcl
RuleNumber: '102'
Protocol: '6'
RuleAction: allow
Egress: 'false'
CidrBlock: 0.0.0.0/0
PortRange:
From: '1024'
To: '65535'
OutBoundNetworkAclEntry:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAcl
RuleNumber: '103'
Protocol: '6'
RuleAction: allow
Egress: 'true'
CidrBlock: 0.0.0.0/0
PortRange:
From: '0'
To: '65535'
SubnetNetworkAclAssociation1:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref Subnet1
NetworkAclId: !Ref NetworkAcl
SubnetNetworkAclAssociation2:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref Subnet2
NetworkAclId: !Ref NetworkAcl
LambdaSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPC
GroupDescription: Access to Lambda functions
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
IpProtocol: -1
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
IpProtocol: -1
Outputs:
VpcId:
Description: VPC ID
Value: !Ref VPC
Export:
Name: !Sub "Portal-VpcId"
Subnet1:
Description: Subnet ID 1
Value: !Ref Subnet1
Export:
Name: !Sub "Portal-SubnetID1"
Subnet2:
Description: Subnet ID 2
Value: !Ref Subnet2
Export:
Name: !Sub "Portal-SubnetID2"
LambdaSecurityGroup:
Description: Access to Lambda functions
Value: !Ref LambdaSecurityGroup
Export:
Name: !Sub "LambdaSecurityGroup"
Egress: 'false'
CidrBlock: 0.0.0.0/0
PortRange:
From: '80'
To: '80'
InboundResponsePortsNetworkAclEntry:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAcl
RuleNumber: '102'
Protocol: '6'
RuleAction: allow
Egress: 'false'
CidrBlock: 0.0.0.0/0
PortRange:
From: '1024'
To: '65535'
OutBoundNetworkAclEntry:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAcl
RuleNumber: '103'
Protocol: '6'
RuleAction: allow
Egress: 'true'
CidrBlock: 0.0.0.0/0
PortRange:
From: '0'
To: '65535'
SubnetNetworkAclAssociation1:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref Subnet1
NetworkAclId: !Ref NetworkAcl
SubnetNetworkAclAssociation2:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref Subnet2
NetworkAclId: !Ref NetworkAcl
LambdaSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPC
GroupDescription: Access to Lambda functions
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
IpProtocol: -1
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
IpProtocol: -1
Outputs:
VpcId:
Description: VPC ID
Value: !Ref VPC
Export:
Name: !Sub "Portal-VpcId"
Subnet1:
Description: Subnet ID 1
Value: !Ref Subnet1
Export:
Name: !Sub "Portal-SubnetID1"
Subnet2:
Description: Subnet ID 2
Value: !Ref Subnet2
Export:
Name: !Sub "Portal-SubnetID2"
LambdaSecurityGroup:
Description: Access to Lambda functions
Value: !Ref LambdaSecurityGroup
Export:
Name: !Sub "LambdaSecurityGroup"
CF模板2:
Lambda:
Type: "AWS::Lambda::Function"
Properties:
Handler: "index.handler"
Environment:
Variables:
Environment: !Ref Environment
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: "nodejs6.10"
VpcConfig:
SecurityGroupIds:
- !ImportValue "LambdaSecurityGroup"
SubnetIds:
- !ImportValue "Portal-SubnetID1"
- !ImportValue "Portal-SubnetID2"
Code:
S3Bucket: !Ref BaseS3Bucket
S3Key:
!Join
- ''
- - !Ref TemplatePath
- '/'
- !Ref Environment
- '/Quick-Links/build-output.zip'
推荐答案
在私有网络内执行lambda函数时,lambda函数只会收到内网IP地址。它将不会收到公用IP地址。
任何AWS服务器要通过互联网网关访问公共互联网,必须:
- 有公有IP地址,
- 位于公有子网(即可直接访问Internet网关的子网)
由于您的Lambda函数没有该公用IP地址,因此即使它位于公用子网中,也无法访问Internet。
要解决此问题,您必须在专用子网中执行Lambda函数。这是一个子网,该子网:- 没有直接访问Internet网关的权限,
- 通过NAT实例或NAT网关定向所有出站
这篇关于私有网络中的Lambda函数不能访问Internet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文