

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# AWS CodeBuild 沙箱 SSH 连接问题疑难解答
<a name="sandbox-troubleshooting"></a>

使用本主题中的信息来帮助您识别、诊断和解决 CodeBuild 沙箱 SSH 连接问题。

**Topics**
+ [`StartSandboxConnection``InvalidInputException`SSH 进入 CodeBuild 沙盒环境时出错](#sandbox-troubleshooting.invalid-input)
+ [错误：SSH 进入 CodeBuild 沙盒环境时出现 “找不到凭证”](#sandbox-troubleshooting.credentials)
+ [`StartSandboxConnection``AccessDeniedException`SSH 进入 CodeBuild 沙盒环境时出错](#sandbox-troubleshooting.access-denied)
+ [SSH 进入 CodeBuild 沙盒环境时出现错误：“ssh：无法解析主机名”](#sandbox-troubleshooting.hostname)

## `StartSandboxConnection``InvalidInputException`SSH 进入 CodeBuild 沙盒环境时出错
<a name="sandbox-troubleshooting.invalid-input"></a>

**问题：**尝试使用命令连接到 CodeBuild 沙盒环境时`ssh codebuild-sandbox-ssh=<sandbox-arn>`，可能会遇到如下`InvalidInputException`错误：

```
An error occurred (InvalidInputException) when calling the StartSandboxConnection 
operation: Failed to start SSM session for {sandbox-arn}
User: arn:aws:sts::<account-ID>:assumed-role/<service-role-name>/AWSCodeBuild-<UUID> 
is not authorized to perform: ssm:StartSession on resource.
```

```
An error occurred (InvalidInputException) when calling the StartSandboxConnection 
operation: Failed to start SSM session for 
sandbox <sandbox-arn>: codebuild:<UUID> is not connected.
```

**可能的原因：**
+ 缺少 Amazon EC2 Systems Manager 代理：构建映像未正确地安装或配置 SSM 代理。
+ 权限不足： CodeBuild 项目服务角色缺少所需的 SSM 权限。

**建议的解决方案：**如果您针对构建使用自定义映像，请执行以下操作。

1. 安装 SSM 代理 有关更多信息，请参阅**中的[在适用于 Linux 的 Amazon EC2 实例上手动安装和卸载 SSM 代理](https://docs.aws.amazon.com/systems-manager/latest/userguide/manually-install-ssm-agent-linux.html)。SSM 代理必须是 `3.0.1295.0` 或更高版本。

1. 将文件 [ https://github.com/aws/aws-codebuild-docker-images/blob/master/ubuntu/standard/7.0/amazon-ssm-agent.js](https://github.com/aws/aws-codebuild-docker-images/blob/master/ubuntu/standard/7.0/amazon-ssm-agent.json) on 复制到图像中的目录中。`/etc/amazon/ssm/`这将在 SSM 代理中启用**容器模式**。

1. 确保 CodeBuild 项目的服务角色具有以下权限，然后重新启动沙盒环境：

   ```
   {
      "Effect": "Allow",
         "Action": [
            "ssmmessages:CreateControlChannel",
            "ssmmessages:CreateDataChannel",
            "ssmmessages:OpenControlChannel",
            "ssmmessages:OpenDataChannel"
         ],
         "Resource": "*"
    },
    {
       "Effect": "Allow",
       "Action": [
          "ssm:StartSession"
        ],
        "Resource": [
           "arn:aws:codebuild:region:account-id:build/*",
           "arn:aws:ssm:region::document/AWS-StartSSHSession"
        ]
    }
   ```

## 错误：SSH 进入 CodeBuild 沙盒环境时出现 “找不到凭证”
<a name="sandbox-troubleshooting.credentials"></a>

**问题：**尝试使用命令连接到 CodeBuild 沙盒环境时`ssh codebuild-sandbox-ssh=<sandbox-arn>`，可能会遇到以下凭据错误：

```
Unable to locate credentials. You can configure credentials by running 
"aws configure".
```

**可能的原因：**您的本地环境中未正确配置 AWS 凭证。

**推荐的解决方案：**按照官方文档[配置您的 AWS CLI 证书： AWS CLI在*版本 2 的AWS 命令行界面用户指南*中配置设置](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)。

## `StartSandboxConnection``AccessDeniedException`SSH 进入 CodeBuild 沙盒环境时出错
<a name="sandbox-troubleshooting.access-denied"></a>

**问题：**尝试使用命令连接到 CodeBuild 沙盒环境时`ssh codebuild-sandbox-ssh=<sandbox-arn>`，可能会遇到以下权限错误：

```
An error occurred (AccessDeniedException) when calling the StartSandboxConnection 
operation: 
User: arn:aws:sts::account-id:assumed-role/role-name
is not authorized to perform: codebuild:StartSandboxConnection on resource: 
sandbox-arn
because no identity-based policy allows the codebuild:StartSandboxConnection action
```

**可能的原因：**您的 AWS 凭证缺少执行此操作所需的 CodeBuild 权限。

**推荐的解决方案：**确保与您的 AWS CLI 证书关联的 IAM 用户或角色具有以下权限：

```
{
    "Effect": "Allow",
    "Action": [
       "codebuild:StartSandboxConnection"
     ],
     "Resource": [
        "arn:aws:codebuild:region:account-id:sandbox/*"
     ]
}
```

## SSH 进入 CodeBuild 沙盒环境时出现错误：“ssh：无法解析主机名”
<a name="sandbox-troubleshooting.hostname"></a>

**问题：**尝试使用命令连接到 CodeBuild 沙盒环境时`ssh codebuild-sandbox-ssh=<sandbox-arn>`，遇到以下主机名解析错误：

```
ssh: Could not resolve hostname
```

**可能的原因：**此错误通常发生在本地环境中未正确执行所需的 CodeBuild 沙箱连接脚本时。

**建议的解决方案：**

1. 下载 CodeBuild 沙箱连接脚本。

1. 在终端中执行脚本以建立必要的 SSH 配置。

1. 重试与沙盒环境的 SSH 连接。