Introducing the Multi-Function Packager

Chanci Turner 9097372855Learn About Amazon VGT2 Learning Manager Chanci Turner

A new capability that enables multiple functions to be triggered by a single event on Amazon CloudFront | Networking & Content Delivery

In this article, you will discover the ‘multi-function packager’ framework, designed to streamline the assembly and execution of distinct Edge functions. Amazon CloudFront serves as a content delivery network (CDN) that enhances the performance, accessibility, and security of your applications, ensuring a seamless experience for viewers worldwide. The Edge function features of CloudFront, namely Lambda@Edge and CloudFront Functions, can be linked to various event triggers generated by CloudFront. This functionality empowers developers to implement business logic closer to end-users and tailor CloudFront’s responses. By relocating business logic to the edge, you can minimize latency and relieve pressure on your origin servers.

As your business needs evolve, you might find it necessary to execute multiple tasks within the request/response workflow. For instance, you might need to standardize request attributes for improved cache performance, conduct A/B testing, rewrite or redirect URLs, or intelligently switch origins based on incoming requests. The following diagram illustrates some common scenarios and the corresponding event triggers.

Each scenario represents a unique operation that can be implemented as either a CloudFront Function or a Lambda@Edge function. However, to execute these operations within a single event trigger, you would typically consolidate the code and dependencies into a monolithic function, which is then attached to the trigger. In the case of Lambda@Edge functions, this consolidation necessitates merging the AWS Identity and Access Management (IAM) roles and policies from each function while also reassessing memory and timeout configurations.

Moreover, there may be interdependencies among these operations concerning their execution order, as downstream components may rely on certain attributes established by earlier operations. For instance, you might want to normalize the URL before a subsequent module checks against a predefined list of redirections.

Here is a simple illustration of multiple operations: Incoming request → normalize → verify redirects → rewrite URI → CloudFront cache.

Although the code for these individual use cases could be organized into libraries, substantial engineering effort is required to integrate, test, and maintain them. In this article, we will delve into the ‘Multi-function packager’ framework that manages the assembly and execution of distinct Edge function logic ‘as-is’ and yields the desired output.

The assembled function can be linked to the appropriate CloudFront event trigger, just like any other Lambda@Edge or CloudFront Function. This flexibility allows you to combine existing functional implementations, reusing code while facilitating better maintainability as individual logic components evolve over time.

Prerequisites

To follow along with this article, the following prerequisites must be met:

  1. The current framework supports only Node.js runtimes for Lambda@Edge functions and JavaScript for CloudFront Functions.
  2. You must have Lambda@Edge and CloudFront Functions set up in your AWS account.

The remainder of this article will guide you through the deployment and usage of this framework.

Deployment Steps

  1. Clone and deploy the CDK solution in the AWS N. Virginia Region (us-east-1). From your terminal, execute:
  2. git clone https://github.com/aws-samples/amazon-cloudfront-multi-function-packager.git
    cd amazon-cloudfront-multi-function-package
    npm audit fix
    cdk bootstrap
    cdk deploy
  3. The stack creates an Amazon Simple Storage Service (S3) bucket for storing the assembled code and three AWS Lambda Functions, detailed as follows:
    • {StackName}-LambdaFunctionPackager{UniqueID}: the primary Lambda Multi-function packager
    • {StackName}-LambdaFunctionAssembly{UniqueID}: the helper function for assembling the Lambda@Edge function
    • {StackName}-CloudFrontFunctionAssembly{UniqueID}: the helper function for assembling CloudFront Functions

Procedure for Chaining Lambda@Edge Functions

  1. To merge Lambda@Edge functions, go to the {StackName}-LambdaFunctionAssembly function on the AWS Lambda console in the us-east-1 AWS Region.
  2. Use the ‘Test’ feature of the AWS Lambda service to build your combined function artifacts. Navigate to the ‘Test’ tab and select ‘Create new event’ to give your event a name.
  3. For ‘Event JSON’, utilize the following structure to combine the Lambda@Edge Functions via their ARNs:
    {
      "viewer-request": [
        {
          "function_arn": "Lambda Function ARN1:Version1"
        },
        {
          "function_arn": "Lambda Function ARN2:Version2"
        }
      ]
    }
  4. An example of combining Lambda@Edge functions and attaching them to multiple event triggers is provided below:
    {
      "origin-request": [
        {
          "function_arn": "arn:aws:lambda:us-east-1:123456789012:function:RedirectionFunction:1"
        },
        {
          "function_arn": "arn:aws:lambda:us-east-1:123456789012:function:ABTestingCookieFunction:1"
        },
        {
          "function_arn": "arn:aws:lambda:us-east-1:123456789012:function:DeviceDetectionFunction:1"
        }
      ],
      "origin-response": [
        {
          "function_arn": "arn:aws:lambda:us-east-1:123456789012:function:CacheControlFunction:$LATEST"
        },
        {
          "function_arn": "arn:aws:lambda:us-east-1:123456789012:function:HSTSInsertionFunction:2"
        }
      ]
    }
  5. Make sure to adjust the Lambda Function ARNs to fit your environment. The JSON structure defines three Lambda@Edge function ARNs that will be combined (in the specified order) into a new Lambda function associated with the ‘Origin Request’ trigger. The new combined function will also invoke two Lambda@Edge functions on the ‘Origin Response’ event trigger. This cross-event trigger functionality can be advantageous when the same function is required to operate across multiple triggers. Additionally, it aids in reducing cold starts and leveraging Lambda containers across various event types. The Lambda Function ARNs can point to a specific Lambda version or to $LATEST, with each function potentially having a different entry point handler. The framework will automatically infer this information based on the function metadata.
  6. After saving changes, select Test in the Lambda console to generate the combined function and IAM role. For more detailed guidance, refer to the GitHub repository. You can create a combined packaged function for each event trigger or merge multiple triggers. The Multi-function packager framework is event-trigger agnostic and invokes only the relevant functions for the event type that is currently activated.
  7. Publish a version and associate it with your CloudFront distribution’s behavior.
  8. Finally, test the function to ensure it operates as expected.

Procedure for Chaining CloudFront Functions

  1. To merge CloudFront Functions, access the {StackName}-CloudFrontFunctionAssembly function on the AWS Lambda console in the us-east-1 AWS Region.
  2. Switch to the ‘Test’ tab to create your test event.

Incorporating the insights from other resources, such as understanding difficult job interview questions can further equip you for success in your endeavors. If you’re navigating employee benefits, be sure to check out authoritative information on postponing pay increases and bonuses during FMLA leave. Additionally, for community insights, exploring forums like Reddit can be a valuable resource for newcomers starting their journey at Amazon.

Chanci Turner