简体   繁体   English

通过AWS EC2实例中的用户数据进行Git-pull

[英]Git-pull via userdata in AWS EC2 instances

I'm trying to pull a bitbucket repository on an ec2 instance during launch, using userdata. 我正在尝试使用userdata在启动期间在ec2实例上提取一个bitbucket存储库。 To do so, I've created a keypair and linked it to my bitbucket account. 为此,我创建了一个密钥对并将其链接到我的bitbucket帐户。 I've put the private key in an S3 bucket reachable by my instance through IAM Role. 我已经将私钥放在通过IAM角色可由我的实例访问的S3存储桶中。

Although this all works great when I do each step manually (login on an ec2 instance, download the key from s3, ssh-add the key, git clone my repo), it all goes to hell when I try to to it through userdata. 尽管当我手动执行每个步骤(在ec2实例上登录,从s3下载密钥,ssh-添加密钥,git克隆我的仓库)时,所有这些工作都很好,但是当我尝试通过userdata尝试时,一切都会陷入困境。

This is what I've tried so far : 到目前为止,这是我尝试过的:

aws s3 cp s3://bucket/private_key private_key
chmod 600 private_key
eval `ssh-agent -s`
ssh-add private_key 
mkdir -p .ssh
ssh-keyscan -t rsa bitbucket.org > .ssh/known_hosts
git clone git@bitbucket.org:user/project.git

Again, this all works perfectly when done manually. 同样,如果手动完成所有操作,则效果很好。 Yet, when done through userdata, the output would be : 但是,当通过userdata完成时,输出将是:

download: s3://bucket/private_key to ./private_key
Agent pid 1623
Identity added: private_key (id_rsa)
# bitbucket.org SSH-2.0-OpenSSH_5.3
Initialized empty Git repository in /project/.git/
Host key verification failed.
fatal: The remote end hung up unexpectedly

I've tried couple of things, but it's like the ssh used when adding the key isn't the same as the one used to pull the repo... Any input would be greatly appreciated ! 我已经尝试了几件事,但是这就像添加密钥时使用的ssh与用于拉出回购协议的ssh不同...任何输入将不胜感激!

What is your Linux distro? 您的Linux发行版是什么?

mkdir -p .ssh
ssh-keyscan -t rsa bitbucket.org > .ssh/known_hosts

You are not creating the .ssh directory in the home directory. 您不在主目录中创建.ssh目录。 Depending on your linux distribution, change it to /home/ec2-user/ or /home/ubuntu/ or /root/ etc., 根据您的Linux发行版,将其更改为/home/ec2-user//home/ubuntu//root/等,

Example : 范例

mkdir -p /home/ubuntu/.ssh
ssh-keyscan -t rsa bitbucket.org > /home/ubuntu/.ssh/known_hosts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM