AWS在EC2实例启动时从codecommit中提取最新代码

时间:2017-09-11 20:53:34

标签: git amazon-web-services amazon-ec2 aws-codecommit

关于这个话题似乎有很多讨论,但对我的情况来说并不是很正确,到目前为止还没有为我解决。

我的代码放在aws codecommit中。

我为AWS中运行的一个Ubuntu实例创建了一个AMI,并使用此AMI和一个自动缩放组创建了一个启动配置。

我希望每个月左右基础/修改我的启动配置AMI以确保AMI本身具有最近更新的代码,因此新启动的实例(通过自动扩展)可以在启动时从codecommit repo中提取最新更改 - 从而减少发射时间。

为实现这一目标,我将下面的代码放在用户数据(cloud-init)脚本中,并选择了一个对所有EC2和codecommit以及IAM:Passrole权限具有完全权限的IAM角色。但是在启动时,脚本总是抛出错误并且不会进行更改(我故意将文件保存在repo中进行测试)

选项1

#!/bin/bash
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
cd /path/to/my/folder/
git remote set-url origin https://git-codecommit.ap-southeast-2.amazonaws.com/v1/repos/reponame
git pull origin master

抛出错误

Error
fatal: $HOME not set
fatal: $HOME not set
fatal: Not a git repository (or any of the parent directories): .git
fatal: could not read Username for 'https://git-codecommit.ap-southeast-2.amazonaws.com': No such device or address

选项2 -

使用SSH尝试此选项(虽然尚未尝试任何进一步的修复)

#!/bin/bash
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
cd /path/to/my/folder/
git remote set-url origin ssh://git-codecommit.ap-southeast-2.amazonaws.com/v1/repos/reponame
git pull origin master

得到了其他错误 -

Errpr: 
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

有人可以帮我理解我哪里出错了吗?

感谢。

1 个答案:

答案 0 :(得分:2)

在选项1中,看起来主目录尚未创建。当您设置全局git配置时,它将进入主目录的.gitconfig文件。虽然该选项不需要是全球性的,例如您可以将行的顺序切换为:

cd /path/to/my/folder/ git config credential.helper '!aws codecommit credential-helper $@' git config credential.UseHttpPath true

前提是您已正确设置EC2实例角色,并且您的AWS CLI能够从EC2元数据中获取EC2实例角色凭证以调用AWS API。

虽然从输出中不清楚是否安装了AWS CLI。需要为您已发布的git配置行安装CLI,因为它将调用" aws codecommit credential-helper"根据实例角色凭据获取临时用户名和密码。

在选项2中,您根本不需要使用凭据帮助程序。如果在文档中不清楚,我很抱歉。但是,您需要将公钥上传到IAM(此处的说明:http://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-unixes.html#setting-up-ssh-unixes-keys

您还需要找到一种方法将公钥和私钥对分发到您尝试扩展的EC2实例,这可能非常麻烦。

您还可以为CodeCommit(http://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-gc.html#setting-up-gc-iam)生成静态凭据,并将它们放在EC2实例上,例如.netrc文件。

IMO选项1似乎最安全,因为你不必处理传递秘密。

相关问题