Amit kumar
Amit kumar

Reputation: 2724

Unable to run AWS lambda function

I have configured SQS to serve as an event source to trigger Lambda.

Lambda function(Running inside VPC) code :

import boto3

def lambda_handler(event, context):

    sqs = boto3.client ('sqs')
    print (event)


response = sqs.send_message (
    QueueUrl='https://sqs.ap-south-1.amazonaws.com/07xx0801xxxxx/test-queue',
    DelaySeconds=10,
    MessageAttributes={
        'Title': {
            'DataType': 'String',
            'StringValue': 'Test'
        },
    },
    MessageBody=(
        'This is a test message'
    )
)

print(response['MessageId'])

It throws module initialization error: name 'sqs' is not defined when I am trying to test the lambda function.

Response:
{
  "errorMessage": "module initialization error"
}

I am unable to find out what's wrong with the function. If anyone had faced similar issue in past or knows how to get this fixed, Please help me out.

Thanks a ton in advance!

Upvotes: 0

Views: 192

Answers (1)

Mayank Raj
Mayank Raj

Reputation: 1624

If the code you have provided is actually the code you have in your lambda function, correct indentation would solve the problem

import boto3

def lambda_handler(event, context):

    sqs = boto3.client ('sqs')
    print (event)

    response = sqs.send_message (
        QueueUrl='https://sqs.ap-south-1.amazonaws.com/07xx0801xxxxx/test-queue',
        DelaySeconds=10,
        MessageAttributes={
            'Title': {
                'DataType': 'String',
                'StringValue': 'Test'
            },
        },
        MessageBody=(
            'This is a test message'
        )
    )

    print(response['MessageId'])

Upvotes: 2

Related Questions