API网关部署资源上DELETE操作的受限IAM策略



我已经创建了一个无服务器用户,该用户具有创建和部署AWS lambda函数以及API Gateway所需的权限。

然而,当我改变一段代码并执行serverless deploy时,它给出了错误:

Error:
DELETE_FAILED:... "User: arn:aws:iam::XXXXXXXXXXX:user/serverless is not authorized to perform: apigateway:DELETE on resource: arn:aws:apigateway:us-east-1::/restapis/1zhmt1r45r2/deployments/27gb11 because no identity-based policy allows the apigateway:DELETE action (Service: ApiGateway, Status Code: 403...

现在我可以在网关资源上添加DELETE操作权限。允许在生产环境中删除任何apigateway资源是非常危险的。我想要的是限制这个无服务器/编程用户可以删除的内容(只有自己创建的api,或者资源名的前缀)。

我已经创建了策略,只允许serverless-user创建具有特定前缀的资源,但是由于此错误显示资源arn:aws:apigateway:us-east-1::/restapis/1zhmt1r45r2/deployments/27gb11上的错误,它使用id(1zhmt1r45r2/deployments/27gb11)。我想不出一个有效的方法来限制这个用户可以删除的内容。

有什么变通办法吗?我急需解决这个问题。

如果没有更多的细节,我不确定您真正想要实现什么,但是您的错误是告诉您需要使用仅限制于lambda函数的策略授予apigateway:DELETE权限给lambda函数。最简单的方法是在SAM模板中指定它。通过这种方式,您可以实现IaC(基础设施即代码),而无需手动处理,并允许您自动执行重新部署和将模板添加到源代码控制的过程。下面的例子定义了一个lambda函数,它需要apigateway:DELETE权限

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
role/AmazonAPIGatewayPushToCloudWatchLogs"
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
CreateLedger:
Type: AWS::Serverless::Function
Properties:
FunctionName: CreateLedgerFunction
Handler: QLDB.API.Config.Lambda::QLDB.API.Config.Lambda.Functions.CreateLedger.CreateLedgerFunction::FunctionHandler
Runtime: dotnetcore3.1
Timeout: 30
CodeUri: .
Policies:
- Statement:
- Sid: QLDBCreateLedgerCommandPolicy
Effect: Allow
Action:
- apigateway:DELETE #<----- specify the permissions
Resource: '*'
Events:
CreateLedger:
Type: Api
Properties:
Path: /createLedger
Method: post
RestApiId:
Ref: ApiGatewayApi

最新更新