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

时间:2018-08-10 20:25:01

标签: amazon-ec2 ssh aws-codecommit

我正在从userdata启动一个ec2实例,我需要用我的ansible剧本克隆一个repo,但是它无法克隆。请参阅下面的详细信息。谁能帮我解决这个问题。当我在引导后SSH到实例时,然后克隆可以工作,但在引导时却不能。

#!/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

1 个答案:

答案 0 :(得分:0)

在git clone命令完成之前增加60秒的睡眠时间。似乎SSH密钥上载需要一段时间才能生效。

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

OR

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