我在 AWS 控制台中创建了一个用户,该用户只能访问 Lambda 服务。
我的问题是,使用无服务器框架,在我的 serverless.yaml 中,是否可以向我的用户和可能的任何其他服务添加 S3 完全访问权限? 谢谢。
handler.js
'use strict';
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
module.exports.helloWorld = (event, context, callback) => {
const params = {};
s3.listBuckets(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
const response = {
statusCode: 200,
message: JSON.stringify({message: 'Success!'})
};
callback(null, response);
};
serverless.yaml
provider:
name: aws
runtime: nodejs8.10
region: eu-blah-1
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:ListBucket"
- "s3:PutObject"
- "s3:GetObject"
Resource: "arn:aws:s3:::examplebucket/*"
functions:
helloWorld:
handler: handler.helloWorld
events:
- http:
path: hello-world
method: get
cors: true
最佳答案
如果您指的是您在执行时赋予 Lambda 函数的权限,则在无服务器框架部署后,您可以在 serverless.yaml
中添加角色权限文件,位于 provider
部分。
以下是 Lambda 与 S3 对话、执行其他 Lambda 以及使用 SES 发送电子邮件的权限示例:
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:PutObject"
- "s3:DeleteObject"
- "s3:DeleteObjects"
Resource: arn:aws:s3:::${self:custom.s3WwwBucket}/content/pages/*
- Effect: Allow
Action:
- lambda:InvokeFunction
- lambda:InvokeAsync
Resource: arn:aws:lambda:${self:custom.region}:*:function:${self:service}-${opt:stage}-*
- Effect: "Allow"
Action:
- "ses:SendEmail"
- "ses:SendEmailRaw"
Resource: "arn:aws:ses:eu-west-1:01234567891234:identity/noreply@example.com"
关于node.js - 如何使用无服务器添加 AWS 用户权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52538062/