Amazon web services Can';t连接到使用CloudFormation制作的公共EC2
我从昨天开始就一直在努力让这个CloudFormation模板工作。。。目标是将EC2实例启动到我可以通过HTTP访问的公共子网中。在我看来,所有内容都已正确创建,但该实例无法在浏览器中连接。我检查过的东西:Amazon web services Can';t连接到使用CloudFormation制作的公共EC2,amazon-web-services,amazon-ec2,amazon-cloudformation,Amazon Web Services,Amazon Ec2,Amazon Cloudformation,我从昨天开始就一直在努力让这个CloudFormation模板工作。。。目标是将EC2实例启动到我可以通过HTTP访问的公共子网中。在我看来,所有内容都已正确创建,但该实例无法在浏览器中连接。我检查过的东西: EC2实例部署在正确的子网中 子网有一个指向IGW的路由表 EC2实例有一个公共IP 安全组允许从所有来源通过端口80、443和22进行入站访问 在没有用户数据的情况下创建 已验证的用户数据在手动EC2中正常工作 对其他需要检查的东西有什么建议吗 这是我的模板: Resources:
- EC2实例部署在正确的子网中
- 子网有一个指向IGW的路由表
- EC2实例有一个公共IP
- 安全组允许从所有来源通过端口80、443和22进行入站访问
- 在没有用户数据的情况下创建
- 已验证的用户数据在手动EC2中正常工作
Resources:
myVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.2.0.0/16
Tags:
- Key: Name
Value: myVPC
WebDMZcf:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Open to HTTP, HTTPS and SSH on all ports
GroupName: WebDMZcf
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: WebDMZcf
VpcId:
Ref: myVPC
myInternetGatewayCF:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: myInternetGatewayCF
myInternetGatewayCFAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId:
Ref: myInternetGatewayCF
VpcId:
Ref: myVPC
myRouteTableCF:
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value: myRouteTableCF
VpcId:
Ref: myVPC
IGWRoute:
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId:
Ref: myInternetGatewayCF
RouteTableId:
Ref: myRouteTableCF
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
CidrBlock: 10.2.1.0/24
# MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: PublicSubnet
VpcId:
Ref: myVPC
PublicEC2:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-047a51fa27710816e
InstanceType: t2.micro
NetworkInterfaces:
- AssociatePublicIpAddress: True
DeviceIndex: 0
SubnetId:
Ref: PublicSubnet
DeleteOnTermination: True
GroupSet:
- Ref: WebDMZcf
Tags:
- Key: Name
Value: PublicEC2
UserData:
!Base64 |
#!/bin/bash
# Install Apache Web Server
yum install httpd -y
systemctl start httpd
systemctl enable httpd
# Discovery configuration from using the EC2 metadata service
ID=$(curl 169.254.169.254/latest/meta-data/instance-id)
TYPE=$(curl 169.254.169.254/latest/meta-data/instance-type)
AZ=$(curl 169.254.169.254/latest/meta-data/placement/availability-zone)
IPV4=$(curl -f 169.254.169.254/latest/meta-data/public-ipv4)
# Set up the Web Site
cd /var/www/html
## Generate customized index.html for this instance
echo "<html><body><H1>Hello, EC2 Instance!</H1><p><p>" > ./index.html
echo "The ID of this instance is " >> ./index.html
echo "<strong>$ID</strong>.<p><p>" >> ./index.html
echo "This is a <strong>$TYPE</strong> instance" >> ./index.html
echo " in <strong>$AZ</strong>. <p><p>" >> ./index.html
if [ "$IPV4" ];
then
echo "The public ip is <strong>$IPV4</strong>.<p><p>" >> ./index.html
else
echo "This instance does <strong>NOT</strong> have" >> ./index.html
echo "a public ip address.<p><p>" >> ./index.html
fi
echo "</body></html>" >> ./index.html
资源:
多年电价委员会:
类型:AWS::EC2::VPC
特性:
CidrBlock:10.2.0.0/16
标签:
-关键字:姓名
价值:myVPC
WebDMZcf:
类型:AWS::EC2::SecurityGroup
特性:
GroupDescription:在所有端口上对HTTP、HTTPS和SSH开放
组名:WebDMZcf
SecurityGroupIngress:
-IpProtocol:tcp
FromPort:80
托波特:80
CidrIp:0.0.0.0/0
-IpProtocol:tcp
发信人:22
托波特:22
CidrIp:0.0.0.0/0
-IpProtocol:tcp
FromPort:443
托波特:443
CidrIp:0.0.0.0/0
标签:
-关键字:姓名
值:WebDMZcf
VpcId:
参考:myVPC
myInternetGatewayCF:
类型:AWS::EC2::InternetGateway
特性:
标签:
-关键字:姓名
价值:myInternetGatewayCF
MyInternetGatewayC附件:
类型:AWS::EC2::VPCGatewayAttachment
特性:
InternetGatewayId:
参考:myInternetGatewayCF
VpcId:
参考:myVPC
myRouteTableCF:
类型:AWS::EC2::RouteTable
特性:
标签:
-关键字:姓名
值:myRouteTableCF
VpcId:
参考:myVPC
IGWRoute:
类型:AWS::EC2::路由
特性:
目标IDRBLOCK:0.0.0.0/0
网关ID:
参考:myInternetGatewayCF
RouteTableId:
参考:myRouteTableCF
公共子网:
类型:AWS::EC2::子网
特性:
CidrBlock:10.2.1.0/24
#MapPublicIpOnLaunch:对
标签:
-关键字:姓名
值:PublicSubnet
VpcId:
参考:myVPC
PublicEC2:
类型:AWS::EC2::实例
特性:
图像ID:ami-047a51fa27710816e
实例类型:t2.micro
网络接口:
-AssociatePublicAddress:True
DeviceIndex:0
子网:
参考:公共子网
DeleteOnTermination:True
组集:
-参考:WebDMZcf
标签:
-关键字:姓名
价值:PublicEC2
用户数据:
!Base64|
#!/bin/bash
#安装Apache Web服务器
yum安装httpd-y
systemctl启动httpd
systemctl启用httpd
#使用EC2元数据服务的发现配置
ID=$(curl 169.254.169.254/最新/元数据/实例ID)
TYPE=$(curl 169.254.169.254/最新/元数据/实例类型)
AZ=$(curl 169.254.169.254/最新/元数据/位置/可用区)
IPV4=$(curl-f 169.254.169.254/latest/meta-data/public-IPV4)
#设置网站
cd/var/www/html
##为此实例生成自定义index.html
echo“你好,EC2实例!”>。/index.html
echo“此实例的ID为”>>。/index.html
echo“$ID”>./index.html
echo“这是一个$TYPE实例”>>。/index.html
echo“in$AZ”>./index.html
如果[“$IPV4”];
然后
echo“公共ip是$IPV4”>./index.html
其他的
echo“此实例不具有”>>。/index.html
echo“一个公共ip地址。”>./index.html
fi
echo”“>>。/index.html
编辑:
我现在已经向该实例添加了一个密钥对,但我无法在其中使用SSH。当我尝试使用控制台中的“连接”按钮时,我得到以下结果:
连接到您的实例时出现问题,我们无法连接到您的实例。确保为EC2实例连接正确配置了实例的网络设置。有关更多信息,请参阅[任务1:配置实例的网络访问][1]
,我现在正在调查此问题。您没有,因此您的公用子网与VPC的默认路由表不关联,因此您的公用子网没有到Internet网关的默认路由,无法访问Internet
添加以下内容:
PublicSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref myRouteTableCF
SubnetId: !Ref PublicSubnet
您没有,因此您的公共子网不与VPC的默认路由表关联,因此您的公共子网没有到Internet网关的默认路由,无法访问Internet
添加以下内容:
PublicSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref myRouteTableCF
SubnetId: !Ref PublicSubnet
您的安全组没有出口规则,这意味着所有出站流量都被阻止。它使用默认出口规则,允许所有流量:所有流量都是0.0.0.0/0Ah,terraform不应用默认出口规则,我假设CF也没有。您的安全组没有出口规则,这意味着所有出站流量都被阻止。它使用默认的出口规则,允许所有流量:所有流量全部0.0.0.0/0Ah,terraform不应用默认的出口规则,我假设CF也不应用。