Skip to content

Instantly share code, notes, and snippets.

@hutch120
Last active March 21, 2024 08:32
Show Gist options
  • Save hutch120/1f2716d68769a250a64d278afe65e49e to your computer and use it in GitHub Desktop.
Save hutch120/1f2716d68769a250a64d278afe65e49e to your computer and use it in GitHub Desktop.
Test the AWS SQS Queue functionality from a local server.
{
"accessKey": "",
"secretKey": "",
"sessionToken": "",
"region": "" ,
"queueUrl": ""
}
#
# Purpose
# Test the AWS SQS Queue functionality from a local server.
#
# References
# https://docs.aws.amazon.com/powershell/latest/reference/items/Receive-SQSMessage.html
#
# Instructions
# Install the AWS Powershell Module. Open Powershell and run the command on the next line (one time install)
# Install-Module -Name AWSPowerShell -Scope CurrentUser -AllowClobber
# Copy the text in this file onto a file on the local server.
# Save the file as "testqueue.ps1"
# In the same folder as testqueue.ps1, create a file called "config.json" with the following text
#
# {
# "accessKey": "",
# "secretKey": "",
# "sessionToken": "",
# "region": "" ,
# "queueUrl": ""
# }
#
# Open Powershell and run this file by typing: ./testqueue.ps1
#
$config = Get-Content 'config.json' | Out-String | ConvertFrom-Json
$accessKey = $config.accessKey
$secretKey = $config.secretKey
$sessionToken = $config.sessionToken
$region = $config.region # e.g. ap-southeast-2
$queueUrl = $config.queueUrl # e.g. https://sqs.ap-southeast-2.amazonaws.com/[AccountID]/[QueueName]
$longPollSeconds = 5 # The time in seconds the TCP connection waits for each poll before returning.
Import-Module AWSPowerShell
function startTest() {
logInfo("Starting test ...")
$counter = 0
$StartDate = get-date
$EndDate = get-date
while ($true) {
try {
$counter = next -counter $counter
$timeDiffSeconds = (New-TimeSpan -Start $StartDate -End $EndDate).seconds
if ($timeDiffSeconds -gt $longPollSeconds + 1) { # give it a 1 second buffer
logInfo("Long poll #$counter Diff $timeDiffSeconds seconds !!!TOO LONG!!!")
} else {
logInfo("Long poll #$counter Diff $timeDiffSeconds seconds")
}
$StartDate = get-date
$messages = Receive-SQSMessage -QueueUrl $queueUrl -WaitTimeInSeconds $longPollSeconds -AccessKey $accessKey -SecretKey $secretKey -SessionToken $sessionToken -Region $region
# Not important for network test.
# handleMessage($messages)
$EndDate = get-date
} catch {
Write-Output "$(Get-TimeStamp) An error occurred: $_"
}
}
}
function Get-TimeStamp {
return [DateTime]::UtcNow.ToString('u')
}
function next() {
param($counter)
$counter++
return ($counter)
}
function logInfo($message) {
Write-Output "$(Get-TimeStamp) $message"
}
# Not required for network test
function handleMessage($messages) {
if ($messages -ne $null) {
foreach ($message in $messages) {
Write-Output "$(Get-TimeStamp) Received message: $($message.Body)"
}
} else {
Write-Output "$(Get-TimeStamp) Long poll $counter 0 messages"
}
}
startTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment