如何在 AWS Datapipeline 的 Python 脚本中提供 Redshift 数据库密码?

How to provide Redshift Database Password in Python Script in AWS Datapipeline?

我正在使用 Redshift,必须编写一些自定义脚本来生成报告。我正在为 运行 我的自定义逻辑使用 AWS datapipeline CustomShellActivity。我正在使用 python 和 boto3。我想知道什么是最安全的方法,实际上是在 python 脚本中提供数据库密码的最佳做法。我确信在脚本中硬编码密码不是好的做法。我还有哪些其他选择或应该探索哪些其他选择?

一个非常标准的方法是 store credentials in a secure S3 bucket and download them as part of the deployment/launch process using an IAM role with access to the secure bucket. For limited runtime cases like lambda or datapipeline you could download from S3 to an in memory buffer using boto.Key.get_contents_as_string() 在启动时解析文件并设置您的凭据。

为了提高安全性,您可以合并 KMS secret management. Here is an example 将两者结合起来。

我通常将它们存储为环境变量。我不确定 AWS 数据管道部署,但在标准 Linux 框 (EC2) 上,您可以:

# ~/.profile or /etc/profile
export MY_VAR="my_value"

然后您可以像这样在 Python 中访问它们:

# python script
import os
my_var_value = os.environ['MY_VAR'] if 'MY_VAR' in os.environ else 'default'