排查 AWS CodeBuild 沙盒 SSH 连接问题
使用本主题中的信息来协助您识别、诊断和解决 CodeBuild 沙盒 SSH 连接问题。
主题
通过 SSH 进入 CodeBuild 沙盒环境时出现 StartSandboxConnectionInvalidInputException 错误
问题:尝试使用命令 ssh codebuild-sandbox-ssh= 连接到 CodeBuild 沙盒环境时,可能会遇到如下 <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 权限。
建议的解决方案:如果您针对构建使用自定义映像,请执行以下操作。
-
安装 SSM 代理 有关更多信息,请参阅中的在适用于 Linux 的 Amazon EC2 实例上手动安装和卸载 SSM 代理。SSM 代理必须是
3.0.1295.0或更高版本。 -
将 https://github.com/aws/aws-codebuild-docker-images/blob/master/ubuntu/standard/7.0/amazon-ssm-agent.json
文件复制您的映像中的 /etc/amazon/ssm/目录中。这将在 SSM 代理中启用容器模式。 -
确保 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 沙盒环境时出现“Unable to locate credentials”
问题:尝试使用命令 ssh codebuild-sandbox-ssh= 连接到 CodeBuild 沙盒环境时,可能会遇到以下凭证错误:<sandbox-arn>
Unable to locate credentials. You can configure credentials by running "aws configure".
可能的原因:本地环境中未正确地配置 AWS 凭证。
建议的解决方案:按照官方文档配置您的 AWS CLI 凭证:《AWS 命令行界面版本 2 的用户指南》中的配置 AWS CLI 设置。
通过 SSH 进入 CodeBuild 沙盒环境时出现 StartSandboxConnectionAccessDeniedException 错误
问题:尝试使用命令 ssh codebuild-sandbox-ssh= 连接到 CodeBuild 沙盒环境时,可能会遇到以下权限错误:<sandbox-arn>
An error occurred (AccessDeniedException) when calling the StartSandboxConnection operation: User: arn:aws:sts::account-id:assumed-role/role-nameis not authorized to perform: codebuild:StartSandboxConnection on resource:sandbox-arnbecause 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: Could not resolve hostname”
问题:尝试使用命令 ssh codebuild-sandbox-ssh= 连接到 CodeBuild 沙盒环境时,遇到以下主机名解析错误:<sandbox-arn>
ssh: Could not resolve hostname
可能的原因:当本地环境中未正确地执行所需的 CodeBuild 沙盒连接脚本时,通常会发生此错误。
建议的解决方案。
-
下载 CodeBuild 沙盒连接脚本。
-
在终端中执行脚本以建立必要的 SSH 配置。
-
重试与沙盒环境的 SSH 连接。