Using environment variables
Environment variables are useful for storing variables that you may want to change without modifying code.
Cody is going on vacation. While she would normally be the main person to receive messages, for a week it will be have to go to Jeff.
Let's tweak the Lambda function a bit to send a personalized SMS to Jeff!
I have assigned Jeff's phone number and name to the relevant environment variables.
This exercise is part of the course
Streaming Data with AWS Kinesis and Lambda
Exercise instructions
- Import the
os
module. - Get the environment variables for PHONE_NUMBER and RECEIVER.
- Get the environment variable for DEPARTMENT, and if it's not set, fall back to
'Fleet Department'
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import ____
# Get Environment variables, specify defaults
PHONE_NUMBER = os.____.get("ALERT_PHONE_NUMBER", None)
RECEIVER = os.environ.____("RECEIVER_NAME", "Random Person")
DEPARTMENT = ____("DEPARTMENT", "____")
# Construct message
message = """Hello {} from {}!
Here are the speeders:
{}
""".format(RECEIVER, DEPARTMENT, speeders.to_string())
# Send message
sns.publish(PhoneNumber = PHONE_NUMBER, Message = message)