Can i execute an Athena saved query from lambda?

0

I have a saved query in Athena, that i want to run from Lambda.

As per: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Athena.html It seems that the SDK can only execute a query if you provide the SQL. (needs to be node.js in lambda)

Is there a way to: var params = { QueryName: '<saved query name>, /* required */ },

athena.startQueryExecution(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response });

MODERATOR
asked 5 years ago2340 views
2 Answers
1
Accepted Answer

I generally use the following steps.

  • get all query ID's
  • get the query details for each query ID
  • check if its the right one & do something

Code:

Get Athena client/object

client = boto3.client('athena')

# Get all the saved queries in Athena
response = client.list_named_queries()

# Get the named query IDs from the response
named_query_IDs = response['NamedQueryIds']

# Go through all the query ID, to find the delete & create queries we need to run
for query_ID in named_query_IDs: 

    # Get all the details of a named query using its ID
    named_query = client.get_named_query(
        NamedQueryId=query_ID
    )
    
    # Get the query string & query name of the query
    querystring = named_query['NamedQuery']['QueryString']
    queryname = named_query['NamedQuery']['Name']
    
    # If its a create query, add it to the list of create queries
    # We also replace the '/subfolder' string in the query with the folder structure for the current month
    if 'create_linked_' in queryname:
        new_query = querystring.replace('/subfolder', currentmonth)
        create_query_strings.append(new_query)
        
    # If its a delete query, add it to the list of delete queries to execute later
    if 'delete_linked_' in queryname:
        delete_query_strings.append(querystring)
MODERATOR
answered 5 years ago
1

Please check out the section from ** AWS Well-Architected Labs** ,

CREATE LAMBDA FUNCTION TO RUN THE SAVED QUERIES

https://www.wellarchitectedlabs.com/cost/300_labs/300_splitting_sharing_cur_access/4_lambda_function/

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions