如何使用 boto3 从 Lambda 调用 Redshift 存储过程?
How to call a Redshift stored procedure from Lambda using boto3?
因此,我尝试从 AWS Lambda 调用 Redshift 存储过程,但没有成功。如果我编辑 sql_text 参数以明确执行此操作,我可以获得 Lambda 函数来创建和删除表,但我无法让它在 Redshift 上执行我的过程。关于我在这里遗漏的任何想法?
import boto3
import json
def lambda_handler(event, context):
# initiate redshift-data client in boto3
client = boto3.client("redshift-data")
# parameters for running execute_statement
secretArn = 'arn:aws:secretsmanager:us-north-1:234567890123:secret:supersecret-dont-tell-a-soul'
redshift_database = 'dbase'
redshift_user = 'admin_user'
sql_text = 'call public.myproc(''somerandomvalue'')'
redshift_cluster_id = 'the-redshift-cluster'
print("Executing: {}".format(sql_text))
response = client.execute_statement(SecretArn = secretArn, Database=redshift_database, Sql=sql_text,ClusterIdentifier=redshift_cluster_id)
return {'statusCode': 200,'body': json.dumps('Lambdone!')}
这是我得到 Lambda 的响应
{
'ClusterIdentifier': 'my-data-warehouse'
, 'CreatedAt': datetime.datetime(2021, 7, 9, 18, 1, 59, 34000, tzinfo=tzlocal())
, 'Database': 'bidev', 'Id': '011f00d0-bf9e-45cd-bb12-b045c9504c0b'
, 'SecretArn': 'arn:aws:secretsmanager:us-north-1:234567890123:secret:redshiftqueryeditor-super-duper-secret-secret-keeper'
, 'ResponseMetadata':
{
'RequestId': 'b2b867f0-d20c-4f2b-8e14-64942176bd6e'
, 'HTTPStatusCode': 200
, 'HTTPHeaders':
{
'x-amzn-requestid': 'b2b867f0-d20c-4f2b-8e14-64942176bd6e'
, 'content-type': 'application/x-amz-json-1.1'
, 'content-length': '270', 'date': 'Fri, 09 Jul 2021 18:01:59 GMT'}
, 'RetryAttempts': 0
}
}
尝试更改您的 sql 文本,使字符串被双引号括起来,然后其他内容只有单引号。
"调用 public.myproc('somerandomvalue')"
因此,我尝试从 AWS Lambda 调用 Redshift 存储过程,但没有成功。如果我编辑 sql_text 参数以明确执行此操作,我可以获得 Lambda 函数来创建和删除表,但我无法让它在 Redshift 上执行我的过程。关于我在这里遗漏的任何想法?
import boto3
import json
def lambda_handler(event, context):
# initiate redshift-data client in boto3
client = boto3.client("redshift-data")
# parameters for running execute_statement
secretArn = 'arn:aws:secretsmanager:us-north-1:234567890123:secret:supersecret-dont-tell-a-soul'
redshift_database = 'dbase'
redshift_user = 'admin_user'
sql_text = 'call public.myproc(''somerandomvalue'')'
redshift_cluster_id = 'the-redshift-cluster'
print("Executing: {}".format(sql_text))
response = client.execute_statement(SecretArn = secretArn, Database=redshift_database, Sql=sql_text,ClusterIdentifier=redshift_cluster_id)
return {'statusCode': 200,'body': json.dumps('Lambdone!')}
这是我得到 Lambda 的响应
{
'ClusterIdentifier': 'my-data-warehouse'
, 'CreatedAt': datetime.datetime(2021, 7, 9, 18, 1, 59, 34000, tzinfo=tzlocal())
, 'Database': 'bidev', 'Id': '011f00d0-bf9e-45cd-bb12-b045c9504c0b'
, 'SecretArn': 'arn:aws:secretsmanager:us-north-1:234567890123:secret:redshiftqueryeditor-super-duper-secret-secret-keeper'
, 'ResponseMetadata':
{
'RequestId': 'b2b867f0-d20c-4f2b-8e14-64942176bd6e'
, 'HTTPStatusCode': 200
, 'HTTPHeaders':
{
'x-amzn-requestid': 'b2b867f0-d20c-4f2b-8e14-64942176bd6e'
, 'content-type': 'application/x-amz-json-1.1'
, 'content-length': '270', 'date': 'Fri, 09 Jul 2021 18:01:59 GMT'}
, 'RetryAttempts': 0
}
}
尝试更改您的 sql 文本,使字符串被双引号括起来,然后其他内容只有单引号。
"调用 public.myproc('somerandomvalue')"