I need to clone a private git repo within aws lambda (lambda runtime is python). After doing some research, I know that the I need to clone the repo in /tmp directory because it is the only accessible directory in lambda. In addition, I need to use a private key to ssh to github.
Basically, my code looks like:
import git # lambda-git
import os
private_key = """-----BEGIN OPENSSH PRIVATE KEY-----
...
-----END OPENSSH PRIVATE KEY-----
"""
with open("/tmp/id_rsa", "w") as id_rsa:
id_rsa.write(private_key)
os.chmod("/tmp/id_rsa", 600)
os.environ["GIT_SSH_COMMAND"] = 'ssh -i /tmp/id_rsa -o StrictHostKeyChecking=no'
git.exec_command("clone", "git@domain:repo_name.git", "/tmp")
However, after uploading the zipped folder into lambda, I got the following error
Could not create directory \\'/home/sbx_user1051/.ssh\\'.
Failed to add the host to the list of known hosts (/home/sbx_user1051/.ssh/known_hosts).
Load key \"/tmp/id_rsa\": Permission denied
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
What am I missing? Or is it even possible to clone a private repo within aws lambda? Any help is appreciated.
PS. I know that I can use a git token and clone the repo over https. But I cannot modify the repo url and have to use git@domain:repo_name.git as it is.