git - AWS Pull latest code from codecommit on EC2 Instance startup -
there seem lot of discussion around topic nothing precisely situation , hasn't resolved me far.
i have code placed in aws codecommit.
i have created ami 1 of running ubuntu instance in aws , created launch configuration using ami along auto scaling group.
i want base/modify launch config ami every month or ensure ami has recent updated code , newly launched instances (thru auto scaling) can pull latest changes codecommit repo on launch - resulting in reduced launch time.
to achieve this, placed below code in user data (cloud-init) script , selected iam role has full permissions on ec2 , codecommit iam:passrole permission. on launch, script throws error , not pull changes (i intentionally kept file in repo test)
option 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 git repository (or of parent directories): .git fatal: not read username 'https://git-codecommit.ap-southeast-2.amazonaws.com': no such device or address
option 2 -
tried option ssh (although haven't tried further fixes this)
#!/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 different error -
errpr: host key verification failed. fatal: not read remote repository. please make sure have correct access rights , repository exists.
can please hep me understand going wrong?
thanks.
in option 1, looks home directory wasn't created yet. when setting global git config, go home directory's .gitconfig file. though option doesn't need global, e.g. can switch order of lines to:
cd /path/to/my/folder/ git config credential.helper '!aws codecommit credential-helper $@' git config credential.usehttppath true
this provided have set ec2 instance roles correctly , aws cli able ec2 instance role credentials ec2 metadata call aws apis.
though unclear output whether aws cli installed. cli needs installed git config lines you've posted work because going call "aws codecommit credential-helper" temporary username , password based on instance role credentials.
in option 2, not need use credential helper @ all. sorry if not clear in documentation. do, however, need upload public key iam (instructions here: http://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-unixes.html#setting-up-ssh-unixes-keys)
you need figure out way distribute public , private key pair ec2 instances trying scale up, can quite troublesome.
you can generate static credentials codecommit (http://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-gc.html#setting-up-gc-iam) , put them on ec2 instance in .netrc file.
imo option 1 seems secure since don't have deal passing secrets around.
Comments
Post a Comment