How can I help scale an Elasticache (redis) set up with Lambda (SDK)?

Question:

I’m implementing a solution that basically involves a

Redis high/low CPU Utilization –> (CW Alarm goes off) –> EventBridge Event –> Triggers Lambda –> Lambda scales in Redis

I want this Lambda to check the the number of shards currently being used by Redis, and to increment and decrease that number by 1, or increment and increase that number by one (depending on which alarm I trigger).

If I wanted to set the number of shards statically using Lambda, it would be done as following:

import boto3

client = boto3.client('elasticache')

response = client.modify_replication_group_shard_configuration(
    ReplicationGroupId='singleview',
    NodeGroupCount=4
    ApplyImmediately=True,
    NodeGroupsToRemove=[
        'string1',
        'string2',
        'string3',
    ]

If I put this in a lambda_function.py file, it would only update the NodeGroupCount from whatever it is at the moment to e.g. 4. This is obviously not a great scaling policy.

My issue is that I don’t know how to increment the NodeGroupCount number and make it -1 of whatever the current number is in a Lambda/boto3 environment – how do I acquire the current NodeGroupCount & how do I feed that into the result, etc.

Asked By: DevBob

||

Answers:

It turns out that you’re able to move through the JSON response by using a series of braces.

So I basically did response[‘KeyName’][0][‘KeyName’] etc. until I reached the value I needed. I set that as a variable by just using

variable = response[‘KeyName][0][‘KeyName’]

Then I just created a simple function that added or removed from that as was necessary.

Answered By: DevBob