Add Amplify deploy key to multiple github repositories?

1

I have a project hosted in a private github repository that I'm attempting to deploy using the Amplify console. When I set up the app, I authorize AWS to have access to my github account and a random SSH key gets added to my project's repo as a deploy key. My build then fails because I have a dependency in my package.json which points to another private github repo in my account. I can't find any way to get access to the public key to add it to this dependency account.

I've tried adding various commands to my build configuration in an attempt to dump out the key data, but it appears that key gets loaded into an ssh-agent instance and then is deleted from disk before my build starts. I also attempted setting up another Amplify app pointed at my other repository, but the deploy key appears to be generated per app and the key has a different fingerprint.

speshak
asked 5 years ago1866 views
10 Answers
1

I had to modify the above solutions to get around a Host key verification failed error. End to end, my steps:

  1. Create a custom deploy key for the private repo in github
    • generate the key
    ssh-keygen -f deploy_key -N ""
  2. Encode the deploy key as a base64 encoded env variable for amplitude
    cat deploy_key | base64 | tr -d \\n 
  3. Modify the amplify.yml file to make use of the deploy key
    • there's 2 key steps
      • adding deploy key to ssh-agent
        • WARNING: this implementation will print the $DEPLOY_KEY to stdout
      • disabling StrictHostKeyChecking
        • NOTE: amplify does not have a $HOME/.ssh folder by default so you'll need to create one as part of the deployment process
    • relevant excerpt below
    - ...
    - eval "$(ssh-agent -s)"
    - ssh-add <(echo "$DEPLOY_KEY" | base64 -d)
    
    - echo "disable strict host key check"
    - mkdir ~/.ssh
    - touch ~/.ssh/config
    
    - 'echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - ...
    • full build file here

I also did a more detailed writeup as well as alternatives and gotchas which you can read about here

answered 2 years ago
  • Thanks! this is what finally did it for me. For some reason I do have $HOME/.ssh directory so I had to drop the mkdir and touch lines (maybe the linux image I'm using), but that did the trick! Cheers!

0

Hi speshak,

Thanks for using AWS Amplify Console! You are correct that the deploy keys are generated on a per-app basis. However, you may be able to work around this by creating your own key pair.

Add the private key as an environment variable in the Amplify app. Add the public key to the repository you would like to clone. You could then add the key to the ssh-agent on the build instance during build and git clone the second repository.

Please let us know if that works!

Thanks,
Dan

AWS
answered 5 years ago
0

I was able to make that work, though it is a bit gross (albeit it's better than using a GitHub personal token in the package.json, which was my work around before)

It would be great if the public key generated for the Amplify app could be accessed in some way to avoid needing to do all of this. Please consider this a feature request for the Amplify console.

speshak
answered 5 years ago
0

Thanks for your feedback!

answered 5 years ago
0

Hello,

Whenever I try adding the private key using "ssh-add" it asks for the passphrase and does not continue the build. The key does not have a passphrase. Any ideas?

Thanks

answered 5 years ago
0

Im having this issue as well. I'm trying ssh-add in the commands section of the build settings, and even though the key doesn't have a password, it asks for it and the build fails. Any suggestions? If someone has gotten this to work can you share the relevant bit of amplify.yaml?

Thanks in advance

answered 4 years ago
0

so turns out I needed to base64 encode the key as per @certik's answer on the following thread:https://gitlab.com/gitlab-examples/ssh-private-key/issues/1

  1. create the keypair without a password:
ssh-keygen -f deploy_key -N ""
  1. encode it and copy the output into an Environment Variable in the Amplify Console (eg DEPLOY_KEY)
cat deploy_key | base64 | tr -d \\n
  1. add the contents of deploy_key.pub to the access keys of the private repo you want to access

  2. then in amplify.yml

commands:
        - eval "$(ssh-agent -s)"
        - ssh-add <(echo "$DEPLOY_KEY" | base64 -d)
answered 4 years ago
0

Is this answer still valid? My build just freezes when it runs eval "$(ssh-agent -s)"

answered 3 years ago
0

Is this answer still valid? My build just freezes when it runs eval "$(ssh-agent -s)"

Yeah I had the same problem. Could resolve by doing the following:

  • Remove newline characters from base64 encoded key. When I copied the key over from my pc to the amplify console variables the new lines were always readded. I didn't really saw that for a while due to the strange formatting in the amplify console. I then removed the newlines manually in my editor and copied over again. Then it worked.
  • I also used this command instead:

echo "$DEPLOY_KEY" | base64 -d | ssh-add - > /dev/null

Just make sure you don't have any newline characters in your base64 string. I encoded with this command:

cat deploy_key | base64

(And afterwards removed the newlines as described above)

Hope this helps somehow.

Edited by: marcuszy on Apr 22, 2021 12:34 AM

Edited by: marcuszy on Apr 22, 2021 12:34 AM

Edited by: marcuszy on Apr 22, 2021 12:36 AM

answered 3 years ago
0

Just a note because it took me a while to figure out. The current environment of Amplify is not the latest, and it uses a less than 7.6 OpenSSH (OpenSSH_7.4p1 as the day of writing this). If you have a newer NPM, then you have errors installing your private repos as modules (https://github.com/npm/git/issues/31). You must run npm install this way:

 - GIT_SSH_COMMAND="ssh" npm install

I believe this forces NPM to rely on SSH not in a newer mechanism that will fail in older environments.

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions