Skip to content

Automatic compress private CodeCommit repository and push to S3 bucket via Lambda function

Notifications You must be signed in to change notification settings

0xlen/aws-lambda-python-codecommit-s3-deliver

Repository files navigation

aws-lambda-python-codecommit-s3-deliver

This lambda package can automatic pack the CodeCommit repository to S3 bucket.

Overview

Quick start

  1. Download the aws-lambda-python-codecommit-s3-deliver package
git clone https://github.com/0xlen/aws-lambda-python-codecommit-s3-deliver
cd aws-lambda-python-codecommit-s3-deliver/
zip -r aws-lambda-python-codecommit-s3-deliver.zip .
  1. Go to the Lambda console and create a Lambda function

  2. Add an trigger by clicking the Add trigger button as the following example

Add trigger

Next, set up your

  • Repository name
  • Trigger name
  • Events: Push to existing branch
  • Branch names: master (Or you may want to use other branch)

Add trigger detail

  1. In the tab Code, select Upload a .ZIP file and upload the aws-lambda-python-codecommit-s3-deliver package.

Application

  1. In the Environment variables, please set up your repository, branch and S3 bucket information
Name Type Description
BRANCH string(required) The pulling branch the lambda function is triggered.
S3_BUCKET string(required) Specify the S3 bucket will put the compressed repository file.
FILE_NAME string(required) The compressed repository file name.
  1. In the Configuration tab, please use the following settings.
  • Runtime: Python 2.7
  • Handler: app.lambda_handler

Configuration

  1. In the Advanced settings. If your repository is need to pull for a long while, please increase your Timeout. In my use case, my Timeout is 1 min.

Timeout

  1. In the Role, select Create a new IAM Role from template
  2. Save the lambda function. Go to the IAM Role Console
  3. Please select the Lambda IAM Role created before.

IAM

  1. Add the inline policy below

IAM Policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1501197956000",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::YOUR_BUCKET",
                "arn:aws:s3:::YOUR_BUCKET/*"
            ]
        },
        {
            "Sid": "Stmt1502993499000",
            "Effect": "Allow",
            "Action": [
                "codecommit:GetBlob",
                "codecommit:GetBranch",
                "codecommit:GetCommit",
                "codecommit:GetRepository",
                "codecommit:GetRepositoryTriggers",
                "codecommit:GitPull",
                "codecommit:ListBranches",
                "codecommit:TestRepositoryTriggers"
            ],
            "Resource": [
                "arn:aws:codecommit:ap-northeast-1:YOUR_ACCOUNT_ID:YOUR_CODECOMMIT_REPOSITORY"
            ]
        }
    ]
}

Here is what it looks like, click Apply policy

IAM Policy

Test

Now this lambda function would be triggered when the CodeCommit is having the Push event. You can check the .zip file in your S3 bucket after any new push event.

git push -u origin master

You will see the compressed repository is automatically generated by the lambda function.

Result

About

Automatic compress private CodeCommit repository and push to S3 bucket via Lambda function

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published