Home > Back-end >  botot3 with sqs: the address 'QueueUrl' is not valid for this endpoint
botot3 with sqs: the address 'QueueUrl' is not valid for this endpoint

Time:12-27

I am trying to the get queue url from a SQS queue. I have read about other posts Set the endpoint for boto3 SQS and boto3 sqs incorrect url when not specified endpoint url but it is still puzzling why the code is not working.

My region being us-east-1 and the endpoint seems to be the correct one https://queue.amazonaws.com/XXXXXXXXXXXX/MyMessages (legacy)

import json
import boto3

from datetime import datetime


def lambda_handler(event, context):

    now = datetime.now()
    current_time = now.strftime("%H:%M:%S %p")

    sqs = boto3.client('sqs', region_name="us-east-1")
    #get queue url using queue name
    queueurl = sqs.get_queue_url(QueueName='MyMessages')
    print(queueurl)
    
    for x in range(5):
        sqs.send_message(
            QueueUrl=str(queueurl),
            MessageBody=current_time
        )
    return {
        'statusCode': 200,
        'body': json.dumps(current_time)
    }

Error

Test Event Name
sendMessage

Response
{
  "errorMessage": "An error occurred (InvalidAddress) when calling the SendMessage operation: The address {'QueueUrl': 'https://queue.amazonaws.com/XXXXXXXXXXXX/MyMessages', 'ResponseMetadata': {'RequestId': '10fea792-29fa-5484-9bc0-fbb6ad9320b7', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '10fea792-29fa-5484-9bc0-fbb6ad9320b7', 'date': 'Tue, 27 Dec 2022 05:27:05 GMT', 'content-type': 'text/xml', 'content-length': '322'}, 'RetryAttempts': 0}} is not valid for this endpoint.",
  "errorType": "ClientError",
  "requestId": "ba1f3c72-6180-4308-ad12-0bc6b02c3793",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 18, in lambda_handler\n    sqs.send_message(\n",
    "  File \"/var/runtime/botocore/client.py\", line 391, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/runtime/botocore/client.py\", line 719, in _make_api_call\n    raise error_class(parsed_response, operation_name)\n"
  ]
}

Function Logs
START RequestId: ba1f3c72-6180-4308-ad12-0bc6b02c3793 Version: $LATEST
{'QueueUrl': 'https://queue.amazonaws.com/XXXXXXXXXXXX/MyMessages', 'ResponseMetadata': {'RequestId': '10fea792-29fa-5484-9bc0-fbb6ad9320b7', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '10fea792-29fa-5484-9bc0-fbb6ad9320b7', 'date': 'Tue, 27 Dec 2022 05:27:05 GMT', 'content-type': 'text/xml', 'content-length': '322'}, 'RetryAttempts': 0}}
[ERROR] ClientError: An error occurred (InvalidAddress) when calling the SendMessage operation: The address {'QueueUrl': 'https://queue.amazonaws.com/XXXXXXXXXXXX/MyMessages', 'ResponseMetadata': {'RequestId': '10fea792-29fa-5484-9bc0-fbb6ad9320b7', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '10fea792-29fa-5484-9bc0-fbb6ad9320b7', 'date': 'Tue, 27 Dec 2022 05:27:05 GMT', 'content-type': 'text/xml', 'content-length': '322'}, 'RetryAttempts': 0}} is not valid for this endpoint.
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 18, in lambda_handler
    sqs.send_message(
  File "/var/runtime/botocore/client.py", line 391, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 719, in _make_api_call
    raise error_class(parsed_response, operation_name)END RequestId: ba1f3c72-6180-4308-ad12-0bc6b02c3793
REPORT RequestId: ba1f3c72-6180-4308-ad12-0bc6b02c3793  Duration: 1108.64 ms    Billed Duration: 1109 ms    Memory Size: 128 MB Max Memory Used: 65 MB  Init Duration: 275.81 ms

CodePudding user response:

Response of sqs.get_queue_url is

{
    'QueueUrl': 'string'
}

So modifying like this will work.

    response = sqs.get_queue_url(QueueName='MyMessages')
    queueurl = response['QueueUrl']
  • Related