使用无服务器框架时如何从 AWS Lambda 访问 DynamoDB? [英] How to access DynamoDB from AWS Lambda when using the Serverless Framework?

查看:15
本文介绍了使用无服务器框架时如何从 AWS Lambda 访问 DynamoDB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 无服务器框架 来管理我的 AWS Lambda 部署.框架凭证可以访问 DynamoDB 资源,但与框架一起部署的 Lambda 无法访问我的 DynamoDB 表.

I'm using the Serverless Framework to manage my AWS Lambda deploys. The framework credentials has access to DynamoDB resources, but my Lambda, deployed with the framework, can't access my DynamoDB tables.

如何为我的 Lambda 函数提供适当的访问权限?

How can I give my Lambda functions the proper access?

推荐答案

编辑:更新了 Serverless Framework 1.x 的答案.

EDIT: updated the answer for Serverless Framework 1.x.

解决方案是设置 iamRoleStatements 以允许 Lambda 访问 DynamoDB 资源.注意:无服务器框架使用的凭据必须具有相同 DynamoDB 资源的权限.

The solution is to set the iamRoleStatements to allow Lambda to access the DynamoDB resources. Note: the credentials used by the Serverless Framework must have permission to the same DynamoDB resources.

  1. 在您的 serverless.yml 中添加 iamRoleStatements:

provider:
  name: aws
  runtime: nodejs4.3
  stage: dev
  region: us-east-1
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "dynamodb:*"
      Resource: "arn:aws:dynamodb:${self:provider.region}:*:table/*"

  • 部署更改:

  • deploy the changes:

    > serverless deploy
    

  • 要在函数级别授予权限(而不是允许所有函数访问 DynamoDB),请参阅我的其他答案此处.

    To give permissions in a function level (instead of allowing all functions to access DynamoDB), see my other answer here.

    这篇关于使用无服务器框架时如何从 AWS Lambda 访问 DynamoDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆