AWS CLI Logs Tip – Converting Local Time to UTC using Bash for Time Ranges

I’d been assigned a task to do some log analysis via AWS and didn’t want to have to use the AWS UI every time I needed to do the analysis as it would be a recurring task for a few days.

I wrote the below snippet to allow for quick variable substitution each time I needed to grab these logs – setting them to appropriate UTC time but visually appealing since all email communication around time ranges was in EST.

Hope this may help someone out in the future as I was very particular that I wanted to use built in bash functionality instead of installing commands/libraries to accomplish this task for portability.

function generateEpochString()
{
    TIME_TO_CONVERT="$1"

    #We use >&2 to prevent messing the return value of the function up
    #This allows us to print the echo statements to stderr and final return value to stdout
    #See more - https://superuser.com/a/1320694/233708

    #Begin conversion of time to UTC
    echo "Converting $TIME_TO_CONVERT to UTC..." >&2

    #Convert Local Timezone String to UTC 
    VARIABLE_TIME_UTC_STRING=$(date -u -d "$TIME_TO_CONVERT")
    echo "$TIME_TO_CONVERT converted to UTC is $VARIABLE_TIME_UTC_STRING ..." >&2

    #Convert Local Timezone String to UTC Epoch
    echo "Converting $TIME_TO_CONVERT to UTC Epoch..." >&2
    VARIABLE_TIME_UTC_EPOCH=$(date -u -d "$TIME_TO_CONVERT" +"%s")
    echo "$TIME_TO_CONVERT converted to UTC Epoch is $VARIABLE_TIME_UTC_EPOCH ..." >&2
    echo "$VARIABLE_TIME_UTC_EPOCH"
}

function generateLocalTimezoneString()
{
    VARIABLE_MONTH="$1"
    VARIABLE_DAY="$2"
    VARIABLE_YEAR="$3"
    VARIABLE_TIME="$4"
    VARIABLE_TIMEZONE="$5"

    VARIABLE_LOCAL_TIMEZONE_STRING="$VARIABLE_MONTH/$VARIABLE_DAY/$VARIABLE_YEAR $VARIABLE_TIME $VARIABLE_TIMEZONE"
    echo "$VARIABLE_LOCAL_TIMEZONE_STRING"
}

function main()
{
    #Declare variables for Start Time in Local Timezone
    VARIABLE_START_MONTH="06"
    VARIABLE_START_DAY="30"
    VARIABLE_START_YEAR="2021"
    VARIABLE_START_TIME="10:21:22"
    VARIABLE_START_TIMEZONE="EST"

    VARIABLE_START_TIME_EST_STRING=$(generateLocalTimezoneString "$VARIABLE_START_MONTH" "$VARIABLE_START_DAY" "$VARIABLE_START_YEAR" "$VARIABLE_START_TIME" "$VARIABLE_START_TIMEZONE")
    VARIABLE_START_TIME_UTC_EPOCH=$(generateEpochString "$VARIABLE_START_TIME_EST_STRING")

    #Declare variables for End Time in Local Timezone
    VARIABLE_END_MONTH="06"
    VARIABLE_END_DAY="30"
    VARIABLE_END_YEAR="2021"
    VARIABLE_END_TIME="13:21:22"
    VARIABLE_END_TIMEZONE="EST"

    VARIABLE_END_TIME_EST_STRING=$(generateLocalTimezoneString "$VARIABLE_END_MONTH" "$VARIABLE_END_DAY" "$VARIABLE_END_YEAR" "$VARIABLE_END_TIME" "$VARIABLE_END_TIMEZONE")
    VARIABLE_END_TIME_UTC_EPOCH=$(generateEpochString "$VARIABLE_END_TIME_EST_STRING")

    #Confirm the Variables before Execution
    echo "AWS Log Request - Start Time - Local Time - $VARIABLE_START_TIME_EST_STRING"
    echo "AWS Log Request - Start Time - Epoch UTC Time - $VARIABLE_START_TIME_UTC_EPOCH"
    echo "AWS Log Request - End Time - Local Time - $VARIABLE_END_TIME_EST_STRING"
    echo "AWS Log Request - End Time - Epoch UTC Time - $VARIABLE_END_TIME_UTC_EPOCH"

    #Pull the Logs - Example Usage
    aws logs start-query --log-group-name /aws/batch/job --start-time "$VARIABLE_START_TIME_UTC_EPOCH" --end-time "$VARIABLE_END_TIME_UTC_EPOCH" --query-string 'fields @message | limit 10'
}

main

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s