[英]Issue while executing Python script on AWS Lambda function
I tried running the below script on AWS Lambda, but unfortunately, it's not executing successfully.我尝试在 AWS Lambda 上运行以下脚本,但不幸的是,它没有成功执行。 Can anyone help me to get this fix or correct me if there is an issue with the script which needs some change executing it from Lambda?
如果脚本有问题需要从 Lambda 执行一些更改,任何人都可以帮助我修复或纠正我吗?
#!/usr/bin/env python3
import boto3
client = boto3.client('athena')
def run_query(query, database, s3_output):
response = client.start_query_execution(
QueryString=query,
QueryExecutionContext={
'Database': 'database'
},
ResultConfiguration={
'OutputLocation': s3_output,
}
)
print('Execution ID: ' + response['QueryExecutionId'])
return response
#Athena configuration
s3_input = 's3://smathena/cf-ant-prod/'
s3_ouput = 's3://smathena/athenatest/'
database = 's3_accesslog'
table = 'test_output1'
#Athena database and table definition
create_database = "CREATE DATABASE IF NOT EXISTS %s;" % (database)
delete_table = "drop table %s.%s;" % ( database, table )
create_table = \
"""CREATE EXTERNAL TABLE IF NOT EXISTS %s.%s (
`Date` DATE,
ScContentLen BIGINT,
ScRangeStart BIGINT,
ScRangeEnd BIGINT
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
LOCATION '%s'
TBLPROPERTIES ('skip.header.line.count' = '2');""" % ( database, table, s3_input )
#Query definitions
query_1 = "SELECT * FROM %s.%s where CAST(status AS VARCHAR) like '404';" % (database, table)
#Execute all queries
queries = [ create_database, delete_table, create_table, query_1 ]
for q in queries:
print("Executing query: %s" % (q))
res = 'run_query(q, database, s3_ouput)'
Error while testing on AWS Lambda:在 AWS Lambda 上测试时出错:
Response:
{
"errorMessage": "run_query() missing 1 required positional argument: 's3_output'",
"errorType": "TypeError",
"stackTrace": [
" File \"/var/runtime/bootstrap.py\", line 131, in handle_event_request\n response = request_handler(event, lambda_context)\n"
]
}
Request ID: "2cb2175c-8838-470d-a8dd-efdf4c051312"请求 ID:“2cb2175c-8838-470d-a8dd-efdf4c051312”
Function logs: START RequestId: 2cb2175c-8838-470d-a8dd-efdf4c051312 Version: $LATEST [ERROR] TypeError: run_query() missing 1 required positional argument: 's3_output' Traceback (most recent call last): File "/var/runtime/bootstrap.py", line 131, in handle_event_request response = request_handler(event, lambda_context) END RequestId: 2cb2175c-8838-470d-a8dd-efdf4c051312函数日志:START RequestId:2cb2175c-8838-470d-a8dd-efdf4c051312 版本:$LATEST [ERROR] TypeError:run_query() 缺少 1 个必需的位置参数:'s3_output' Traceback(最近一次调用):文件“/var/runtime /bootstrap.py", line 131, in handle_event_request response = request_handler(event, lambda_context) END RequestId: 2cb2175c-8838-470d-a8dd-efdf4c051312
import boto3
client = boto3.client('athena')
def run_query(event, context):
response = client.start_query_execution(
QueryString=query,
QueryExecutionContext={
'Database': 'database'
},
ResultConfiguration={
'OutputLocation': s3_output,
}
)
print('Execution ID: ' + response['QueryExecutionId'])
return event
Getting the below error:得到以下错误:
START RequestId: 55dbf703-f30c-4106-8873-c685f3d06e4d Version: $LATEST [ERROR] NameError: name 's3_output' is not defined Traceback (most recent call last): File "/var/task/lambda_function.py", line 12, in run_query 'OutputLocation': s3_output, END RequestId: 55dbf703-f30c-4106-8873-c685f3d06e4d REPORT RequestId: 55dbf703-f30c-4106-8873-c685f3d06e4d Duration: 14.30 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 75 MB Init Duration: 649.66 ms START RequestId: 55dbf703-f30c-4106-8873-c685f3d06e4d Version: $LATEST [ERROR] NameError: name 's3_output' is not defined 回溯(最近一次调用):文件“/var/task/lambda_function.py”,第 12 行, 在 run_query 'OutputLocation': s3_output, END RequestId: 55dbf703-f30c-4106-8873-c685f3d06e4d REPORT RequestId: 55dbf703-f30c-4106-8873-c685f3d20ms 最大内存大小:D100ms D10300ms :75 MB 初始化持续时间:649.66 毫秒
This is the same problem as in your earlier question: Error while running a python script on AWS Lambda - Stack Overflow这与您之前的问题中的问题相同: 在 AWS Lambda 上运行 python 脚本时出错 - 堆栈内存溢出
An AWS Lambda function has this format: AWS Lambda 函数具有以下格式:
import boto3
def lambda_handler(event, context):
print(event)
print(context)
When the Lambda function is invoked, it calls the lambda_handler()
function.当 Lambda 函数被调用时,它会调用
lambda_handler()
函数。 The name of this function can be changed if desired, but it will always receive those two incoming parameters: event
and context
如果需要,可以更改此函数的名称,但它将始终接收这两个传入参数:
event
和context
Depending upon how the Lambda function is invoked, the contents of the event
contains information that is "passed into" the Lambda function.根据调用 Lambda 函数的方式,
event
的内容包含“传递到”Lambda 函数的信息。 For example:例如:
It appears that you are wanting to use three values in the function: query, database, s3_output
看来您想在函数中使用三个值:
query, database, s3_output
These values will need to be passed to the Lambda function through whatever means the Lambda function is being invoked.这些值需要通过调用 Lambda 函数的任何方式传递给 Lambda 函数。 The function can then retrieve those values via the
event
parameter.然后该函数可以通过
event
参数检索这些值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.