3

I've been trying to use the gitpython package in aws lambda. I've used python2.7 environment. I bundled up gitpython using this along with my python code into a zip file and uploaded.

import json
import git

def lambda_function(event, context):
    repo="https://github.com/abc/xyz.git"
    git.Git().clone(repo)

It says

Cmd('git') not found due to: OSError('[Errno 2] No such file or directory')
  cmdline: git clone https://github.com/abc/xyz.git: GitCommandNotFound
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 13, in lambda_function
    git.Git().clone("https://github.com/abc/xyz.git")
  File "/var/task/git/cmd.py", line 425, in <lambda>
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
  File "/var/task/git/cmd.py", line 877, in _call_process
    return self.execute(call, **exec_kwargs)
  File "/var/task/git/cmd.py", line 602, in execute
    raise GitCommandNotFound(command, err)
GitCommandNotFound: Cmd('git') not found due to: OSError('[Errno 2] No such file or directory')
  cmdline: git clone https://github.com/abc/xyz.git

I think this error is caused because the lambda machine dosen't have git in it! How can I use this?

Arafat Nalkhande
  • 11,078
  • 9
  • 39
  • 63
Neeraj
  • 1,769
  • 3
  • 24
  • 41
  • I think, the answer will help to you http://stackoverflow.com/questions/10804744/oserror-errno-2-no-such-file-or-directory-on-gitpythonz –  May 04 '17 at 12:52
  • It says the instance lacks git. If so, how can we install git in aws lambda? – Neeraj May 04 '17 at 13:02
  • 1
    I'm pretty sure this is bundling issue and not related to git not being installed but you can validate that by launching AMI amzn-ami-hvm-2016.03.3.x86_64-gp2 which is what Lambda is using. If git is in that AMI then the problem is definitely not git, if it's not then you'll need to include git in your bundle or use pygit2. More into on the lambda execution environment here: http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html – alanwill May 07 '17 at 17:15
  • how can you clone private git repo using ssh in lambda? – Ashish Sharma Oct 13 '22 at 16:51

2 Answers2

7

There is a special lambda layer that brings in git to lambda functions. Check this and this reference. Basically,

Click on Layers and choose "Add a layer", and "Provide a layer version ARN" and enter the following ARN (replace us-east-1 with the region of your Lambda):

arn:aws:lambda:us-east-1:553035198032:layer:git:6

Shadi
  • 9,742
  • 4
  • 43
  • 65
1

I faced this issue and finally got a very easy soultion. All you have to do is just add a layer in your lambda function. No need to change any thing in the code.

You can add the lambda layer by providing arn something like this where region will be changed to your region and for version you can refer this link.

https://github.com/lambci/git-lambda-layer

arn:aws:lambda:region:553035198032:layer:git:version