在假定的aws iam角色的上下文中启动程序的cli
iam-starter的Python项目详细描述
一个cli,它使用 通过担任一个角色或一个配置文件来创造环境
安装
$ pip install iam-starter
用法
几个例子:
# start a program given an IAM Role (assumes default creds can assume role) $ iam-starter --role myrole --command ./my-program.sh # start a program with a profile $ iam-starter --profile myprofile --command ./my-program.sh # start a program with an IAM Role using a profile to assume that role $ iam-starter --role myrole --profile dev --command ./my-program.sh # get export commands to paste into shell to assume the role manually $ iam-starter --role myrole exportAWS_ACCESS_KEY_ID="ASIAI....UOCA"exportAWS_SECRET_ACCESS_KEY="DWYnQ....h93k"exportAWS_SESSION_TOKEN="KMWSz...8wX=="# note that these two are equivalent: $ iam-starter --profile myprofile --command aws s3 ls $ aws s3 ls --profile myprofile # you can use iamx as shorthand for iam-starter (less typing) $ iamx --role myrole --command aws s3 ls
动机
希望能够轻松运行需要aws的程序 在本地使用凭证的方式与在aws中运行凭证的方式相同 不需要代码更改或复杂逻辑来支持多个 获取AWS证书的方法。在ec2上,只需附加一个iam 角色到实例,然后在该实例上执行的任何操作都是 在该IAM角色的上下文中自动执行。在当地还不如 在角色的上下文中容易运行的东西,必须设置aws 配置文件(已经是你在aws中没有做的事情)来承担你的角色, 然后你的程序必须支持在 向aws认证,这并不总是一个选项(不是所有工具 正确/完全支持使用AWS配置文件)。
如果你想用角色的上下文而不是 使用完整的管理员和开发人员凭据运行所有内容 等待在生产中发现您具有IAM权限 问题,您可能正在使用命名配置文件 (aws configure --profile profilename)。但是在运行应用程序时 为了让aws api调用使用它所需要的配置文件 代码更改以指定aws sdk的配置文件名。听起来不错 首先,直到您意识到这些配置文件将不存在时 部署到aws中,因此“在您的机器上工作”的代码在 生产。
如果你还没有放弃做一个好公民 使用为其定义的有限IAM策略在本地运行/测试应用程序 它,这导致你不得不做像这样的不幸的事情 您的代码:
# how annoying that I have to pass an optional profile name heredefdo_something_with_aws(profile=None)ifprofile:# this is what will happen on my laptopsession=boto3.Session(profile_name=profile)client=session.client('s3')else:# this is what will happen when running on EC2client=boto3.client('s3')
(一个python示例,但对于任何语言或 aws cli)
iam starter使您可以很容易地使用角色在本地和aws中运行 或命名配置文件,通过相同的凭据方法-环境 变量,这是最普遍支持的凭据方法。
defdo_something_with_aws()# yay, simple and the same everywhere!client=boto3.client('s3')
与Docker一起使用
主要用于Docker外部。管理码头工人 具有假定iam角色的容器,使用 IAM-Docker-Run。
与SSM起动机一起使用
这可以用链子 SSM-Starter。这个 以下示例使用给定的iam角色启动程序并加载 给定环境路径的SSM参数,然后运行 现在具有iam角色和配置优点的程序 加载到环境中。
$ exportAWS_REGION=us-east-1 # needed for ssm-starter $ iam-starter --role myrole --profile dev --command ssm-starter --ssm-name /myssmprefix/ --command ./my-program.sh
限制
这是一个开发工作流工具,不是为运行生产而设计的 应用程序。
临时凭据将在1小时内过期,并且不会自动续订。这个 工具设计用于开发、即席或witih build/ci 执行时间较短的环境。
将更新发布到pypi
对于维护者-发布ssm search的更新版本, 在version.py中增加版本号并运行以下命令:
docker build -f ./Dockerfile.buildenv -t iam-starter:build . docker run --rm -it --entrypoint make iam-starter:build publish
在提示中,输入pypi.org repo的用户名和密码。
测试
通过插入本地aws凭据在容器中执行测试 进入容器。
docker build -f ./Dockerfile.buildenv -t iam-starter:build . docker run -it -v $(cd ~/.aws;pwd):/root/.aws iam-starter:build # then, inside the container pip install awscli # assumes a local profile named "dev" which has access to list S3 buckets iam-starter --profile dev --command aws s3 ls