/ #Lambda #AWS 

Local to Lambda via Aws CLI

Problem

Handing source control of Lambda functions from your local PC to AWS Lambda.
I’ve overwritten code in one Lambda function thinking I was in another with the inline editor that is built within the AWS Lambda platform.

This was totally my fault, but I would put a practice in place that would stop this from happening again.

Solution

Implement source control via BitBucket & Sourcetree. Develop locally and push coding changes up to S3 bucket (with versioning enabled) and update Lambda function code via CLI.

Setup

My Setup:

  • S3 bucket to only store Lambda function files
  • AWS CLI on local PC
  • BitBucket or similar I use BitBucket with Sourcetree
  • Using NodeJS to handle the code within the Lambda functions
  • S3 Bucket and Lambda must be in the same region
  • AWS Account with CLI access

If you have function files already in your S3 bucket, use sync to copy those down to your local repo http://docs.aws.amazon.com/cli/latest/reference/s3/sync.html

cd c:\localrepo\lambda\
aws s3 sync s3://s3LambdaFunctionBucket/ .

Total of 3 commands via Windows Powershell.

Zip the index.js file Copy Zip to S3 bucket Update Lambda Function from Zip

Compress-Archive -Path c:\localrepo\lambda\functionName\index.js -DestinationPath c:\localrepo\lambda\functionName.zip -Force
aws s3 cp functionName.zip s3://s3LambdaFunctionBucket/functionName.zip
aws lambda update-function-code --function-name  arn:aws:lambda:{yourRegion}:{yourID}:function:functionName --zip-file fileb://C:\localrepo\lambda\functionName.zip

https://docs.aws.amazon.com/cli/latest/reference/lambda/create-function.html

Zipping the Lambda File:

Compress-Archive -Path c:\localrepo\lambda\functionName\index.js -DestinationPath c:\localrepo\lambda\functionName.zip -Force
-Path c:\localrepo\lambda\functionName\index.js

This is the location of your local repo and Lambda function index.js

-DestinationPath c:\localrepo\lambda\functionName.zip

This is the location of where the zip file will be placed. Just keep this in mind for the next steps.

-Force 

This allows the zip file to be overwritten if it already exists. Function Name

Pushing the zip file to s3 bucket:

aws s3 cp functionName.zip s3://s3LambdaFunctionBucket/functionName.zip

Updating the Lambda function (method 1). This will update the Lambda function from local copy.

aws lambda update-function-code --function-name  arn:aws:lambda:{yourRegion}:{yourID}:function:functionName --zip-file fileb://C:\localrepo\lambda\functionName.zip

Updating the Lambda function (method 2). This will update the Lambda function from S3 Bucket copy.

aws lambda update-function-code --function-name  arn:aws:lambda:{yourRegion}:{yourID}:function:functionName --s3-bucket s3LambdaFunctionBucket --s3-key functionName.zip

After this, you will have three copies of your Lambda code and a great platform for version control. In the event that you need to roll back to a version of code, you will be all set to push those changes out.

  1. Bit Bucket Remote Repo
  2. Local PC Repo
  3. S3 Bucket for Lambda Functions
Author

CDD

Look, I made a thing.