SQS get_queue_by_name can't find the queue in AWS Lambda

Viewed 3846

I am trying to write to an SQS queue from AWS lambda like below.

sqs = boto3.resource(
        'sqs',
        region_name='us-east-1'
    )

def lambda_handler(event, context):
    queue_name = event["queue_name"]
    sqsQ = sqs.get_queue_by_name(QueueName=queue_name)
    msg_body = {
            "source": "some_source",
            "mse": 120
        }
    msg = sqsQ.send_message(MessageBody=json.dumps(msg_body), MessageGroupId="some_id", MessageDeduplicationId=str(uuid.uuid4()))

But I am getting queue nonexistence error

"errorMessage": "An error occurred (AWS.SimpleQueueService.NonExistentQueue) when calling the GetQueueUrl operation: The specified queue does not exist or you do not have access to it.",
  "errorType": "QueueDoesNotExist",

I made sure the queue name is correct (copy pasted) and it is the right region.

What am I missing?

1 Answers
  • Worth checking Lambda has the following permissions to manage messages in your Amazon SQS queue. Add them to your function's execution role. sqs:ReceiveMessage, sqs:DeleteMessage, sqs:GetQueueAttributes.aws docs
def get_msg_from_sqs():
    sqs_client = boto3.client('sqs')

    print("--- Getting message from SQS")
    response = sqs_client.receive_message(
        QueueUrl="https://sqs.eu-west-2.amazonaws.com/0000000000/manual_test_to_mesage_sender_queue",
        MaxNumberOfMessages=10,
        WaitTimeSeconds=10
    )

    print("--- Full Response:")
    print(response)

    messages = response.get('Messages')
    for message in messages:
        body = message.get('Body')
        json_body = json.loads(body)
        message = json_body.get('Message')
        json_msg = json.loads(message)

        print("--- Message:")
        print(json_msg.get('number'))
        print(json_msg.get('session_id'))
Related