繁体   English   中英

AWS System Manager 启动 session:调用 StartSession 操作时发生错误 (TargetNotConnected):<instance_id> 未连接</instance_id>

[英]AWS System Manager start session: An error occurred (TargetNotConnected) when calling the StartSession operation: <instance_id> is not connected

问题:

当我尝试使用 AWS System Session Manager CLI 命令在本地连接到正在运行的 EC2 实例时: aws ssm start-session --target i-123456

我收到错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.

背景:

  • Linux 2 个实例托管在自定义 VPC 内的私有 su.net 上
  • VPC 端点用于将 System Manager 连接到托管实例,而无需 NAT GW 或 IGW。
  • 端点服务名称:
com.amazonaws.us-west-2.s3
com.amazonaws.us-west-2.ec2
com.amazonaws.us-west-2.ec2messages
com.amazonaws.us-west-2.ssm
com.amazonaws.us-west-2.ssmmessages
  • AWS CLI == 2.0.40
  • Python == 3.7.4
  • 自定义 Terraform 模块以在其中一个私有 su.net 中启动 airflow 实例(参见下面的模块“airflow_aws_resources”)
  • 与此问题相关的only.tf文件是模块“airflow_aws_resources”中的 airflow.tf。 此文件包含通过 SSM 连接的 EC2 实例的安全组和实例配置文件配置。

用 Terraform 重现:

module "airflow_aws_resources" {
  source                      = "github.com/marshall7m/tf_modules/airflow-aws-resources"
  resource_prefix             = "test"
  vpc_id                      = module.vpc.vpc_id
  env                         = "testing"
  private_bucket              = "test-bucket"
  private_subnets_ids         = module.vpc.private_subnets
  private_subnets_cidr_blocks = module.vpc.private_subnets_cidr_blocks

  create_airflow_instance     = true
  create_airflow_instance_sg  = true
  create_airflow_db           = false
  create_airflow_db_sg        = false
  airflow_instance_ssm_access = true
  airflow_instance_ssm_region = "us-west-2"

  airflow_instance_ami  = "ami-0841edc20334f9287"
  airflow_instance_type = "t2.micro"

}

resource "aws_security_group" "vpc_endpoints" {
  name        = "test-vpc-endpoint-sg"
  description = "Default security group for vpc endpoints"
  vpc_id = module.vpc.vpc_id
  
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["10.0.0.32/28", "10.0.0.64/28"]
  }

  ingress {
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    #private subnet cidr blocks
    cidr_blocks = ["10.0.0.32/28", "10.0.0.64/28"]
  }

  egress {
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = ["10.0.0.32/28", "10.0.0.64/28"]
  }
  egress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["10.0.0.32/28", "10.0.0.64/28"]
  }
}

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
  version = "2.44.0"
  name = "test-vpc" 
  cidr = "10.0.0.0/24"

  azs = ["us-west-2a", "us-west-2b"]
  
  private_subnets = ["10.0.0.32/28", "10.0.0.64/28"]
  private_dedicated_network_acl = true
  private_subnet_suffix = "private"

  public_subnets = ["10.0.0.96/28", "10.0.0.128/28"]
  public_dedicated_network_acl = true
  public_subnet_suffix = "public"

  enable_s3_endpoint = true

  enable_ec2messages_endpoint = true
  ec2messages_endpoint_security_group_ids = [aws_security_group.vpc_endpoints.id]
  enable_ec2_endpoint = true
  ec2_endpoint_security_group_ids = [aws_security_group.vpc_endpoints.id]

  enable_ssm_endpoint = true
  ssm_endpoint_security_group_ids = [aws_security_group.vpc_endpoints.id]
  enable_ssmmessages_endpoint = true
  ssmmessages_endpoint_security_group_ids = [aws_security_group.vpc_endpoints.id]

  enable_nat_gateway = false
  single_nat_gateway = false
  enable_vpn_gateway = false

  create_database_subnet_route_table = false
  create_database_internet_gateway_route = false
  create_database_subnet_group = false
   
  manage_default_network_acl = false 
  enable_dns_hostnames = true
  enable_dns_support = true
  
  private_inbound_acl_rules = [
    {
      "description": "Allows inbound https traffic for aws s3 package requests"
      "cidr_block": "0.0.0.0/0",
      "from_port": 443,
      "to_port": 443,
      "protocol": "tcp",
      "rule_action": "allow",
      "rule_number": 101
    },
    { 
      "description": "Allows inbound http traffic for aws s3 package requests"
      "cidr_block": "0.0.0.0/0",
      "from_port": 80,
      "to_port": 80,
      "protocol": "tcp",
      "rule_action": "allow",
      "rule_number": 102
    }
  ]
  private_outbound_acl_rules = [
    {
      "description": "Allows outbound https traffic for aws s3 package requests"
      "cidr_block": "0.0.0.0/0",
      "from_port": 443,
      "to_port": 443,
      "protocol": "tcp",
      "rule_action": "allow",
      "rule_number": 101
    },
    { 
      "description": "Allows outbound http traffic for aws s3 package requests"
      "cidr_block": "0.0.0.0/0",
      "from_port": 80,
      "to_port": 80,
      "protocol": "tcp",
      "rule_action": "allow",
      "rule_number": 102
    }
  ]
  
  vpc_endpoint_tags = {
    type = "vpc-endpoint"
  }
}

尝试:

#1

我尝试了 EC2 控制台 SSM 中的故障排除提示(AWS Ec2 控制台 >> instance-id >> 连接 >> Session 管理器):

控制台ssm

  1. SSM 代理已预安装在 AWS Linux 实例类型上。 尽管我通过 SSH 访问实例并运行sudo status amazon-ssm-agent进行了双重检查,结果返回: amazon-ssm-agent start/running, process 1234

  2. 上面显示的 EC2 实例配置文件包括所需的AmazonSSMManagedInstanceCore策略

  3. 我完成了 Session 经理先决条件。

#2

使用以下命令将AmazonSSMFullAccess附加到用户: aws ssm start-session --target i-123456

通过 SSM 连接实例时出现同样的错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.

#3

将来自 VPC 端点的关联私有 su.net 的 HTTPS 入站/出站流量添加到 EC2 实例安全组(参见airflow.tf

同样的错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.

#4

在 System Manager 控制台中,我使用了 Quick Setup 选项,并使用airflow.tf中指定的实例配置文件和具有默认角色的 System Manager 角色配置了 Quick Setup。 ec2 实例在快速设置页面中成功注册了“托管实例”。

同样的错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.

#5

鉴于这是一个测试 VPC 和 EC2 实例,我尝试允许来自所有 IPv4 源 (0.0.0.0/0) 的所有类型的流量用于以下资源:

  • 私有 su.net NACL
  • EC2 实例安全组
  • 与以下接口/网关端点关联的安全组:
com.amazonaws.us-west-2.s3
com.amazonaws.us-west-2.ec2
com.amazonaws.us-west-2.ec2messages
com.amazonaws.us-west-2.ssm
com.amazonaws.us-west-2.ssmmessages

通过 SSM 连接实例时出现同样的错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.

我会在这里参考以确保您已正确设置所有内容。 我将首先添加配置文件参数。 如果这仍然不起作用,当我的配置文件默认区域与我希望开始活动会话的区域不同时,我遇到了类似的问题。 因此,我也需要使用 region 参数。 示例 .ssh/config 如下:

host ssh i-abc123
ProxyCommand sh -c "aws --region desired_region --profile my_profile ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

我还鼓励使用 AWS CLI v2。 将 .ssh/config 配置为与上面类似后,只需在 CLI 中执行以下命令:

ssh i-abc123

因此,您可能需要使用配置文件。 我在 OSX 上使用 AWS CLI 通过终端连接到 VPC 中的 linux 主机。 这是一个只能通过 SSO 访问的帐户。 我能够创建一个配置文件,并且在通过 CLI 对 SSO 进行身份验证后,我可以建立这样的连接。

这样做一次

aws sso login --profile my_customer  

然后用一个简单的命令验证 sso 登录是否成功(在我的 osx 终端上)

 aws s3 ls --profile my_customer  custbucket-s3-sftp/rds/

现在建立会话管理器连接

 aws ssm start-session --profile my_customer  --target i-0012345abcdef890

我知道您正在使用 python,但也许这会有所帮助。

在某些情况下,您必须验证以下内容:

  • AWS 账户/资料
  • AWS 区域

在一种情况下,我发现它正在尝试连接到 aws 配置文件。

后来在其他情况下,我连接到不同的区域。

就我而言,在使用 AWS 控制台/UI 将 IAM 角色附加到 EC2 后,我不得不等待大约 10 分钟

当我尝试从终端连接时,我也遇到了同样的错误: An error occurred (TargetNotConnected) when calling the StartSession operation: i-122334455 is not connected.

就我而言,问题在于目标实例上安装的 SSM 已过期。 我通过尝试从 AWS 控制台中的 Systems Manager 启动会话发现了这一点; 基本上去 Systems Manager->Fleet Manager->{INSTANCE_ID}->Instance Actions->Start Session。 当我尝试这样做时,我收到目标 ec2 实例上的 SSM 代理已过期的错误消息。 更新后,我能够成功登录。

要进行更新,您可以为所有托管实例启用 SSM 代理自动更新、手动更新特定实例或对托管实例进行选择性更新。 有关信息,请参阅以下文档:

我遇到了类似的问题。 如果您尝试在位于不同 AWS 账户或 AWS 区域的托管节点上启动 session,您将看到类似的错误。 例如,我的 aws 实例位于 us-east-2 区域,我的 aws 默认配置文件设置在 us-east-1 中,所以当我运行“aws ssm start-session instance_number”时它返回错误,因为 ssm 不知道us-east-2 区域中的实例。 为了解决这个问题,我删除了区域过滤器名称并运行了“aws ssm start-session instance_number --region us-east-2”。 我能够毫无问题地连接到实例..

说明:不幸的是, ec2实例不是容错的,并且在您的系统服务器下是一个主机系统。 作为最佳实践,您应该添加另一个实例来备份并防止单点故障。

当您尝试 ssm/ssh 您的主机并出现TargetNotConnected问题时,可能的原因之一可能是由于以下几个原因:如果主机硬件出现故障、连接/电力问题、软件内存泄漏(内存不足)、未满的磁盘清理或您的应用程序可以处理边缘情况并自行崩溃。

在这种情况下,尽管可达性失败,但 ec2 实例状态可能仍在运行。

当您运行aws ec2 describe-instance-status --instance-ids <instance-id>时,您可能会注意到实例状态正在运行,但运行状况检查失败。

示例

请求: aws ec2 describe-instance-status --instance-ids i-abc123

回复:

{
    "InstanceStatuses": [
        {
            "AvailabilityZone": "us-west-1b",
            "InstanceId": "i-abc123",
            "InstanceState": {
                "Code": 16,
                "Name": "running"
            },
            "InstanceStatus": {
                "Details": [
                    {
                        "ImpairedSince": "2020-10-10T12:10:00+00:00",
                        "Name": "reachability",
                        "Status": "failed"
                    }
                ],
                "Status": "impaired"
            },
            "SystemStatus": {
                "Details": [
                    {
                        "Name": "reachability",
                        "Status": "passed"
                    }
                ],
                "Status": "ok"
            }
        }
    ]
}

如果它是硬件问题(在 iaac 平台中,例如 terraform / clodformation 或手动当然),如果它是应用问题连接到机器并解决确切问题,则解决方案将再次重新创建此实例。

您的Interface类型 VPC 终端节点是否启用了私有 DNS?

会话管理器似乎需要在Interface类型的 Terraform VPC 端点中使用private_dns_enabled = true才能工作。

在使用修改了 EC2 实例的 terraform 进行了一些更改后,我遇到了这个问题。 原来我需要做的就是重新启动 EC2,然后它允许我再次连接

私有 ec2 实例需要访问 Inte.net(ssm 点)

所以需要用NAT网关来实现。

另一个可能的陷阱:

将 VPC 端点上的安全组与附加到我的 EC2 实例的安全组混淆了。 起初我将其解释为授予该安全组(和实例)访问我的 VPC 端点的权限。

相反,我需要创建一个新的安全组来指定我的 VPC 端点上允许的入站/出站流量。 来自AWS 文档

附加到 VPC 端点的安全组必须允许端口 443 上来自托管实例的私有 su.net 的传入连接。 如果不允许传入连接,则托管实例无法连接到 SSM 和 EC2 端点。

因此,我添加了规则以允许所有 HTTPS 流量进/出 VPC 端点。

这与 ec2 实例上的安全组是分开的,我允许所有出站流量但不允许入站流量。

一旦我将新的 VPC 安全组添加到我的每个 VPC 端点,该实例就会在 session 管理器中显示为已连接并准备启动会话。

问题:

当我尝试使用AWS System Session Manager CLI命令本地连接到正在运行的EC2实例时: aws ssm start-session --target i-123456

我得到了错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.

背景:

  • Linux 2实例托管在自定义VPC内的私有子网上
  • VPC 端点用于将系统管理器连接到托管实例,而无需NAT GW或IGW。
  • 端点服务名称:
com.amazonaws.us-west-2.s3
com.amazonaws.us-west-2.ec2
com.amazonaws.us-west-2.ec2messages
com.amazonaws.us-west-2.ssm
com.amazonaws.us-west-2.ssmmessages
  • AWS CLI == 2.0.40
  • Python == 3.7.4
  • 定制Terraform模块,用于在私有子网之一中启动气流实例(请参见下面的模块“ airflow_aws_resources”)
  • 唯一.tf文件,这将是有关这个问题将是airflow.tf模块“airflow_aws_resources”内。 该文件包含通过SSM连接的EC2实例的安全组和实例配置文件配置。

用Terraform复制:

module "airflow_aws_resources" {
  source                      = "github.com/marshall7m/tf_modules/airflow-aws-resources"
  resource_prefix             = "test"
  vpc_id                      = module.vpc.vpc_id
  env                         = "testing"
  private_bucket              = "test-bucket"
  private_subnets_ids         = module.vpc.private_subnets
  private_subnets_cidr_blocks = module.vpc.private_subnets_cidr_blocks

  create_airflow_instance     = true
  create_airflow_instance_sg  = true
  create_airflow_db           = false
  create_airflow_db_sg        = false
  airflow_instance_ssm_access = true
  airflow_instance_ssm_region = "us-west-2"

  airflow_instance_ami  = "ami-0841edc20334f9287"
  airflow_instance_type = "t2.micro"

}

resource "aws_security_group" "vpc_endpoints" {
  name        = "test-vpc-endpoint-sg"
  description = "Default security group for vpc endpoints"
  vpc_id = module.vpc.vpc_id
  
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["10.0.0.32/28", "10.0.0.64/28"]
  }

  ingress {
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    #private subnet cidr blocks
    cidr_blocks = ["10.0.0.32/28", "10.0.0.64/28"]
  }

  egress {
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = ["10.0.0.32/28", "10.0.0.64/28"]
  }
  egress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["10.0.0.32/28", "10.0.0.64/28"]
  }
}

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
  version = "2.44.0"
  name = "test-vpc" 
  cidr = "10.0.0.0/24"

  azs = ["us-west-2a", "us-west-2b"]
  
  private_subnets = ["10.0.0.32/28", "10.0.0.64/28"]
  private_dedicated_network_acl = true
  private_subnet_suffix = "private"

  public_subnets = ["10.0.0.96/28", "10.0.0.128/28"]
  public_dedicated_network_acl = true
  public_subnet_suffix = "public"

  enable_s3_endpoint = true

  enable_ec2messages_endpoint = true
  ec2messages_endpoint_security_group_ids = [aws_security_group.vpc_endpoints.id]
  enable_ec2_endpoint = true
  ec2_endpoint_security_group_ids = [aws_security_group.vpc_endpoints.id]

  enable_ssm_endpoint = true
  ssm_endpoint_security_group_ids = [aws_security_group.vpc_endpoints.id]
  enable_ssmmessages_endpoint = true
  ssmmessages_endpoint_security_group_ids = [aws_security_group.vpc_endpoints.id]

  enable_nat_gateway = false
  single_nat_gateway = false
  enable_vpn_gateway = false

  create_database_subnet_route_table = false
  create_database_internet_gateway_route = false
  create_database_subnet_group = false
   
  manage_default_network_acl = false 
  enable_dns_hostnames = true
  enable_dns_support = true
  
  private_inbound_acl_rules = [
    {
      "description": "Allows inbound https traffic for aws s3 package requests"
      "cidr_block": "0.0.0.0/0",
      "from_port": 443,
      "to_port": 443,
      "protocol": "tcp",
      "rule_action": "allow",
      "rule_number": 101
    },
    { 
      "description": "Allows inbound http traffic for aws s3 package requests"
      "cidr_block": "0.0.0.0/0",
      "from_port": 80,
      "to_port": 80,
      "protocol": "tcp",
      "rule_action": "allow",
      "rule_number": 102
    }
  ]
  private_outbound_acl_rules = [
    {
      "description": "Allows outbound https traffic for aws s3 package requests"
      "cidr_block": "0.0.0.0/0",
      "from_port": 443,
      "to_port": 443,
      "protocol": "tcp",
      "rule_action": "allow",
      "rule_number": 101
    },
    { 
      "description": "Allows outbound http traffic for aws s3 package requests"
      "cidr_block": "0.0.0.0/0",
      "from_port": 80,
      "to_port": 80,
      "protocol": "tcp",
      "rule_action": "allow",
      "rule_number": 102
    }
  ]
  
  vpc_endpoint_tags = {
    type = "vpc-endpoint"
  }
}

尝试次数:

#1

我尝试了EC2控制台SSM(AWS Ec2控制台>>实例ID >>连接>>会话管理器)中的故障排除技巧:

控制台-ssm

  1. SSM代理已预先安装在AWS Linux实例类型上。 尽管我通过SSH访问实例并运行sudo status amazon-ssm-agent并通过返回以下内容amazon-ssm-agent start/running, process 1234amazon-ssm-agent start/running, process 1234

  2. 上面显示的EC2实例配置文件包括必需的AmazonSSMManagedInstanceCore策略

  3. 我完成了会话管理器先决条件。

#2

使用以下命令将AmazonSSMFullAccess附加到用户: aws ssm start-session --target i-123456

通过SSM连接实例时出现相同的错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.

#3

将来自VPC端点的关联专用子网的HTTPS入站/出站流量添加到EC2实例安全组(请参阅airflow.tf

同样的错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.

#4

在系统管理器控制台中,我使用了“快速设置”选项,并使用airflow.tf中指定的“实例”配置文件配置了“快速设置”,并使用默认角色配置了“系统管理器”角色。 ec2实例已在快速设置页面中成功注册了“托管实例”。

同样的错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.

#5

鉴于这是一个测试VPC和EC2实例,我尝试为以下资源允许来自所有IPv4源(0.0.0.0/0)的所有类型的流量:

  • 专用子网NACL
  • EC2实例安全组
  • 与以下接口/网关端点关联的安全组:
com.amazonaws.us-west-2.s3
com.amazonaws.us-west-2.ec2
com.amazonaws.us-west-2.ec2messages
com.amazonaws.us-west-2.ssm
com.amazonaws.us-west-2.ssmmessages

通过SSM连接实例时出现相同的错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM