In this article , I want to share a specific use case where you may need to revert a change that broke your application when using AWS SAM.
- I am deploying my serverless backend with AWS Sam
- I have a front end contact form app that sends request to the backend
- Someone ( me in this case) changed the lambda function code using the console because of a mistake. I changed a wrong function and therefore the contact form app is broken.
- I need to troubleshoot and revert the changes
Now we’ve established the scenario, here are the steps that can be taken to revert to backend to the the state when things last worked.
How can I find when things were working properly ? Cloudwatch Logs to the rescue. Lambda sends all the function execution logs to cloudwatch.
So I head over to my Lambda function for the contact app backend.
Now the steps to take to debug and revert to a working change:
- Find the cloudwatch log that shows successful lambda function execution and note the timestamp.
- Go to the S3 bucket where you’re uploading the sam deployment package. ( Sam packages and stores the files in a S3 bucket before deploying the stack)
- Use the timestamp you noted to find a file that was uploaded before the working log was written.
- Download that file and extract.
- The files extracted contains the lambda function file. Im my case it’s lambda_function.py ( I’m using python runtime for my lambda function)
- View the code and compare with the current code.
- Change the current code and package and deploy the sam package.
- Things should work now
This is a very special case which could be specefic to my workflow. And there might be other ways to make sure I don’t ever have to go through this process. But in this instance, I was able to use the listed steps to debug and revert back the changes that broke my contact form application.
Let me know if you have any questions in the comments below.