Skip to content

Backup all repositories in our Organization to AWS Code Commit

Notifications You must be signed in to change notification settings

pa/serverless-repo-git-backup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serverless

This is a template for Serverless backup of all repositories from Organization/User to AWS Code Commit - Below is a brief explanation of what I have generated for you:

.
├── README.md                   <-- This instructions file
├── backup                      <-- Source code for a lambda function
│   ├── app.py                  <-- Lambda function code
│   ├── requirements.txt        <-- Lambda function code
├── template.yaml               <-- SAM Template
└── tests                       <-- Unit tests
    └── unit
        ├── __init__.py
        └── test_handler.py

Requirements

  • Add GitHub Token to SSM Parameter store with name

    /GitHub/Token/< Github Username or Organization Name>
    
    Example: /GitHub/Token/pramodh-ayyappan
  • AWS CLI already configured with Administrator permission

  • Python 3 installed

  • pip3 install aws-sam-cli

  • Set Up Lambda Layers

Setup process

SAM CLI is used to emulate Lambda uses our template.yaml to understand how to bootstrap this environment (runtime, where the source code is, etc.) - The Lambda Function will get triggered automatically on a daily basis:

...
 ...
 Events:
        Serverless:
          Properties:
            Method: post
            Path: /github
            RestApiId:
              Ref: ServerlessAPI
          Type: Api

Packaging and deployment

AWS Lambda Python runtime requires a flat folder with all dependencies including the application. SAM will use CodeUri property to know where to look up for both application and dependencies:

...
    ServerlessFunction:
    Properties:
      CodeUri: backup/
            ...

AWS Lambda Layers

  • Login into AWS Account
  • Go to this link and select a specific region and deploy.
  • Now copy generated AWS CLI Lambda Layer ARN and paste it in this file template.yaml at line 59.
  • For git binary we are going to use already existing layer arn:aws:lambda:us-east-1:553035198032:layer:git:10

SAM CLI commands

Building the project

AWS Lambda requires a flat folder with the application as well as its dependencies in deployment package. When you make changes to your source code or dependency manifest.

sam build

Create S3 bucket

aws s3 mb s3://BUCKET_NAME

Package Lambda function defined locally and upload to S3 as an artifact

sam package \
    --output-template-file packaged.yaml \
    --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME

Deploy SAM template as a CloudFormation stack

sam deploy \
    --template-file packaged.yaml \
    --stack-name Serverless \
    --capabilities CAPABILITY_IAM \
    --parameter-overrides NotificationEmail="<your email>" GitOrgOrUser="< Github User or Organisation>" GitTokenSSMParameterKey="<ssm parameter key for git access token>" VpcId="<vpc id>" VpcSubnetIds="<subnet id>"

Describe Output section of CloudFormation stack previously created

aws cloudformation describe-stacks \
    --stack-name Serverless-Pramodh \
    --query 'Stacks[].Outputs[?OutputKey==`ServerlessApi`]' \
    --output table

About

Backup all repositories in our Organization to AWS Code Commit

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages