[英]AWS Lambda Python/Boto3/psycopg2 Redshift temporary credentials
I'm pretty new to AWS so please let me know if what I'm trying to do is not a good idea, but the basic gist of it is that I have a Redshift cluster that I want to be able to query from Lambda (Python) using a combination of psycopg2 and boto3. 我对AWS来说还很陌生,所以请告诉我我想做的事不是一个好主意,但是其基本要点是我有一个Redshift集群,我希望能够从Lambda进行查询( Python)结合使用psycopg2和boto3。 I have assigned the Lambda function a role that allows it to get temporary credentials (get_cluster_credentials) from Redshift. 我为Lambda函数分配了一个角色,该角色允许其从Redshift获取临时凭证(get_cluster_credentials)。 I then use psycopg2 to pass those temporary credentials to create a connection. 然后,我使用psycopg2传递那些临时凭据来创建连接。 This works fine when I run interactively from my Python console locally, but I get the error: 当我从本地的Python控制台以交互方式运行时,此方法工作正常,但出现错误:
OperationalError: FATAL: password authentication failed for user "IAMA:temp_user_cred:vbpread" OperationalError:致命:用户“ IAMA:temp_user_cred:vbpread”的密码身份验证失败
If I use the temporary credentials that Lambda produces directly in a connection statement from my python console they actually work (until expired). 如果我使用Lambda直接在我的python控制台的连接语句中生成的临时凭证,它们将实际起作用(直到过期)。 I think I'm missing something obvious. 我想我缺少明显的东西。 My code is: 我的代码是:
import boto3
import psycopg2
print('Loading function')
def lambda_handler(event, context):
client = boto3.client('redshift')
dbname = 'medsynpuf'
dbuser = 'temp_user_cred'
response = client.describe_clusters(ClusterIdentifier=dbname)
pwresp = client.get_cluster_credentials(DbUser=dbuser,DbName=dbname,ClusterIdentifer=dbname,DurationSeconds=3600,AutoCreate=True, DbGroups=['vbpread'])
dbpw = pwresp['DbPassword']
dbusr = pwresp['DbUser']
endpoint = response['Clusters'][0]['Endpoint']['Address']
print(dbpw)
print(dbusr)
print(endpoint)
con = psycopg2.connect(dbname=dbname, host=endpoint, port='5439', user=dbusr, password=dbpw)
cur = con.cursor()
query1 = open("001_copd_yearly_count.sql","r")
cur.execute(query1.read())
query1_results = cur.fetchall()
result = query1_results
return result
I'm using Python 3.6. 我正在使用Python 3.6。
Thanks! 谢谢! Gerry 格里
I was using a Windows compiled version of psycopg2 and needed Linux. 我使用的是Windows的psycopg2编译版本,并且需要Linux。 Swapped it out for the one here: https://github.com/jkehler/awslambda-psycopg2 在这里将其交换出来: https : //github.com/jkehler/awslambda-psycopg2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.