How to fetch GitHub files data in AWS Lambda

Read-git-data-in-aws-lambda

GitHub is a leading version control tool that is used by developers all over the world. On the other hand, AWS lambda is also a first choice among enterprises for serverless computing. This blog post talks about how to connect AWS Lambda with GitHub to fetch source code through GitHub APIs.

Read-git-data-in-aws-lambda

For connecting AWS Lambda with the GitHub certain information is required that we will use later steps. Check the requirements below.

Prerequisites 

  • GitHub URL
  • GitHub Repo access token
  • GitHub branch name
  • Build a Node.JS layer for Octokit npm module

Steps to read GitHub Repo files in AWS lambda

  • Require Octokit from @octokit/core library, for GitHub enterprise we need to require enterpriseServer220Admin from @octokit/plugin-enterprise-server library
  • Instantiate Octokit class with GitHub token
  • Using async await make a Get request to the GitHub repo endpoint
  • Handle any error that can occur while calling the GitHub endpoint
  • If your repo is on GitHub enterprise then there is a little bit different way to call the API

Code Snippet to fetch file content from GitHub

const { Octokit } = require("@octokit/core");
const octokit = new Octokit({ auth: ENTER_GIT_TOKEN_HERE }); //add GitHub authentication token here

const fetchFileDataFromGit = async (repoName, folderPath, fileName, branchName) => {
    try {
        const { data } = await octokit.request('GET /repos/{owner}/{repo}/git/trees/{tree_sha}', {
            repo: repoName,
            tree_sha: `${branchName}:${folderPath}`, //Folder location as per GitHub Repo
            path: fileName,  //config.json'
            ref: branchName
        });

        //print the result here
        console.log(data);
    } catch (err) {
        throw new Error(err);
    }
}

// add your inputs to function like repository name, filePath relative to repo home, branch name
fetchFileDataFromGit('dummyRepo', 'config', 'config.json', 'develop');

    Code Snippet to fetch file content from GitHub Enterprise

    const { enterpriseServer220Admin } = require("@octokit/plugin-enterprise-server");
    const OctokitEnterprise220 = Octokit.plugin(enterpriseServer220Admin);
    const { baseUrl } = "https://github.acme-inc.com/api/v3",
    const octokit = new OctokitEnterprise220({ auth: GIT_TOKEN, baseUrl }); //add GitHub authentication token here
    
    const fetchFileDataFromGitEnterprise = async (repoName, folderPath, fileName, branchName) => {
        try {
            const { data } = await octokit.request('GET /repos/{owner}/{repo}/git/trees/{tree_sha}', {
                repo: repoName,
                tree_sha: `${branchName}:${folderPath}`, //Folder location as per GitHub Repo
                path: fileName,  //config.json'
                ref: branchName
            });
    
            //print the result here
            console.log(data);
        } catch (err){
            throw new Error(err);
        }
        
    }
    
    // add your inputs to function like repository name, filePath relative to repo home, branch name
    fetchFileDataFromGitEnterprise('dummyRepo', 'config', 'config.json', 'develop');

    Conclusion

    In some of the use cases, developers require to fetch configuration or JSON files directly from private GitHub repo using AWS lambda. Fetching it directly using GitHub APIs is best suited for this kind of use cases where we need only few files, this method eliminates the requirement to clone the whole Git repo and then access the files.

    0 Shares:
    Leave a Reply

    Your email address will not be published. Required fields are marked *

    You May Also Like