Skip to content

Latest commit

 

History

History

alb-lambda-serverless

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Application Load Balancer with Lambda as a target

This pattern registers the lambda function as the target for the Application Load Balancer.

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

Requirements

Deployment Instructions

  1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:

    git clone https://github.com/aws-samples/serverless-patterns
  2. Change directory to the pattern directory:

    cd serverless-patterns/alb-lambda-serverless
  3. From the command line, use npm/yarn to install the development dependencies:

    npm install

    -or-

    yarn install
  4. From the command line, use Serverless Framework to deploy the AWS resources for the pattern as specified in the serverless.yml file:

    serverless deploy --verbose

    The above command will deploy resources to ap-south-1 region by default. You can override the target region with --region <region> CLI option, e.g.

    serverless deploy --verbose --region us-west-2
  5. Note the LoadBalancerDNSName output from the Serverless Framework deployment process. You will use this value for testing.

How it works

This pattern registers the lambda function as the target for the Application Load Balancer. When the load balancer endpoint is hit in the browser, if the specified URL (/hello) path matches, it triggers the lambda function & the file (hello) is downloaded in your local system with the output from lambda function.

Testing

Hit the Load Balancer DNS endpoint.

To test the endpoint, paste the output URL obtained from serverless framework in the browser of your choice.

Expected result

As no specific URL path is provided, it returns the default output i.e. 404-Page Not Found.

Page Not Found

Now edit the URL by appending the /hello path in it. This will download a file named hello in your local system. The file will have the following content.

<p>Lambda function integrated with ALB called</p>

CloudWatch logs

Open AWS CloudWatch Console and navigate to /aws/lambda/alb-lambda-serverless-prod-functionOne log group. You should be able to see a new Event Stream with the Received Event information, and Event Message, logged into the stream.

Cleanup

  1. Delete the stack

    serverless remove --verbose
  2. Confirm the stack has been deleted

    aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'alb-lambda-serverless-prod')].StackStatus"

    Expected output

    [
        "DELETE_COMPLETE"
    ]

    NOTE: You might need to add --region <region> option to AWS CLI command if you AWS CLI default region does not match the one, that you used for the Serverless Framework deployment.


Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0