Skip to content

aws-samples/lambda-extension-optimization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Optimizing Lambda extensions performance

Please refer to Optimizing AWS Lambda extensions in C# and Rust blog for more details.

Prerequisites

To follow the solution walkthrough, you will need to:

  1. An AWS Account.
  2. Set up the AWS CLI to deploy resources to your account.
  3. Have the appropriate AWS Credentials configured.
  4. Rust, Cargo Lambda, dotnet sdk, and curl (version 7.75.0 or higher) installed.
  5. Install Docker on your machine.
  6. Bootstrap your environment with AWS CDK.

Solution Walkthrough

You can use your preferred IDE to deploy the solution. Refer to the cleanup section of this post for instructions to delete the resources to stop incurring any further charges.

Setup

  1. Clone the project repository to your local machine.
git clone https://github.com/aws-samples/lambda-extesion-optimization.git
  1. Navigate to the solution folder.
cd lambda-extension-optimization/app/
  1. Install the packages required.
npm install
  1. Store the AWS profile name in a variable.
AWS_PROFILE="<AWS profile to be used>"

Blank Lambda extension

  1. Store the stack name in a variable.
STACK_NAME="demo-blank-ext-stack"
  1. Inspect the code.
  • HeaderCounter Lambda function has a 500 ms delay baked into it.
  • Blank extension written in .NET 6 has a corp-demo-extensions-blank shell script that is responsible for running the extension. The extension is leveraging a common library that is responsible for the extension registration and continuous operation.
  • Blank extension written in Rust is leveraging lambda-extension crate that is taking care of the extension registration and continuous operation.
  1. Build the source files.
../scripts/publish-src.sh
  1. Deploy the solution stack.
cdk deploy $STACK_NAME --context lambda-memory=128 --profile $AWS_PROFILE
  1. Store the endpoints into corresponding variables.
API_URL_NO_EXT=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs[?OutputKey=='NoExtensionLambdaUrl'].OutputValue" \
--output text \
--profile $AWS_PROFILE)

echo $API_URL_NO_EXT

API_URL_DOTNET_EXT=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs[?OutputKey=='DotnetExtensionLambdaUrl'].OutputValue" \
--output text \
--profile $AWS_PROFILE)

echo $API_URL_DOTNET_EXT

API_URL_RUST_EXT=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs[?OutputKey=='RustExtensionLambdaUrl'].OutputValue" \
--output text \
--profile $AWS_PROFILE)

echo $API_URL_RUST_EXT
  1. Load test the solution by running the load-test.sh script against the endpoints.
ac=$(aws configure get aws_access_key_id --profile $AWS_PROFILE)
sc=$(aws configure get aws_secret_access_key --profile $AWS_PROFILE)
rg=$(aws configure get region --profile $AWS_PROFILE)

../scripts/load-test.sh $API_URL_NO_EXT $ac $sc $rg
../scripts/load-test.sh $API_URL_DOTNET_EXT $ac $sc $rg
../scripts/load-test.sh $API_URL_RUST_EXT $ac $sc $rg
  1. Navigate to Amazon CloudWatch (make sure you select the region where you stack is deployed).
  2. Select Dashboards in the left-hand side menu.
  3. Click on lambda-performance-dashboard to open the dashboard.
  4. Inspect the results.
  5. Clean up by deleting the CDK stack.
cdk destroy $STACK_NAME --profile $AWS_PROFILE

Event Collector Lambda extension

  1. Store the stack name in a variable.
STACK_NAME="demo-ec-ext-stack"
  1. Inspect the code.
  • HeaderCounter Lambda function has a 500 ms delay baked into it.
  • EventCollector extension written in .NET 6 has a corp-demo-extensions-event-collector shell script that is responsible for running the extension. The extension is leveraging a common library that is responsible for the extension registration and continuous operation.
  • EventCollector extension written in Rust is leveraging lambda-extension crate that is taking care of the extension registration and continuous operation.
  1. Build the source files.
../scripts/publish-src.sh
  1. Deploy the solution stack.
cdk deploy $STACK_NAME --context lambda-memory=128 --profile $AWS_PROFILE
  1. Store the endpoints into corresponding variables.
API_URL_NO_EXT=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs[?OutputKey=='NoExtensionLambdaUrl'].OutputValue" \
--output text \
--profile $AWS_PROFILE)

echo $API_URL_NO_EXT

API_URL_DOTNET_EXT=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs[?OutputKey=='DotnetExtensionLambdaUrl'].OutputValue" \
--output text \
--profile $AWS_PROFILE)

echo $API_URL_DOTNET_EXT

API_URL_RUST_EXT=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs[?OutputKey=='RustExtensionLambdaUrl'].OutputValue" \
--output text \
--profile $AWS_PROFILE)

echo $API_URL_RUST_EXT
  1. Load test the solution by running the load-test.sh script against the endpoints.
ac=$(aws configure get aws_access_key_id --profile $AWS_PROFILE)
sc=$(aws configure get aws_secret_access_key --profile $AWS_PROFILE)
rg=$(aws configure get region --profile $AWS_PROFILE)

../scripts/load-test.sh $API_URL_NO_EXT $ac $sc $rg
../scripts/load-test.sh $API_URL_DOTNET_EXT $ac $sc $rg
../scripts/load-test.sh $API_URL_RUST_EXT $ac $sc $rg
  1. Navigate to Amazon CloudWatch (make sure you select the region where you stack is deployed).
  2. Select Dashboards in the left-hand side menu.
  3. Click on lambda-performance-dashboard to open the dashboard.
  4. Inspect the results.
  5. Clean up by deleting the CDK stack.
cdk destroy $STACK_NAME --profile $AWS_PROFILE

Event Collector Native AOT Lambda extension

  1. Store the stack name in a variable.
STACK_NAME="demo-ec-aot-ext-stack"
  1. Inspect the code.
  • HeaderCounter Lambda function has a 500 ms delay baked into it.
  • EventCollector extension written in .NET 7 is compiled into an executable using the publish-src-aot.sh shell script. The extension is leveraging a common library that is responsible for the extension registration and continuous operation. The extension is using a custom serializer required for the Native AOT compilation.
  • EventCollector extension written in Rust is leveraging lambda-extension crate that is taking care of the extension registration and continuous operation.
  1. Make sure you have public.ecr.aws/sam/build-dotnet7 image.
# search for public.ecr.aws/sam/build-dotnet7
docker images -a
  1. If the public.ecr.aws/sam/build-dotnet7 is not present, pull the image.
# make sure to select the right architecture
docker pull public.ecr.aws/sam/build-dotnet7:latest-x86_64 
  1. Build the source files.
../scripts/publish-src-aot.sh
  1. Deploy the solution stack.
cdk deploy $STACK_NAME --context lambda-memory=128 --profile $AWS_PROFILE

Warning

Make sure you do not see this message: [WARNING]: Cannot find EventCollector extension built using Native AOT. Falling back to the DLL. You will need to fix the Native AOT build in case the message above appears.

  1. Store the endpoints into corresponding variables.
API_URL_NO_EXT=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs[?OutputKey=='NoExtensionLambdaUrl'].OutputValue" \
--output text \
--profile $AWS_PROFILE)

echo $API_URL_NO_EXT

API_URL_DOTNET_EXT=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs[?OutputKey=='DotnetExtensionLambdaUrl'].OutputValue" \
--output text \
--profile $AWS_PROFILE)

echo $API_URL_DOTNET_EXT

API_URL_RUST_EXT=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs[?OutputKey=='RustExtensionLambdaUrl'].OutputValue" \
--output text \
--profile $AWS_PROFILE)

echo $API_URL_RUST_EXT
  1. Load test the solution by running the load-test.sh script against the endpoints.
ac=$(aws configure get aws_access_key_id --profile $AWS_PROFILE)
sc=$(aws configure get aws_secret_access_key --profile $AWS_PROFILE)
rg=$(aws configure get region --profile $AWS_PROFILE)

../scripts/load-test.sh $API_URL_NO_EXT $ac $sc $rg
../scripts/load-test.sh $API_URL_DOTNET_EXT $ac $sc $rg
../scripts/load-test.sh $API_URL_RUST_EXT $ac $sc $rg
  1. Navigate to Amazon CloudWatch (make sure you select the region where you stack is deployed).
  2. Select Dashboards in the left-hand side menu.
  3. Click on lambda-performance-dashboard to open the dashboard.
  4. Inspect the results.
  5. Clean up by deleting the CDK stack.
cdk destroy $STACK_NAME --profile $AWS_PROFILE

Cost considerations

Make sure you follow the cleanup process to avoid any unexpected charges.

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

Warning

You should not use this AWS Content in your production accounts, or on production or other critical data. You are responsible for testing, securing, and optimizing the AWS Content, such as sample code, as appropriate for production grade use based on your specific quality control practices and standards.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •