A Cloud Whisperer's Guide to AWS: Talking to Lambdas
Tags: python, aws, cloud, devops
Published: 2023-07-30 06:00:00

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:

1. **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`, and `Lambda2_TurnOFF_Lambda0` today.

2. **EventBridge Scheduler**: A fancy Cloud alarm clock that wakes up our Lambdas when we need them.

3. **Concurrency**: The magic juice that dictates how many tasks a Lambda can perform simultaneously.

4. **Redshift**: A hoarder of data that, despite its cool name, costs money to operate.

5. **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:

1. Venture into the `Lambda` section of the console.
2. Click `Create function`.
3. Bestow the names `Lambda0_Redshift_Queue`, `Lambda1_TurnON_Lambda0`, and `Lambda2_TurnOFF_Lambda0` upon your functions.
4. Under `Runtime`, select a spell language like `Python`.
5. Click `Create function`.
6. 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:

```python
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:

```python
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:

1. Drift over to the EventBridge section in the AWS Management Console.
2. Click `Create rule`.
3. Give it a catchy name like `InvokeRooster`.
4. In `Schedule`, select `Recurring schedule` then select `Cron-based schedule` and whisper: 0 0 * * ? *. This charm will rouse our rooster, `Lambda1_TurnON_Lambda0`, daily at midnight!
5. Under `Targets`, choose `Lambda function` and select `Lambda1_TurnON_Lambda0`.
6. Click `Create`.

![EventBridge Scheduler Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvcxrnoa0p7n2j6gnqbm.png)

#### Setting the Owl's Alarm:

Follow the same steps, but slightly differently:

1. Name it something catchy like `InvokeOwl`.
2. Change the Cron expression to 30 1 * * ? * to rouse our owl, `Lambda2_TurnOFF_Lambda0`, at 1:30 AM.
3. Under `Targets`, choose `Lambda function` and select `Lambda2_TurnOFF_Lambda0`.

![EventBridge Scheduler Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a30kyih9qws1r7fq7tyg.png)

### 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!