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

Mar*_*llm 5 amazon-web-services amazon-vpc aws-ssm

问题:

当我尝试使用 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.
Run Code Online (Sandbox Code Playgroud)

背景:

  • Linux 2 实例托管在自定义 VPC 内的私有子网上
  • 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
Run Code Online (Sandbox Code Playgroud)
  • 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"
  }
}

Run Code Online (Sandbox Code Playgroud)

尝试:

#1

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

控制台-ssm

  1. SSM 代理已预安装在 AWS Linux 实例类型上。尽管我通过 SSH 访问实例并运行sudo status amazon-ssm-agent返回的实例进行了双重检查:amazon-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.
Run Code Online (Sandbox Code Playgroud)

#3

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

同样的错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.
Run Code Online (Sandbox Code Playgroud)

#4

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

同样的错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.
Run Code Online (Sandbox Code Playgroud)

#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
Run Code Online (Sandbox Code Playgroud)

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

An error occurred (TargetNotConnected) when calling the StartSession operation: i-123456 is not connected.
Run Code Online (Sandbox Code Playgroud)

小智 9

我会参考这里以确保您已正确设置所有内容。我首先添加配置文件参数。如果这仍然不起作用,当我的个人资料的默认区域与我想要开始活动会话的区域不同时,我遇到了类似的问题。因此,我还需要使用区域参数。下面的 .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'"
Run Code Online (Sandbox Code Playgroud)

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

ssh i-abc123
Run Code Online (Sandbox Code Playgroud)

  • 当我忘记在我的实例所在位置添加“--region us-west-2”时,我同样遇到了此错误,并且它默认为“us-east-1”。 (5认同)

kin*_*ing 7

当我尝试从终端连接时,我也遇到了同样的错误: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 代理自动更新、手动更新特定实例或对托管实例进行选择性更新。请参阅以下文档以获取信息:


小智 5

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

这样做一次

aws sso login --profile my_customer  
Run Code Online (Sandbox Code Playgroud)

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

 aws s3 ls --profile my_customer  custbucket-s3-sftp/rds/
Run Code Online (Sandbox Code Playgroud)

现在建立会话管理器连接

 aws ssm start-session --profile my_customer  --target i-0012345abcdef890
Run Code Online (Sandbox Code Playgroud)

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