Skip to content

Instantly share code, notes, and snippets.

@ctison
Last active April 27, 2021 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctison/986c7b935eaaa341476ac7e6d045bf96 to your computer and use it in GitHub Desktop.
Save ctison/986c7b935eaaa341476ac7e6d045bf96 to your computer and use it in GitHub Desktop.
Add SQS Event Source Mapping to trigger AWS Lambdas

Add SQS Event Source Mappings to trigger AWS Lambdas

CLI tools are: aws, jq, more, grep

Tips

# `jq -C` enables the colors
# `jq -r` outputs raw text format instead of JSON
# `more -R` renders formatting characters like terminal colors

List lambdas function

aws lambda list-functions | jq -r '.Functions[].FunctionName'

List event source mappings of a function

FUNCTION_NAME=
aws lambda list-event-source-mappings --function-name $FUNCTION_NAME | jq -C | more -R

List SQS queues

# Get Queue URL
PREFIX=app-
SUFFIX='-dlq$'
for SQS_URL in $(aws sqs list-queues --queue-name-prefix "$PREFIX" | jq -r '.QueueUrls[]' | grep -- "$SUFFIX"); do
  echo $(aws sqs get-queue-attributes --queue-url "$SQS_URL" --attribute-names QueueArn | jq -r .Attributes.QueueArn)
done

Add an event source mapping to a function

FUNCTION_NAME=
SQS_ARN=
aws lambda create-event-source-mapping --function-name "$FUNCTION_NAME" --event-source-arn "$SQS_ARN" --batch-size 5 --enabled --maximum-batching-window-in-seconds 0
#!/bin/bash
set -euo pipefail
shopt -s nullglob
FUNCTION_NAME=
PREFIX=app-
SUFFIX='-plq$'
for SQS_URL in $(aws sqs list-queues --queue-name-prefix "$PREFIX" | jq -r '.QueueUrls[]' | grep -- "$SUFFIX"); do
SQS_ARN=$(aws sqs get-queue-attributes --queue-url "$SQS_URL" --attribute-names QueueArn | jq -r .Attributes.QueueArn)
aws lambda create-event-source-mapping --function-name "$FUNCTION_NAME" --event-source-arn "$SQS_ARN" --batch-size 5 --enabled --maximum-batching-window-in-seconds 0 || true
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment