繁体   English   中英

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

[英]AWS Pull latest code from codecommit on EC2 Instance startup

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

我把我的代码放在了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中,看起来主目录尚未创建。 当您设置全局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似乎最安全,因为您不必处理传递秘密。

暂无
暂无

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

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