【发布时间】:2020-08-30 06:40:32
【问题描述】:
VPC:
PublicSubnet:
EC2-Instance
IGW
NAT
Route to IGW
PrivateSubnet:
EC2-Instance
Route to NAT
Route to S3VPCEndpoint
S3VPCEndpoint
这是我的 CFn 模板的网络部分:
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
EnableDnsSupport: true
EnableDnsHostnames: true
InstanceTenancy: default
CidrBlock: 10.1.0.0/16
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
CidrBlock: 10.1.0.0/24
MapPublicIpOnLaunch: True
VpcId: !Ref VPC
Tags:
- Key: "Name"
Value: "Raffael Public Subnet"
PrivateSubnet:
Type: AWS::EC2::Subnet
Properties:
CidrBlock: 10.1.1.0/24
VpcId: !Ref VPC
Tags:
- Key: "Name"
Value: "Raffael Private Subnet"
InternetGateway:
Type: AWS::EC2::InternetGateway
DependsOn: VPC
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
PublicRouteToIGW:
Type: AWS::EC2::Route
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: PublicSubnet
RouteTableId:
Ref: PublicRouteTable
PrivateSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: PrivateSubnet
RouteTableId:
Ref: PrivateRouteTable
PublicInstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
PrivateInstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
SourceSecurityGroupId: !GetAtt PublicInstanceSecurityGroup.GroupId
Nat:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NatEIP.AllocationId
SubnetId: !Ref PublicSubnet
NatEIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
PrivateRouteToNat:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTable
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref Nat
S3GatewayEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
RouteTableIds:
- !Ref PublicRouteTable
- !Ref PrivateRouteTable
ServiceName: !Sub com.amazonaws.${AWS::Region}.s3
VpcId: !Ref VPC
有两个 EC2 实例 - 一个在私有子网中,一个在公共子网中。这些和相关的安全组在第二个模板中设置。
如果我通过公共 EC2 SSH 到私有 EC2,我可以从私有子网访问 S3。含义 aws s3 ls 列出所有存储桶。
但是 - 这个请求显然是通过 NAT 的。因为如果我通过将其路由的DestinationCidrBlock 设置为1.2.3.4/32 来有效地停用它,那么aws s3 ls 就会超时:
HTTPSConnectionPool(host='s3.amazonaws.com', port=443):
Max retries exceeded with url: / (Caused by ConnectTimeoutError
(<botocore.awsrequest.AWSHTTPSConnection object at 0x7fd86b183828>,
'Connection to s3.amazonaws.com timed out. (connect timeout=60)'))
所以我看了一下Why can’t I connect to an S3 bucket using a gateway VPC endpoint?:
- VPC 中的 DNS 设置:VPC 的“DNS 解析”设置为“已启用”
- 到 Amazon S3 的路由表设置:有一条路由
- 安全组出站规则:无限制
- 网络 ACL 规则:无限制
- 网关 VPC 终端节点策略:无限制
- S3 存储桶策略:我正在使用
ListBucket进行测试
知道我必须在模板中调整什么吗?
【问题讨论】:
-
我看到的一个问题是您在 VPCGatewayAttachment 上的 EIP 上缺少必需的 DependsOn 属性。这很可能不是 S3 网关无法正常工作的原因,但拥有该属性仍然是一种很好的做法。
-
实际上,关于 DependsOn,我不确定您指的是哪里。该链接意味着我必须将
DependsOn: AttachGateway放入 EC2-Resource(公共子网的)中。你是这个意思吗?这里唯一的 EIP 与 VPCGatewayAttachment 无关。 -
@Marcin 我认为您指的是此处最后一个示例中指示的内容:docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…。这个例子让我很困惑 b/c 没有指定
VPCGatewayAttach并且没有这个DependsOn它可以工作,所以我把它省略了。 -
检查您发布的链接中的示例。它有
DependsOn: VPCGatewayAttach用于 EIP。
标签: amazon-web-services amazon-cloudformation