2

I would like to share a GitHub project ssh key pair with all new instances that I create so that it's possible to git clone and launch the program from the user data file without having to ssh in the instance.

Quite easy to do on GCP but not quite sure how to do any of that in AWS ec2 instances.

Edit: In GCP I would simply use the "Secret manager" which is shared between instances.

3
  • The only option I've found at the moment is to create an AMI that already contains the keys and repo and just add a git pull in the user data file. Doesn't sound like it's a good practice. Commented Feb 18, 2021 at 23:07
  • It might be useful to describe in general terms how you'd approach this in GCP, in case there turns out to be a similar mechanism in AWS that you just haven't found yet. Commented Feb 18, 2021 at 23:10
  • @IMSoP good point, I edited the question Commented Feb 18, 2021 at 23:17

2 Answers 2

6

Since you mention that you'd use Secret Manager in a Google Cloud, it seems reasonable to suggest the AWS Secrets Manager service.

Set your private key as a Secret, and grant access to it with an IAM role attached to the EC2 instance. Then install the AWS CLI package before building the AMI, and you can use it to fetch the secret on first boot with a User Data script.

Sign up to request clarification or add additional context in comments.

Comments

3

Because I find the AWS secret manager hard to use and expensive compared to GCP here's the solution I ended up using.

this is my user data file that is passed to the instance on creation.

sudo mkdir ~/.ssh
sudo touch ~/.ssh/id_rsa
sudo echo "-----BEGIN OPENSSH PRIVATE KEY-----
My GitHub private key" >> ~/.ssh/id_rsa
sudo chmod 700 ~/.ssh/
sudo chmod 600 ~/.ssh/id_rsa
git clone https://wwww.github.com/your-repo 
# other commands goes here

Note that it will add this to the root user.

not the cleanest solution but it works well

edit: sudo shouldn't be required because it all runs as root

1 Comment

The rather serious disadvantage of this is that the private key is visible in plain text to anyone with access to the EC2 console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.