Ah, the beauty of a clear blue sky, teeming with fluffy white clouds. But let's admit it, we're not here to talk about atmospheric condensation. We're here to whisper to a different kind of cloud: the Amazon Web Services (AWS) Cloud. You see, AWS is home to mystical entities called Lambdas. They are quite good listeners, but like any misunderstood creature, you have to know how to talk to them. Today, we're going to learn how to whisper to three specific Lambdas and manage a pesky thing called Redshift costs. OK—let's get into it!
Deciphering the Cloud Slang
Before we head up into the ether, you must familiarize yourself with the lingo of the Cloud:
AWS Lambda: These are the magical spirits of the Cloud, waiting to perform tasks at our command. We're dealing with
Lambda0_Redshift_Queue
,Lambda1_TurnON_Lambda0
, andLambda2_TurnOFF_Lambda0
today.EventBridge Scheduler: A fancy Cloud alarm clock that wakes up our Lambdas when we need them.
Concurrency: The magic juice that dictates how many tasks a Lambda can perform simultaneously.
Redshift: A hoarder of data that, despite its cool name, costs money to operate.
SQS Queue: A never-ending buffet line feeding tasks to our
Lambda0_Redshift_Queue
, which then stuffs them into Redshift.
Calling Upon the Lambdas
Let's summon our Lambda spirits. We'll use the magical artifact known as the AWS Management Console:
- Venture into the
Lambda
section of the console. - Click
Create function
. - Bestow the names
Lambda0_Redshift_Queue
,Lambda1_TurnON_Lambda0
, andLambda2_TurnOFF_Lambda0
upon your functions. - Under
Runtime
, select a spell language likePython
. - Click
Create function
. - Repeat for the other Lambdas.
Our Lambdas are summoned and ready to be imbued with abilities!
Empowering Lambda1_TurnON_Lambda0: The Morning Rooster
Lambda1_TurnON_Lambda0
is our morning rooster. Its crow is a spell that boosts the power of Lambda0_Redshift_Queue
to process more tasks from the SQS buffet line. Here's its chant:
import boto3
def lambda_handler(event, context):
lambda_client = boto3.client('lambda')
response = lambda_client.put_function_concurrency(
FunctionName='Lambda1_TurnON_Lambda0', # replace with your function name
ReservedConcurrentExecutions=40 # replace with the number of concurrent executions you want to reserve
)
return {
'statusCode': 200,
'body': response
}
Now Lambda1_TurnON_Lambda0
is ready to crow Lambda0_Redshift_Queue
awake at 40 concurrency!
Empowering Lambda2_TurnOFF_Lambda0: The Night Owl
Lambda2_TurnOFF_Lambda0
is our night owl. It soothes Lambda0_Redshift_Queue
to sleep by reducing its power to zero, saving us some magical pennies on Redshift. Lambda2_TurnOFF_Lambda0
's lullaby goes like this:
import boto3
def lambda_handler(event, context):
lambda_client = boto3.client('lambda')
response = lambda_client.put_function_concurrency(
FunctionName='Lambda2_TurnOFF_Lambda0', # replace with your function name
ReservedConcurrentExecutions=0 # replace with the number of concurrent executions you want to reserve
)
return {
'statusCode': 200,
'body': response
}
Lambda2_TurnOFF_Lambda0
is now set to serenade Lambda0_Redshift_Queue
to sleep!
The Buffet Line: SQS Queue and Lambda0_Redshift_Queue
Lambda0_Redshift_Queue
, our workhorse, processes tasks from the SQS queue and feeds them into Redshift. But remember, there's no such thing as a free lunch in the cloud, and this continuous feeding can rack up hefty Redshift costs. That's why we need our rooster and owl, Lambda1_TurnON_Lambda0
and Lambda2_TurnOFF_Lambda0
, to regulate Lambda0_Redshift_Queue
's feeding habits.
Setting the Alarm Clock: EventBridge Scheduler
Our rooster and owl need their sleep schedules. Thankfully, we have the EventBridge Scheduler, a rather precise alarm clock.
Setting the Rooster's Alarm:
- Drift over to the EventBridge section in the AWS Management Console.
- Click
Create rule
. - Give it a catchy name like
InvokeRooster
. - In
Schedule
, selectRecurring schedule
then selectCron-based schedule
and whisper: 0 0 * * ? *. This charm will rouse our rooster,Lambda1_TurnON_Lambda0
, daily at midnight! - Under
Targets
, chooseLambda function
and selectLambda1_TurnON_Lambda0
. - Click
Create
.
Setting the Owl's Alarm:
Follow the same steps, but slightly differently:
- Name it something catchy like
InvokeOwl
. - Change the Cron expression to 30 1 * * ? * to rouse our owl,
Lambda2_TurnOFF_Lambda0
, at 1:30 AM. - Under
Targets
, chooseLambda function
and selectLambda2_TurnOFF_Lambda0
.
Kudos, You're Now a Cloud Whisperer!
Well done, you! With Lambda1_TurnON_Lambda0
, our rooster, crowing Lambda0_Redshift_Queue
into action at midnight, and Lambda2_TurnOFF_Lambda0
, our owl, lulling Lambda0_Redshift_Queue
into rest at 1:30 AM, we've mastered the art of cost-efficient cloud whispering.
In this world of Cloud Whisperers, we don't just command our Lambdas. We learn to listen, understand, and then communicate effectively. We've just begun to scratch the surface of this magical world. Don't stop whispering to those clouds!
And remember, even the best whisperers sometimes need to shout for help. If you're lost in the clouds or struggling to understand the whisper of a Lambda, don't hesitate to call out. After all, we're all just trying to make sense of this beautifully complex cloud. Happy whispering, fellow Cloud Whisperer!
Top comments (2)
kudos for the article 😎 it's well explained
thank you!