答えて

1

AWS IAMユーザーはRedshiftデータベースユーザーとは異なります。 Redshiftはpostgresの(非常に遠い)相対関係ですが、まだパスワードなしの接続は許可されていません。

EDIT:

私の答えは、関連するコードスニペットのための他の回答を確認し、もはや適用されます。

+0

これは間違っています。 2018年1月現在、 'get_cluster_credentials'を呼び出してRedshiftクラスタにアクセスするための一時的な資格情報を取得できます。 https://boto3.readthedocs.io/en/latest/reference/services/redshift.html#Redshift.Client.get_cluster_credentials – danielchalef

+0

ありがとうございました。私の回答がもう適用されなくなったことを示す編集を追加しました。私がその時に答えていた時、私はこの機能を見つけられませんでした。どちらかといえば、私は検索がうまくいかず、後でボトットに追加されました。 –

1

AWSは、Redshiftクラスタにアクセスするための一時的な認証情報を要求する方法を提供します。 Boto3はget_cluster_credentialsを実装していますので、次のようなことができます。 instructions hereに従って、IAMユーザーと役割の設定を確認してください。

def db_connection(): 
    logger = logging.getLogger(__name__) 

    RS_PORT = 5439 
    RS_USER = 'myDbUser' 
    DATABASE = 'myDb' 
    CLUSTER_ID = 'myCluster' 
    RS_HOST = 'myClusterHostName' 

    client = boto3.client('redshift') 

    cluster_creds = client.get_cluster_credentials(DbUser=RS_USER, 
               DbName=DATABASE, 
              ClusterIdentifier=CLUSTER_ID, 
               AutoCreate=False) 

    try: 
     conn = psycopg2.connect(
     host=RS_HOST, 
     port=RS_PORT, 
     user=cluster_creds['DbUser'], 
     password=cluster_creds['DbPassword'], 
     database=DATABASE 
    ) 
     return conn 
    except psycopg2.Error: 
     logger.exception('Failed to open database connection.') 
1

彼らはJDBCドライバーのために行うようにAWSがpythonでIAM credsをのための便利なラッパーを提供していません。 GetClusterCredentialsエンドポイントを手動で呼び出し、返されたユーザー名とパスワードをcreate_engineに渡す必要があります。次のようになります。

def get_redshift_credentials(): 
    role_creds = get_role_credentials() 
    client = boto3.client(
     'redshift', 
     region_name=CLUSTER_REGION, 
     aws_access_key_id=role_creds['AccessKeyId'], 
     aws_secret_access_key=role_creds['SecretAccessKey'], 
     aws_session_token=role_creds['SessionToken'], 
    ) 
    response = client.get_cluster_credentials(
     DbUser=PGUSER, 
     ClusterIdentifier=CLUSTER_IDENTIFIER, 
    ) 
    return response 

creds = get_redshift_credentials() 
engine = create_engine('postgresql://{creds.DbUser}:{creds.DbPassword}@hostname:port/database_name'.format(creds)) 
関連する問題