import boto3
import redis
import datetime
# Redis configuration
redis_host = 'cluster-session.***.euc1.cache.amazonaws.com'
redis_port = 6379
redis_password = ''
# S3 configuration
s3_bucket_name = '*****-redis-sessions-debug'
def handler(event, context):
# Connect to Redis
client = redis.StrictRedis(
host=redis_host,
port=redis_port,
password=redis_password,
decode_responses=True
)
# Get all keys from Redis
keys = client.keys('*')
# Find the biggest key by size
biggest_key = ''
biggest_size = 0
for key in keys:
size = client.strlen(key)
if size > biggest_size:
biggest_key = key
biggest_size = size
# Get the value of the biggest key
value = client.get(biggest_key)
# Generate a dynamic filename based on the current date and time
current_time = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M')
filename = f'{current_time}.txt'
# Upload the value as a file to S3
s3 = boto3.resource('s3')
s3_object = s3.Object(s3_bucket_name, filename)
s3_object.put(Body=value)
# Disconnect from Redis
client.close()
return 'Success'
The above code will be executed upon a cloudwatch alert (in our case redis cluster memory utilization above a certain threshold). Lambda will then be triggered and will connect to that redis cluster, list all keys and find the one with the biggest value. Then it will copy the value as a text file and send it to an s3 bucket for further dev review. The file itself is named using the current date time.
zip -r redis-debug.zip .
upload the zip file in Lambda runtime: python 3.10 on arm64 infra allowed ram: 2056 MB max execution time: 1min or more depending on how busy your redis cluster is remember to run lambda in a vpc and give it the appropriate permissions to write to the required s3 bucket
Docker Buildx est une extension de Docker qui permet de créer des images Docker pour plusieurs plates-formes, y compris des plates-formes d’architecture différentes. Il permet également de créer des images pour plusieurs architectures à la fois, ce qui facilite la création de conteneurs Docker multi-plateformes.
Les commandes ci-dessus créent un environnement de build pour la plate-forme arm64. La commande « docker buildx create –name arm64 –platform linux/arm64 » crée un environnement de build nommé « arm64 » avec la plate-forme Linux/arm64 configurée. La commande « docker buildx inspect arm64 –bootstrap » configure l’environnement de build pour utiliser la plate-forme arm64. La commande « docker buildx use arm64 » active l’environnement de build pour la plate-forme arm64.
Enfin, la commande « docker run –rm –privileged multiarch/qemu-user-static –reset -p yes » configure l’environnement pour utiliser QEMU, un émulateur de processeur, pour exécuter des binaires de différentes architectures. Cela permet de construire des images Docker pour des plates-formes d’architecture différentes, même si l’hôte de build ne prend pas en charge ces plates-formes directement.
En gros, on créé un conteneur avec Redis et aws cli, qui va lire le nombre d’événements et envoyer l’info en tant que métrique personnalisée à CloudWatch. Ensuite on peut créer une alarme qui sera déclenchée à partir d’un certain seuil et comme action possible augmenter le nombre d’instances dans un groupe asg …
Commençons par construire une image Docker minimale:
FROM ubuntu:22.04
RUN apt-get update; DEBIAN_FRONTEND=noninteractive apt-get install wget curl jq zip unzip -y \
&& curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \
&& unzip awscliv2.zip && bash ./aws/install \
&& apt autoclean \
&& apt autoremove
RUN apt-get install redis less -y
mysql -h***.us-west-2.rds.amazonaws.com -uroot -p*** --raw --silent -e "SELECT CONCAT('SHOW GRANTS FOR ''', user, '''@''', host, ''';') AS query FROM mysql.user" > /tmp/users.sql
This command uses the MySQL command line tool to generate a SQL file called « users.sql » in the /tmp directory. The SQL file contains a series of SHOW GRANTS statements for each user on the database.
This command executes the SHOW GRANTS statements in the « users.sql » file and saves the output to a new SQL file called « grants.sql » in the /tmp directory.
sed -i 's/$/;/g' /tmp/grants.sql
This command uses sed to add a semicolon at the end of each line in the « grants.sql » file. This is necessary to make the file a valid SQL script.
After running these commands, the « grants.sql » file in the /tmp directory will contain a series of GRANT statements for each user on the database, allowing you to easily see the permissions granted to each user.