AWS-BLOG3

Unlocking the Power of Custom Runtimes in AWS Amplify Lambda Functions

By James Tosswill

19 October, 2023

Share

AWS Lambda has been a game-changer in the serverless world, and AWS Amplify has made it even easier to deploy and manage such applications. But what if you’re itching to use a language or environment not directly supported by AWS Lambda? That’s where custom runtimes come into play. In this guide, we’ll walk through integrating a custom runtime into an AWS Amplify-managed Lambda function.

Custom Runtimes Demystified

At its core, AWS Lambda provides support for a set of popular programming languages. But the tech landscape is vast, and sometimes, specific projects demand unique environments. That’s where custom runtimes shine. They empower developers to define their own execution environment, opening the doors to virtually any programming language within AWS Lambda.

Getting Started

Before we dive in, ensure you’ve got the AWS Amplify CLI installed and have an Amplify project up and running. For this walkthrough, we’re assuming you’ve added a Lambda function with a typical runtime, say nodejs18.x.

Switching to Custom Runtime

Jump into Your Function’s Directory:
Open your Terminal and navigate to your function’s directory:

code-aws

Edit the CloudFormation Template:
Locate and open YOUR_FUNCTION_NAME-cloudformation-template.json. Within, find the Runtime property and modify its value to provided.al2. This signals AWS Lambda to anticipate a custom runtime.

Bootstrap — The Custom Runtime’s Backbone:
The bootstrap executable is the linchpin of a custom runtime. Think of it as the bridge between AWS Lambda's infrastructure and your unique runtime environment. But how do you go about creating this essential component?

1 — Understanding its Role:
At its core, the bootstrap executable is responsible for several tasks:

Creating the Bootstrap Executable:

  • Communicating with the Lambda Runtime API to fetch invocation events.
  • Invoking your function handler with the provided event.
  • Returning the function’s response back to the Lambda Runtime API.
  • Handling and reporting errors that might occur during the function’s execution.

2 — Creating the Bootstrap Executable:
Depending on your chosen language or environment, the process might vary. However, the general steps are:

  • Write a script or program that can perform the tasks mentioned above.
  • Ensure it can make HTTP requests to the Lambda Runtime API.
  • Ensure it can execute your function handler and capture its output.
  • Compile or package this script or program into an executable named bootstrap.

For instance, if you’re using a language like Rust, you’d write a Rust program that handles these tasks and then compile it into a binary named bootstrap.

3 — Placement and Permissions:
Once you have your bootstrap executable ready, place it in the src directory of your Lambda function. If you're on a Windows system and plan to deploy to a Linux-based Lambda environment (like provided.al2), ensure your bootstrap is cross-compiled for Linux. Also, remember that the bootstrap file must have execute permissions. While this is more of a concern on UNIX-based systems, it's good to be aware of it.

4 — Testing:
Before deploying, it’s wise to test your bootstrap locally. Tools like AWS SAM CLI can help emulate the Lambda environment on your machine, allowing you to ensure your custom runtime and bootstrap work as expected.

In essence, the bootstrap executable is your custom runtime's control center. It ensures seamless communication between AWS Lambda and your code, making the magic of custom runtimes possible.

Tweaking the Handler:
Depending on the specifics of your custom runtime, you might need to adjust how the handler is set up. If necessary, modify the Handler property in the CloudFormation template to align with your custom runtime's requirements.

Deploy and Watch the Magic Happen:
Navigate back to your Amplify project’s root directory and deploy your updates:

amplify-push

Post-Deployment Considerations


Once deployed, it’s crucial to test your Lambda function comprehensively. With the freedom of custom runtimes comes added responsibility:

  • Ensure robust error handling.
  • Maintain detailed logs to aid in debugging.
  • Monitor performance, especially cold start times, and optimize where needed.

Conclusion

Marrying AWS Amplify with AWS Lambda offers a robust platform for serverless applications. By integrating custom runtimes, developers can further expand their horizons, embracing a wider array of tools and capabilities. It’s all about finding the right tools for the job and making them work in harmony.

No video selected

Select a video type in the sidebar.

Related Articles

NEXTGEN Group increases investment in the Public Sector and appoints new Leadership

NEXTGEN Group increases investment in the Public Sector and appoints new Leadership 


NEXTGEN Group (NEXTGEN) has appointed Mick Lehmann, CSC as its General Manager Government, based in Canberra...

Read more >
gary cox

Gary Cox joins the NEXTGEN Group Board as an Independent Non-Executive Director

NEXTGEN Group has appointed industry veteran, Gary Cox, to the main Board of NEXTGEN as an Independent Non-Executive Director...

Read more >
asean2

NEXTGEN Group accelerates ASEAN expansion with major headcount investment in Philippines and Malaysia.


Continuing its ASEAN regional expansion, NEXTGEN Group has appointed Jenny Diamzon as Managing Director, Cyber...

Read more >
gigamon-2

Gigamon & NEXTGEN sign ANZ distribution agreement


Gigamon, the leading deep observability company today announced a new value-added distribution agreement with NEXTGEN across A/NZ...

Read more >
cyber secutiry

Cyber Security: Reviews, risks, and opportunity for industry in FedGov


In Canberra IT circles, there may be no more feverishly anticipated phrase than “strategy review” (apart from “MOG change” – but that’s another story)...

Read more >

Want to hear how NEXTGEN Group can help you succeed in APAC?

MKP03094[1]