简体   繁体   English

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

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

There seem to be lot of discussion around this topic however nothing precisely for my situation and hasn't resolved it for me so far. 关于这个话题似乎有很多讨论,但对我的情况来说并不是很正确,到目前为止还没有为我解决。

I have my code placed in aws codecommit. 我把我的代码放在了aws codecommit中。

I have created an AMI for one of my running Ubuntu instance in AWS and created a launch configuration using this AMI along with an auto scaling group. 我为AWS中运行的一个Ubuntu实例创建了一个AMI,并使用此AMI和一个自动缩放组创建了一个启动配置。

I want to base/modify my launch config AMI every month or so to ensure the AMI itself has recent updated code and so newly launched instances (thru auto scaling) can just pull latest changes from codecommit repo on launch - resulting in reduced launch time. 我希望每个月左右基础/修改我的启动配置AMI以确保AMI本身具有最近更新的代码,因此新启动的实例(通过自动扩展)可以在启动时从codecommit repo中提取最新更改 - 从而缩短启动时间。

To achieve this, I placed below code in User data (cloud-init) script and selected a IAM role that has full permissions over all EC2 and codecommit as well as IAM:Passrole permission. 为实现这一目标,我将下面的代码放在用户数据(cloud-init)脚本中,并选择了一个对所有EC2和codecommit以及IAM:Passrole权限具有完全权限的IAM角色。 However on launch, the script always throws error and does not pull changes (I intentionally kept a file in repo to test) 但是在启动时,脚本总是抛出错误并且不会进行更改(我故意将文件保存在repo中以进行测试)

Option 1 选项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

It throws below error 它抛出错误

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

Option 2 - 选项2 -

Tried this option as well with SSH (although haven't tried any further fixes for this) 使用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

Got a different error - 得到了不同的错误 -

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.

Can someone please hep me understand where I am going wrong? 有人可以帮我理解我哪里错了吗?

Thanks. 谢谢。

In Option 1, it looks like the home directory wasn't created yet. 在选项1中,看起来主目录尚未创建。 When you are setting the global git config, it will go into the home directory's .gitconfig file. 当您设置全局git配置时,它将进入主目录的.gitconfig文件。 Though the option doesn't need to be global, eg you can switch the order of the lines to: 虽然该选项不需要是全局的,例如,您可以将行的顺序切换为:

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

This is provided that you have set up EC2 instance roles correctly and that your AWS CLI is able to get the EC2 instance role credentials from EC2 metadata to call AWS APIs. 这是因为您已正确设置EC2实例角色,并且您的AWS CLI能够从EC2元数据获取EC2实例角色凭证以调用AWS API。

Though its unclear from the output whether the AWS CLI is installed. 虽然从输出中不清楚AWS CLI是否已安装。 The CLI needs to be installed for the git config lines you've posted to work because its going to call "aws codecommit credential-helper" to get a temporary username and password based on the instance role credentials. 需要为您发布的git配置行安装CLI,因为它将调用“aws codecommit credential-helper”来获取基于实例角色凭据的临时用户名和密码。

In Option 2, you do not need to use the credential helper at all. 在选项2中,您根本不需要使用凭证帮助程序。 I am sorry if that was not clear in the documentation. 如果在文档中不清楚,我很抱歉。 You do, however, need to upload a public key to IAM (instructions here: http://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-unixes.html#setting-up-ssh-unixes-keys ) 但是,您需要将公钥上传到IAM(此处的说明: http//docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-unixes.html#setting-up-ssh -unixes-keys

You will also need to figure out a way to distribute your public and private key pair to the EC2 instances that you are trying to scale up, this can be quite troublesome. 您还需要找到一种方法将公钥和私钥对分发到您尝试扩展的EC2实例,这可能非常麻烦。

You can also generate static credentials for CodeCommit ( http://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-gc.html#setting-up-gc-iam ) and put them on your EC2 instance in something like a .netrc file. 您还可以为CodeCommit生成静态凭据( http://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-gc.html#setting-up-gc-iam )并将它们放在EC2实例上像.netrc文件。

IMO Option 1 seems the most secure since you don't have to deal with passing secrets around. IMO选项1似乎最安全,因为您不必处理传递秘密。

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

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