配置单个aws cli操作以调用api网关方法的插件
awscli-plugin-execute-api的Python项目详细描述
awscli插件执行api
配置单个aws cli操作以调用api网关/lambda方法的插件
快速启动
使用pip
安装$ pip install awscli-plugin-execute-api
如果您在自制程序中安装了awscli
,请使用其捆绑的python:
$ /usr/local/opt/awscli/libexec/bin/pip install awscli-plugin-execute-api
注册插件
$ aws configure set plugins.execute-api awscli_plugin_execute_api
配置操作终结点
首先使用execute-api:Invoke
权限为iam配置配置文件:
$ exportAWS_PROFILE=myprofile $ aws configure AWS Access Key ID [None]: AKIA... AWS Secret Access Key [None]: pWim... Default region name [None]: us-east-1 Default output format [None]: json
接下来,为操作及其api网关调用url配置插件:
$ aws configure set dynamodb.update-table https://m303r7o808.execute-api.us-east-1.amazonaws.com/Prod/update-table
<详细信息>[plugins] execute-api = awscli_plugin_execute_api [profile myprofile] region = us-east-1 output = json dynamodb = update-table = https://m303r7o808.execute-api.us-east-1.amazonaws.com/Prod/update-table详细信息>
运行操作
现在运行命令:
$ aws dynamodb update-table --table-name $TABLE_NAME --sse-specification Enabled=false An error occurred (ValidationException) when calling the UpdateTable operation: Custom error from Lambda handler<详细信息>
$ aws dynamodb update-table --debug --table-name $TABLE_NAME Plugin awscli_plugin_execute_api: Config [myprofile] dynamodb.update-table => URL https://m303r7o808.execute-api.us-east-1.amazonaws.com/Prod/update-table Plugin awscli_plugin_execute_api: renamed X-Amz-Target DynamoDB_20120810.UpdateTable $ aws dynamodb describe-table --debug --table-name $TABLE_NAME Plugin awscli_plugin_execute_api: Config [myprofile] dynamodb.describe-table not found详细信息>
动机
出于安全原因,我们希望让iam用户访问aws api调用功能的limited subset。例如,在dynamodb update-table
操作中,我们希望允许用户修改:
- 全局索引
但不允许他们修改:
- 提供的吞吐量
- 流
- 服务器端加密
“无服务器”解决方案包括:
IAM用户
- 禁止
dynamodb:UpdateTable
- 允许在API网关资源上
execute-api:Invoke
的策略 - 访问键
- 禁止
API网关方法
- 发送
update-table
请求的URL - AWS U IAM授权
- 发送
lambda函数
- 允许
dynamodb:UpdateTable
- 验证并执行
update-table
请求的代码
- 允许
aws cli
dynamodb update-table
命令的自定义终结点url- API网关的请求签名
此插件为单个cli操作注册一个url并自动使用它。
$ aws configure set dynamodb.update-table https://m303r7o808.execute-api.us-east-1.amazonaws.com/Prod/update-table $ aws dynamodb update-table --table-name $TABLE_NAME --sse-specification Enabled=false An error occurred (ValidationException) when calling the UpdateTable operation: Modifying SSESpecification is not allowed
故障排除
IAM
cli用户需要API execution permissions:
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["execute-api:Invoke"],"Resource":["arn:aws:execute-api:us-east-1:303718836660:m303r7o808/*/POST/update-table"]}]}
lambda
cli操作位于X-Target
头中,因为X-Amz-Target