【问题标题】:Cannot connect to EC2 via SSH | AWS Cloudformation Template无法通过 SSH 连接到 EC2 | AWS Cloudformation 模板
【发布时间】:2021-12-30 11:16:50
【问题描述】:

我有以下 CloudFormation 模板,用于在单个可用区的单个公有子网中创建 EC2 实例。我已将 Internet 网关连接到 VPC,并创建了入口和出口路由以允许通过 SSH 连接到 EC2 实例。

下面是我的 CF 模板

AWSTemplateFormatVersion: "2010-09-09"
Description: "CF template for test website. v1.0.0. DEV Env"
Metadata:
  Instances: 
    Description: "This is the dev environment architecture. Use the dev settings when setting up this environment"
Parameters:
  ECommKeyPair:
    Type: AWS::EC2::KeyPair::KeyName
    Description: Select the dev key pair for the region
Resources:
  DevEnvInternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Environment
          Value: Dev
        - Key: WebsiteName
          Value: test
  DevEnvVpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.1.1/16
      EnableDnsHostnames: 'true'
      EnableDnsSupport: 'true'
      Tags:
        - Key: Environment
          Value: Dev
        - Key: WebsiteName
          Value: test
  DevEnvVpcIgwAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId:
        Ref: DevEnvVpc
      InternetGatewayId:
        Ref: DevEnvInternetGateway
  DevEnvPublicSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId:
        Ref: DevEnvVpc
      CidrBlock: 10.0.1.1/16
      AvailabilityZone: "us-west-2a"
      MapPublicIpOnLaunch: 'true'
      Tags:
        - Key: Environment
          Value: Dev
        - Key: WebsiteName
          Value: test
  DevEnvSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow all inbound (ingress) and outbound (egress) traffic for port 22
      GroupName: test-website-sec-group
      VpcId:
        Ref: DevEnvVpc
      SecurityGroupIngress:
        - CidrIp: 0.0.0.0/0
          Description: allow all inbound traffic
          IpProtocol: tcp
          FromPort: 22
          ToPort: 22
      SecurityGroupEgress:
        - CidrIp: 0.0.0.0/0
          Description: allow all outbound traffic
          IpProtocol: tcp
          FromPort: 22
          ToPort: 22
      Tags:
        - Key: Environment
          Value: Dev
        - Key: WebsiteName
          Value: test
  DevEnvRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId:
        Ref: DevEnvVpc
      Tags:
        - Key: Environment
          Value: Dev
        - Key: WebsiteName
          Value: test
  DevEnvRoute:
    Type: AWS::EC2::Route
    Properties:
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId:
        Ref: DevEnvInternetGateway
      RouteTableId:
        Ref: DevEnvRouteTable
  DevEnvEc2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: ami-00f7e5c52c0f43726
      AvailabilityZone: "us-west-2a"
      KeyName:
        Ref: ECommKeyPair
      SecurityGroupIds:
        - !GetAtt "DevEnvSecurityGroup.GroupId"
      SubnetId:
        Ref: DevEnvPublicSubnet
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            VolumeSize: 20
            VolumeType: gp2
      Tags:
        - Key: Environment
          Value: Dev
        - Key: WebsiteName
          Value: test

我正在使用 Putty 通过与 EC2 实例关联的私钥文件 (ppk) 连接到 EC2 实例。当尝试使用 Putty 连接到实例时,它收到“网络错误:连接超时”错误消息。

我什至无法通过 Web 浏览器使用 AWS 内置的“EC2 Instance Connect”连接到实例。

非常感谢您指出我的 CF 模板中的问题。

【问题讨论】:

  • 我没有在实例上看到对 EIP 或 nic 配置的引用。它有公共 IP 地址吗?你不是在尝试 ssh 到它的私有 IP 是吗?
  • 子网在模板中有“MapPublicIpOnLaunch: 'true'”,创建后 EC2 有一个公共 IP 地址和一个公共 dns 名称。

标签: amazon-web-services amazon-ec2 amazon-cloudformation putty


【解决方案1】:

你忘了创建AWS::EC2::SubnetRouteTableAssociation

  DevRouteAssos:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref DevEnvRouteTable
      SubnetId: !Ref DevEnvPublicSubnet

【讨论】:

  • 非常感谢。我已经添加了上面的内容,它对我有用。我可以知道为什么要添加子网路由表关联吗?我的路由与互联网网关(GatewayId)和路由表(RouteTableId)相连,路由表与VPC(VpcId)相连。
  • @cham 但是您的路由表没有连接到子网。不同的子网可以有不同的路由。您没有使用默认路由表,而是创建了新路由表,因此您必须将其分配给子网。
  • 非常感谢您的解释。我理解了这个概念。
猜你喜欢
  • 2018-03-17
  • 2019-08-08
  • 1970-01-01
  • 2013-01-06
  • 2019-12-28
  • 2012-09-04
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
相关资源
最近更新 更多