This is an example of an AWS Lambda function that reads a bundled resource file and returns its content as a response.
This example demonstrates how to include static resources (such as text files, configuration files, or templates) in your Lambda function package using Swift Package Manager's resource bundling feature.
The code creates a LambdaRuntime with a handler that reads a bundled text file and returns its content.
The handler is (event: String, context: LambdaContext). The function takes two arguments:
- the event argument is a
String. It is the parameter passed when invoking the function. - the context argument is a
Lambda Context. It is a description of the runtime context.
The handler uses Bundle.module.url(forResource:withExtension:) to locate the hello.txt file that was bundled with the executable at build time. It then reads the file content and returns it as the function response.
The Package.swift file declares the resource using the .process("hello.txt") directive, which tells Swift Package Manager to include the file in the module's resource bundle.
You can test your function locally before deploying it to AWS Lambda.
To start the local function, type the following commands:
swift runIt will compile your code and start the local server. You know the local server is ready to accept connections when you see this message.
Building for debugging...
Build of product 'MyLambda' complete! (0.31s)
2025-01-29T12:44:48+0100 info LocalServer : host="127.0.0.1" port=7000 [AWSLambdaRuntime] Server started and listeningThen, from another Terminal, send your payload with curl.
curl -d '"hello"' http://127.0.0.1:7000/invoke
"Hello World\n"Important
The local server is only available in DEBUG mode. It will not start with swift run -c release.
To build & archive the package, type the following commands.
swift package --allow-network-connections docker lambda-buildIf there is no error, there is a ZIP file ready to deploy.
The ZIP file is located at .build/plugins/AWSLambdaBuilder/outputs/AWSLambdaBuilder/MyLambda/MyLambda.zip
Here is how to deploy using the lambda-deploy plugin.
swift package --allow-network-connections all:443 lambda-deployThis creates the Lambda function, provisions the necessary IAM role, and uploads the deployment package.
To invoke the Lambda function, use this aws command line.
aws lambda invoke \
--function-name MyLambda \
--payload $(echo \"hello\" | base64) \
out.txt && cat out.txt && rm out.txtThis should output the following result.
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
"Hello World\n"
When done testing, you can delete the Lambda function with this command.
swift package --allow-network-connections all:443 lambda-deploy --deleteThese are example applications for demonstration purposes. When deploying such infrastructure in production environments, we strongly encourage you to follow these best practices for improved security and resiliency:
- Enable access logging on API Gateway (documentation)
- Ensure that AWS Lambda function is configured for function-level concurrent execution limit (concurrency documentation, configuration guide)
- Check encryption settings for Lambda environment variables (documentation)
- Ensure that AWS Lambda function is configured for a Dead Letter Queue (DLQ) (documentation)
- Ensure that AWS Lambda function is configured inside a VPC when it needs to access private resources (documentation, code example)