EC2 用户数据代码提交克隆失败

EC2 userdata codecommit clone fails

我正在从用户数据启动一个 ec2 实例,我需要使用我的 ansible 剧本克隆一个 repo,但它无法克隆。请参阅下面的详细信息。谁能帮我解决这个问题。当我在 bootstrap 之后通过 ssh 连接到实例时,克隆会工作,但在 bootstrapping.

时却不会
#!/usr/bin/env bash
set -x

exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1

cd /home/ec2-user

mkdir -p .ssh
ssh-keygen -b 2048 -t rsa -f /home/ec2-user/.ssh/codecommit -q -N ""

KEY_ID=`aws iam  upload-ssh-public-key --user-name ${user_id} --ssh-public-key-body "$(cat /home/ec2-user/.ssh/codecommit.pub)" \
  --query 'SSHPublicKey.SSHPublicKeyId' --output text`

echo -e "
Host git-codecommit.*.amazonaws.com
  User $KEY_ID
  IdentityFile /home/ec2-user/.ssh/codecommit
" >> /home/ec2-user/.ssh/config

ssh-keyscan -t rsa git-codecommit.us-east-2.amazonaws.com >> /home/ec2-user/.ssh/known_hosts

  sudo chown -R ec2-user:ec2-user /home/ec2-user/.ssh
sudo chmod 700 /home/ec2-user/.ssh
sudo chmod 644 /home/ec2-user/.ssh/*
sudo chmod 600 /home/ec2-user/.ssh/codecommit*

eval "$(ssh-agent -s)"

export GIT_SSH_COMMAND="ssh -v -F /home/ec2-user/.ssh/config -o StrictHostKeyChecking=no"
export GIT_TRACE_PACKET=true
export GIT_TRACE=2
export GIT_CURL_VERBOSE=1

**sleep 60s**
git clone ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/ansible

在 git 克隆命令之前添加 60 秒的睡眠时间就可以了。似乎 SSH 密钥上传需要一些时间才能生效。

sleep 60s
git clone ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/ansible

for i in {1..30}; do
  git clone ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/ansible      
  [ $? == 0 ] && break || sleep 2s; echo "keep trying ..."
done