
[英]Deploying AWS Lambda "Hello World" using sam init, sam build sam deploy defaults fails with Failed to create changeset for sam-app
[英]Limiting Lambda Permissions using AWS SAM and DynamoDB
我一直在使用 Cloudformation/SAM 将堆栈放在一起,结果为 go。 下面的 lambda function 是在 DynamoDB 中创建联系人的处理程序,因此我认为为它提供最少的必要权限是明智的。
我注意到 DynamoDBWritePolicy 不仅允许 PutItem,所以我想知道 go 进一步锁定事物的最佳方法是什么。 我明白这可能有点矫枉过正,但这是为了学习目的。
这是我的 Lambda 定义:
CreateContact:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
Policies:
- AWSLambdaBasicExecutionRole
- DynamoDBWritePolicy:
TableName: contacts
CodeUri: ./src/handlers/create-contact
Events:
ApiEvent:
Type: Api
Properties:
Path: /
Method: post
RestApiId:
Ref: APIGateway
所以我的问题是:我应该只为 PutItem 创建一个新策略还是依赖于权限边界之类的东西,或者我是否忽略了任何其他选项?
你的方法没有错。 您可以创建新策略以“允许”对特定资源执行某些特定操作。
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "SpecificTable",
"Effect": "Allow",
"Action": [
"dynamodb:PutItem"
],
"Resource": "arn:aws:dynamodb:*:*:table/MyTable"
}
]
}
是的,SAM 提供的预定义策略模板可能包含比您 lambda function 所需的权限更多的权限。如果您需要更精细级别的权限(这是最佳实践),您始终可以在 lambda 函数的策略 object 中的 SAM 模板中设置它们作为如下:
CreateContact:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
Policies:
- Version: 2012-10-17
Statement:
- Effect: Allow
Action: dynamodb:PutItem
Resource: !Sub "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/contacts"
CodeUri: ./src/handlers/create-contact
Events:
ApiEvent:
Type: Api
Properties:
Path: /
Method: post
RestApiId:
Ref: APIGateway
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.