We like to move it move it - Serverless Lambda transport between development stages



You want to ship the very same code you have tested from test stage to production stage. But most of the time a CICD pipeline re-creates the code to deploy to production. So you do not deploy the tested code! With the AWS Lambda transport tool you can move the very same code you have tested to production or between stages in different accounts.

Why should I do this, my development pipeline works fine?

In 95% of the cases, you may be right. But if you want to be 100% sure, you should not re-create the code, but use the same binary package aka zip for the lambda. If you already use a build to deploy it to dev and after successful test deploy the very same binary package to the next development stage, you do not need the transport tool. But if you create the binary from a pipeline, you may be shipping untested code.

Re-Creation deployment

This is how deployment with a deployment pipeline works. The code is re-created and deployed to the next stage. cicd

Integration test and manual test are performed in the test account with the test build.

Usually this approach works. But the following errors could occur:

  • The code in the repository is changed between the test and production deployment.
  • Deployment to test was done with different git commit in the repository than the deployment to production.
  • Libraries which are downloaded during the build process are updated between the test and production deployment.
    • This can occur if you not have pinned the version of the libraries in the build process.
    • With open source libraries it is not guaranteed that the same version is downloaded during the build process.

So to be really sure, that the tested code is deployed to production you need to deploy the build artifact from the test stage to the production stage.

Lambda stores its application code as a zip file in an S3 bucket. You an download the application zip artifact from the test stage and upload it to the production stage. The Lambda transport tool copies the zip file from the test stage to the production stage.

How Transport deployment works

When you transport the zipped artifact, you are 100% sure that the tested application is deployed to production.

You have to make sure that changes to the Lambda resource (not the application artifact) like environment variables, memory, timeout, etc. are deployed before changing the code.

transport

  1. Download the zip file from the test stage
getFunctionOutput, err := client.GetFunction(context.TODO(), &lambda.GetFunctionInput{
		FunctionName: &functionName,
	})

The response from the Lambda function call GetFunction contains a Code attribute.

Code
The deployment package of the function or version.

Type: FunctionCodeLocation object

In the Location attribute you find a pre-signed URL that you can use to download the deployment package.

Using the transport tool

You define the stage accounts, the name of the lambda function and the regions in a configuration file. Then you call the transport tool with the stage name.

transport -h                                                                                                             ─╯
Usage of ./transport:
  -help
    	Display help message
  -stage string
    	define the application stage
  -verbose
    	Display more messages

See details of configuration in github page.

  1. Create Lambda Resource in both accounts
  2. Define access policies in both accounts
  3. Create code packages in source account
  4. Test Lambda function in source account
  5. Transport it to target account
transport --stage dev
time=2024-08-25T14:11:22.693+02:00 level=INFO msg="Downloaded the ZIP file from the source lambda function" filename=downloads/transfertest.zip
time=2024-08-25T14:11:22.882+02:00 level=INFO msg="Uploaded the ZIP file to the target lambda function" functionName=transfertest revisionId=cdff1079-5f14-47bc-9a64-acd90dc9196d

overview

Installation

The cli tool is a single binary. Get the right version for your OS from the release page.

Other Use Cases

Testing orchestrated Lambda functions

As I discussed in The cuckoo egg testing lambda you can use replacing dependent Lambda functions with a binary.

In an distributed environment you want to test Function code independently from each other. So you could replace the functions not-in-test with mock code, which gives a defined response. Mocking Lambdas

Using the downloaded packages

The lambda code from the source account is locally stored in the downloads directory. You may use this zip for analysis or backup.

l downloads
total 8
-rw-r--r--@ 1 gglawe  staff  269 25 Aug 14:11 transfertest.zip

Show all entries in the zip package:

unzip -t downloads/transfertest.zip
Archive:  downloads/transfertest.zip
    testing: index.mjs                OK
No errors detected in compressed data of downloads/transfertest.zip.

Summary

Re-Creating Lambda function for stage deployment can result in different packages being deployed. A fast and clean way to avoid that is using a Lambda transport.

The Lambda transport CLI tool helps you do the job.

See also

What’s next?

If you need AWS training, developers and consulting for your next cloud project, GenAI or not, don’t hesitate to contact us, tecRacer.

Want to learn GO on AWS? GO here

Enjoy building!

Similar Posts You Might Enjoy

Stop LLM/GenAI hallucination fast: Serverless Kendra RAG with GO

RAG is a way to approach the “hallucination” problem with LLM: A contextual reference increases the accuracy of the answers. Do you want to use RAG (Retrieval Augmented Generation) in production? The Python langchain library may be too slow for your production services. So what about serverless RAG in fast GO Lambda? - by Gernot Glawe

Custom runtime on Amazon Linux 2 - GO outperforms Node (3x) and Python (2x) with AWS Lambda Cold-start time

Lambda GO runtime is deprecated until the end of 2023. The new default custom Amazon Linux 2 runtime really speeds things up for GO. Now the cold-start is 2x faster than Python and 3x faster than node! - by Gernot Glawe

Prepopulate Lambda Console Testevents without dirty manual work using Terraform

You like Lambda testevents? Great! But with “automate everything”, manual console clicks are considered dirty! Keep your hand clean by automating the creation of Lambda test events. So you can give your team, and yourself prepopulated test events. This example shows you the terraform code - because this is the fastest way. With a little effort, you can translate it to CloudFormation or AWS-CDK! - by Gernot Glawe