【问题标题】:Unable to connect EC2 instance using Session Manager无法使用 Session Manager 连接 EC2 实例
【发布时间】:2020-11-25 20:17:36
【问题描述】:

我正在使用 Cloudformation 将 EC2 实例部署到不同的 VPC。使用 cloudformation,我创建了一个角色和角色配置文件并将其附加到 EC2 实例 [有问题的角色是 AmazonEC2RoleforSSM ]。然而,我无法使用理智连接到 EC2。 这是 Cloudformation 代码: AWS模板格式版本:2010-09-09 描述:测试和开发环境

Resources:
  VPCdev:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      EnableDnsSupport: true
      InstanceTenancy: default
      Tags:
        - Key: Name
          Value: VPCdev
  VPCtest:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: 192.168.0.0/16
      EnableDnsHostnames: true
      EnableDnsSupport: true
      InstanceTenancy: default
      Tags:
        - Key: Name
          Value: VPCtest
  SubnetDev:
    Type: "AWS::EC2::Subnet"
    Properties:
      AvailabilityZone: !Select [0, !GetAZs '']
      CidrBlock: 10.0.1.0/24
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: SubnetDev
      VpcId: !Ref VPCdev
  SubnetTest:
    Type: "AWS::EC2::Subnet"
    Properties:
      AvailabilityZone: !Select [0, !GetAZs '']
      CidrBlock: 192.168.1.0/24
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: Subnettest
      VpcId: !Ref VPCtest
  IGWdev:
    Type: "AWS::EC2::InternetGateway"
    Properties:
      Tags:
        - Key: Name
          Value: IGWdev
  IGWtest:
    Type: "AWS::EC2::InternetGateway"
    Properties:
      Tags:
        - Key: Name
          Value: IGWtest

  IGWdevattachment:
    Type: "AWS::EC2::VPCGatewayAttachment"
    Properties:
      InternetGatewayId: !Ref IGWdev
      VpcId: !Ref VPCdev
  IGWtestattachment:
    Type: "AWS::EC2::VPCGatewayAttachment"
    Properties:
      InternetGatewayId: !Ref IGWtest
      VpcId: !Ref VPCtest

  RouteTabledev:
    Type: "AWS::EC2::RouteTable"
    Properties:
      Tags:
        - Key: Name
          Value: RouteTabledev
      VpcId: !Ref VPCdev
  RouteTabletest:
    Type: "AWS::EC2::RouteTable"
    Properties:
      Tags:
        - Key: Name
          Value: RouteTabletest
      VpcId: !Ref VPCtest

  defaultdev:
    Type: "AWS::EC2::Route"
    Properties:
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref IGWdev
      RouteTableId: !Ref RouteTabledev

  defaulttest:
    Type: "AWS::EC2::Route"
    Properties:
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref IGWtest
      RouteTableId: !Ref RouteTabletest

  Ec2InstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Path: /
      Roles: [ !Ref Ec2InstanceRole ]
  Ec2InstanceRole:
    Type: AWS::IAM::Role
    Properties:
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service: [ ec2.amazonaws.com ]
            Action:
              - sts:AssumeRole
      Path: /

  sgdev:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      GroupDescription: sgdev
      GroupName: sgdev
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: sgdev
      VpcId: !Ref VPCdev
  sgtest:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      GroupDescription: sgtest
      GroupName: sgtest
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: sgtest
      VpcId: !Ref VPCtest

  instancedev:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-02d55cb47e83a99a0
      InstanceType: "t2.micro"
      IamInstanceProfile: !Ref Ec2InstanceProfile
      NetworkInterfaces:
        - AssociatePublicIpAddress: true
          DeviceIndex: 0
          GroupSet:
            - !Ref sgdev
          SubnetId: !Ref SubnetDev
      Tags:
        - Key: Name
          Value: dev

  instancetest:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-02d55cb47e83a99a0
      InstanceType: "t2.micro"
      IamInstanceProfile: !Ref Ec2InstanceProfile
      NetworkInterfaces:
        - AssociatePublicIpAddress: true
          DeviceIndex: 0
          GroupSet:
            - !Ref sgtest
          SubnetId: !Ref SubnetTest
      Tags:
        - Key: Name
          Value: test  

还附上了错误截图。 P.S:我尝试手动执行它并且它有效。似乎不明白我哪里出错了,堆栈已成功构建,所有资源也已部署。

我使用的是 Ubuntu 18.04 LTS Image,它预装了 Session Manager

【问题讨论】:

  • 您是否在实例上启动了 ssm 服务?我没有看到您在堆栈中执行此操作,因此必须在 AMI 中启动它,但我不知道默认运行 ssm 服务的任何上游 amis。
  • @DanielFarrell 根据 aws 文档,所有 linux 操作系统上都预装了 ssm 代理,在这种情况下,在 ubuntu 16 以上的情况下,它应该可以工作。当我尝试手动部署它时,它无需安装任何代理即可工作。
  • 安装和启动有时是不同的事情。但很公平,如果它是手动工作的,那就不是了。 IT 可能是网络阻止服务器呼叫 ssm 的东西。您是否在堆栈的 vpc、子网和安全组中启动了手动测试?
  • @DanielFarrell 错误是因为我没有将子网关联到路由表并且无法访问。感谢您的宝贵时间!

标签: amazon-web-services amazon-ec2 amazon-cloudformation aws-session-manager


【解决方案1】:

它不起作用,因为您的子网是私有的,它们没有任何互联网连接。

尽管您创建了公共路由表,但它们不关联与任何子网。

以下内容应该会有所帮助:

  MyRouteTableAssoc1:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties: 
      RouteTableId: !Ref RouteTabledev
      SubnetId: !Ref SubnetDev    

  MyRouteTableAssoc2:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties: 
      RouteTableId: !Ref RouteTabletest
      SubnetId: !Ref SubnetTest

【讨论】:

  • 谢谢,我完全错过了子网关联,现在可以使用了
猜你喜欢
  • 2020-08-07
  • 2021-05-07
  • 2014-03-03
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 2013-05-26
  • 2020-10-18
相关资源
最近更新 更多