Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Run aws Athena query by Lambda: error name 'response' is not defined

I create an AWS lambda function with python 3.9 to run the Athena query and get the query result

import time
import boto3

# create Athena client

client = boto3.client('athena')

# create Athena query varuable

query = 'select * from mydatabase.mytable limit 8'
DATABASE = 'mydatabase'
output='s3://mybucket/'

def lambda_handler(event, context):
    # Execution
    response = client.start_query_execution(
        QueryString=query,
        QueryExecutionContext={
            'Database': DATABASE
        },
        ResultConfiguration={
            'OutputLocation': output,
        }
    )
    
query_execution_id = response['QueryExecutionId']
    
time.sleep(10)
    
result = client.get_query_results(QueryExecutionId=query_execution_id)
    
for row in results['ResultSet']['Rows']:
    print(row)

I get this error message when I test it "[ERROR] NameError: name ‘response’ is not defined"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You define the response variable inside the lambda_handler function. But you are referencing it in the global scope, outside of that function, here:

query_execution_id = response['QueryExecutionId']

The variable isn’t defined on that scope, thus the error message. It appears that you may simply be missing indentation on all these lines:

query_execution_id = response['QueryExecutionId']
    
time.sleep(10)
    
result = client.get_query_results(QueryExecutionId=query_execution_id)
    
for row in results['ResultSet']['Rows']:
    print(row)

In Python indentation is syntax! If you intend those lines to be part of the lambda_handler function, then they need to have the correct indentation to place them inside the scope of the function, like so:

def lambda_handler(event, context):
    # Execution
    response = client.start_query_execution(
        QueryString=query,
        QueryExecutionContext={
            'Database': DATABASE
        },
        ResultConfiguration={
            'OutputLocation': output,
        }
    )
    
    query_execution_id = response['QueryExecutionId']
    
    time.sleep(10)
    
    result = client.get_query_results(QueryExecutionId=query_execution_id)
    
    for row in results['ResultSet']['Rows']:
        print(row)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading