Article on how to pass a variable to an AWS Lambda function using the CloudWatch event. With CloudWatch event you can pass different variables and run different code. You can have just one Lambda function and, using a different variable value, invoke a completely different behavior.
What can you do? For example, you can pass a value that will periodically warm up the AWS lambda function and prevent a cold start. More on this topic in another article.
One lambda function can be responsible for starting and stopping ec2 machines. Depending on what variable value you pass, the lambda function will stop or start the ec2 machines. Don’t worry, I’ll make a video about this soon.
IAM role
For my IAM role I will only grant log permissions, you can allow your function to do much more 😉
Lambda function
I create AWS Lambda functions always using the latest runtime version, currently it’s python 3.11
. I also choose a specific IAM role that I created earlier.
The function code is very simple. If the cloudwatch
variable passed by CloudWatch event has the value option_1
, the first part of the code will be executed. If the variable has the value option_2
, then the second part, and if option_3
, then the third part.
Additionally, if the function is run manually and no variable is passed, or the variable has a different value, the code from the else
block will be executed.
Below is the lambda function code:
import json
def lambda_handler(event, context):
variable_from_cloudwatch_event = event.get('cloudwatch')
if variable_from_cloudwatch_event == "option_1":
print("You chose option 1, execute code 1...")
if variable_from_cloudwatch_event == "option_2":
print("You chose option 2, execute code 2...")
if variable_from_cloudwatch_event == "option_3":
print("You chose option 3, execute code 3...")
else:
print("Function triggered manually, or variable not read from Cloudwatch event, execute code...")
CloudWatch event
Now the most important thing is adding the EventBridge (CloudWatch event)
trigger. You can do this from lambda or Amazon EventBridge. If you do it from the lambda level, you must remember to configure it later.
To do this, go to Amazon EventBridge, select the previously created trigger and edit it. In step three, with the selected target, go to additional settings, select constant (JSON text)
and add what you need. I chose the variable name cloudwatch
with the value option_2
:
Code added to constant (JSON text)
:
{
"cloudwatch":"option_2"
}
I encourage you to watch the video and see how it works in practice How to pass variable from CloudWatch event to AWS Lambda function | Tutorial with demo 2023 – YouTube.
Summary
As you can see, running a lambda function with different variable values is not that difficult. I encourage you to experiment and create more complex solutions. It is not worth copying the entire code and creating new functions when you only want to change a small thing in the function’s behavior.
I encourage you to visit my YouTube channel Wojciech Lepczyński – YouTube, gain new knowledge and develop yourself, it may have a positive impact on your life and finances.