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

Time:12-02

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"

CodePudding user response:

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)
  • Related