Skip to content

Instantly share code, notes, and snippets.

@J00MZ
Last active May 4, 2019 22:05
Show Gist options
  • Save J00MZ/e3931ab4f6a02397a1e433bafd6ab954 to your computer and use it in GitHub Desktop.
Save J00MZ/e3931ab4f6a02397a1e433bafd6ab954 to your computer and use it in GitHub Desktop.
Use DynamoDB item to manage Global Flag variable

Objective

Use DynamoDB to have a centralized location flag variable that can be accessed from multiple EC2 instances

Steps

  1. Create DynamoDB table, call it GlobalVariable.
  2. Primary key: name = id, type = number
  3. Sort key field: name = flag, type = String
  4. Add item to table using your value with key id # - example below uses 42.
  5. add your flag variable value in "flag" field beginning with "val-", for example: val-youareawesome

Prerequisites

  • IAM role that can query DynamoDB
  • Install awscli
  • Install jq

Run on instances

The below command will get only the variable value from DynamoDB

aws dynamodb query --table-name GlobalVariable --key-condition-expression "id = :id and begins_with(flag, :dt)" --expression-attribute-values '{":id":{"N":"42"},":dt":{"S":"val-"}}'|jq '.Items[0].flag.S'|awk -F\" '{print $2}'|sed 's/val-//'

In a Bash script you can get this as a variable value by wrapping command like so:
value=$(<COMMAND>)

Run echo $value to see who is awesome :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment