该问题通常是由于Lambda函数位于具有私有子网和公有子网的VPC中引起的。如果您希望Lambda函数能够从同一AWS账户中的EC2访问,则必须将Lambda函数放置在具有公有子网和NAT网关的VPC中。
以下是一个示例CloudFormation模板,用于创建具有公有子网和NAT网关的VPC:
---
AWSTemplateFormatVersion: '2010-09-09'
Resources:
VPC:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: '10.0.0.0/16'
EnableDnsSupport: true
EnableDnsHostnames: true
InstanceTenancy: default
Tags:
- Key: 'Name'
Value: 'MyVPC'
PublicSubnet1:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone: !Select [ 0, !GetAZs '' ]
VpcId: !Ref VPC
CidrBlock: '10.0.1.0/24'
Tags:
- Key: 'Name'
Value: 'PublicSubnet1'
PublicSubnet2:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone: !Select [ 1, !GetAZs '' ]
VpcId: !Ref VPC
CidrBlock: '10.0.2.0/24'
Tags:
- Key: 'Name'
Value: 'PublicSubnet2'
InternetGateway:
Type: 'AWS::EC2::InternetGateway'
VPCGatewayAttachment:
Type: 'AWS::EC2::VPCGatewayAttachment'
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
PublicRouteTable:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref VPC
Tags:
- Key: 'Name'
Value: 'PublicRouteTable'
PublicRoute:
Type: 'AWS::EC2::Route'
DependsOn: VPCGatewayAttachment
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: '0.0.0.0/0'
GatewayId: !Ref InternetGateway
PublicSubnet1RouteTableAssociation:
Type: 'AWS::EC2::