我有一个AWS Lambda
函数,它连接到Jira
webhook。无论何时创建/更改/删除问题,它都会向我的API Gateway
发送请求,该请求会调用我的函数。当我尝试在函数内连接到Redshift时,我收到以下回溯:
Traceback (most recent call last):
File "/var/task/jira_webhook_proc.py", line 171, in lambda_handler
update_item(request, issue, issue_key)
File "/var/task/jira_webhook_proc.py", line 193, in update_item
delete_item(delete_key)
File "/var/task/jira_webhook_proc.py", line 277, in delete_item
port=REDSHIFT_PORT) as conn:
File "/var/task/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
OperationalError: could not translate host name "***" to address: Temporary failure in name resolution
我正在使用预编译的psycopg2
库,可在此处找到https://github.com/jkehler/awslambda-psycopg2。从谷歌搜索似乎这可能是一个PostgreSQL
错误,我没有设置正确的配置文件来收听所有端口。我不知道如何改变它。
答案 0 :(得分:1)
我使用下面添加的代码来连接lambda函数的redshift。使用psycopg2
conn_string = "dbname='your_db_name' port='5439' user='redshift_user' password='^%+^+&7!+' host='xxxxxxx.yyyyyyy.eu-west-1.redshift.amazonaws.com'"
conn = psycopg.connect(conn_string)
cursor = conn.cursor()
cursor.execute("COPY data_table ........ ")
conn.commit()
cursor.close()
conn.close()
答案 1 :(得分:0)
它可以帮助某人,因为在我的情况下它可以很好地工作。
import psycopg2
import od_red_load_cred as CONSTANTS
def main():
# Amazon Redshift connect string
try:
conn_string = "dbname={} port={} user={} password={} host={}"\
.format(CONSTANTS.DBNAME, CONSTANTS.PORT, CONSTANTS.USER,
CONSTANTS.PASSWORD, CONSTANTS.HOST)
print("Connect string Successful!")
except:
print("Unable to create the connect string")
# query generate
try:
sql="""copy {}.{} from '{}'\
credentials 'aws_access_key_id={};aws_secret_access_key={}' \
DELIMITER ',' ACCEPTINVCHARS EMPTYASNULL ESCAPE COMPUPDATE OFF;commit;"""\
.format(CONSTANTS.SCHEMA, CONSTANTS.TABLE_NAME_BV, CONSTANTS.S3_PATH_BV,
CONSTANTS.ACCESS_KEY_ID, CONSTANTS.SECRET_ACCESS_KEY)
print("sql ==>", sql)
print("Query creation Successful!")
print(" ")
except:
print("Unable to create the query")
# get connection
try:
con = psycopg2.connect(conn_string);
print("con ==>", con)
print("Connection Successful!")
except Exception as e:
print("Unable to connect to Redshift")
print(e)
exit(-1)
# get cursor connection
try:
cur = con.cursor()
except Exception as e:
print("Unable to get the cursor from connection of Redshift")
print(e)
exit(-1)
# execute cursor connection
try:
cur.execute(sql)
print("Query executed successfully")
except Exception as e:
print("Failed to execute the query")
print(e)
exit(-1)
# close connection
try:
con.close()
print("Connection closed Successfully!")
except Exception as e:
print("Unable to close connection to Redshift")
print(e)
print(" ")
# Main method
if __name__ == '__main__':
print("start: __main__ ")
main()
print("end: __main__ ")