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.
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.
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.
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. However on launch, the script always throws error and does not pull changes (I intentionally kept a file in repo to 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 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 -
Tried this option as well with SSH (although haven't tried any further fixes for 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 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.