简体   繁体   中英

How do I install Git using AWS Lambda?

I have code in Code commit repository, I am writing a lambda function to build the code for every check-in (event) to the code commit repo.

I am not able to install git and hence cloning the repository is not happening.

How do I go about it.

As the others have mentioned installing git on lambda is either really hard or totally impossible. I did a session at reinvent about lambda and step functions and spent some time trying to get it to work. I gave up. An alternative I found more recently is to use one of the git libraries for node or python or whatever language you are using. But this is still a bad idea and will cause pain when you need to maintain the function.

It turns out another alternative is here but looks messy: Running 'git' in AWS lambda

Using CodeBuild is a much better idea. Its a build system on AWS that will do anything you want it to. Uses Docker and you can supply your own docker image. I did a multi-stage build to create the smallest possible GatsbyJS image. Now the downside is that it takes 30 seconds to provision (it took closer to 2 minutes without the custom image). Plus CodeBuild has 100 free minutes per month, every month for ever. If this is a personal scenario, this could cover you completely.

If you don't mind mixing the clouds, also look into Google's Cloud Container Builder. It says its for building docker images, but you can use it for anything you need. And the first 120 minutes EVERY day are free. So definitely a cool service to look into. There is nothing stopping you from mixing services from different clouds.

My session at reInvent used codebuild, lambda and a lot of other services to live blog the session using images from a raspberry pi and a service to recognize which slide was being shown. It worked beautifully. Do a google search for SRV335 and you should find it if you are interested in seeing codebuild with lambda in action

Expanding on @ephialtis47's answer: AWS Lambda's support containers, specifically the Amazon Linux 2 image.

Following theNode.js example the Dockerfile would look like:

FROM public.ecr.aws/lambda/nodejs:14

# Assumes your function is named "app.js", and there is a package.json file in the app directory 
COPY app.js package.json  ${LAMBDA_TASK_ROOT}/

# Install git
RUN yum -y install git

# Test git:  Get CPython's 5 most recent tags
RUN git ls-remote --tags https://github.com/python/cpython | tail -n 5 

# Install NPM dependencies for function
RUN npm install

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.handler" ] 

The rest of that example details deploying the container to AWS ECR and then creating a Lambda from the container.

Alternative

isomorphic-git is git implementation written in pure JS. This package can be deployed to a standard Lambda. Example index.js :

const path = require("path");
const git = require("isomorphic-git");
const http = require("isomorphic-git/http/node");
const fs = require("fs");

const clone = async () => {
    const dir = "/tmp/test-clone";
    const url = "YOUR REPOSITORY URL (MUST BE HTTP URL, SSH NOT SUPPORTED)"
    let results = await git.clone({
        fs,
        http,
        dir,
        url: url,
    });
}
exports.handler = async function (event, context) {
     await clone();
     return context.logStreamName;
};

An easy working solution is to add a layer to your lambda called git-lambda-layer This will add this layer to the default container and git will be available when your lambda runs.

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-lambda2:8

With the fairly recent addition of building Lambda Functions from a "Container Image" you can now build an instance of your runtime that also includes Git to accommodate what you are trying to do. I do this to build Lambda Layers that require dependencies from private repos.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM