This project showcases how to improve observability in serverless applications by incorporating logging and tracing in a Rust-based AWS Lambda function. The primary purpose of this Lambda function is to find the maximum profit you can achieve given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k, which is the max times of transactions. For detailed, structured logging, it utilizes the tracing and tracing-subscriber crates, and for in-depth tracing of function executions, it integrates with AWS X-Ray. This setup ensures a thorough analysis of the function's execution and performance. Additionally, with the help of AWS CloudWatch, it enables all logs and traces.
Users can remotely configure the invocation request to trigger the function:
cargo lambda invoke --remote max_stock_profit_with_logging_and_tracing --data-ascii "{"prices": [3,2,6,5,0,3], "k": 2}"
or directly trigger the function using the API URL:
curl -X POST https://5n3ntevpng.execute-api.us-east-1.amazonaws.com/max_stock_profit -H 'content-type: application/json' -d '{"prices": [3,2,6,5,0,3], "k": 2}'
- Add logging to a Rust Lambda function
- Integrate AWS X-Ray tracing
- Connect logs/traces to CloudWatch
- Begin by initializing your Cargo Lambda project using the command
cargo lambda new <YOUR-PROJECT-NAME>. - Include "tracing", "tracing-subscriber", and other essential dependencies in the
Cargo.tomlfile. Modify thesrc/main.rsfile according to your design and requirements. - Navigate to the project directory, locally test the functionality by running the command
cargo lambda watch.
- Navigate to the AWS IAM Management Console to create a new IAM User for credential management.
- Attach policies
LambdaFullAccess,IAMFullAccess, andAWSXrayFullAccess. - Under
Security Sredentialssection, generate an access key for API access. Make sure the key is safely stored and not lost. - Set up your environment variables so that cargo lambda knows which AWS account and region to deploy to.
export AWS_ACCESS_KEY_ID="your_access_key_here"
export AWS_SECRET_ACCESS_KEY="your_secret_key_here"
export AWS_DEFAULT_REGION="your_preferred_region_here"
- Build the project by running
cargo lambda build --release. - Deploy the project by running
cargo lambda deploy.
- Go to AWS console and enter AWS Lambda, navigate into your lambda function. Then under
Configuration, clickMonitoring and operations tools. - In the
Additional monitoring toolspanel, clickeditto enableAWS X-Ray Active tracingandLambda Insights Enhanced monitoring. - Test remote invoke and go to
Monitortab to view X-Ray traces.
- Initiate a new API setup, opting for the default REST API option, and then create a new resource for your API's endpoint.
- For the created resource, implement an
ANYmethod and associate it with your Lambda function. - Launch your API by creating a new deployment stage.
- Click deploy, find
stagesand find your invoke URL. - You can test your API gateway using
curlrequest:
curl -X POST https://5n3ntevpng.execute-api.us-east-1.amazonaws.com/max_stock_profit -H 'content-type: application/json' -d '{"prices": [3,2,6,5,0,3], "k": 2}'
- After the success of test, set your
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY, andAWS_REGIONto gitlabSettings -> CI/CD -> Variables. - Use
.gitlab-ci.ymlandmakefileto enable auto build, test, and deploy of the lambda function.


