gpt4 book ai didi

amazon-web-services - 为什么我的Elastic Beanstalk应用程序尽管具有角色,但仍然拒绝PutItem访问我的DynamoDB?

转载 作者:行者123 更新时间:2023-12-04 05:36:51 26 4
gpt4 key购买 nike

目标

我想以编程方式从Elastic Beanstalk应用程序将项目添加到DynamoDB中的表中,并使用类似于以下代码的代码:

Item item = new Item()
.withPrimaryKey(UserIdAttributeName, userId)
.withString(UserNameAttributeName, userName);

table.putItem(item);

意外结果

日志显示以下错误消息,其中 [粗体] 是我的编辑:

User: arn:aws:sts::[iam id?]:assumed-role/aws-elasticbeanstalk-ec2-role/i-[some number] is not authorized to perform: dynamodb:PutItem on resource: arn:aws:dynamodb:us-west-2:[iam id?]:table/PiggyBanks (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: AccessDeniedException; Request ID: [the request id])



我可以很好地使用表,但是调用PutItem时事情就出错了。

配置

我创建了一个新的Elastic Beanstalk应用程序。根据 documentation,这会自动为应用程序分配一个新角色,称为:
aws-elasticbeanstalk-service-role

相同的文档表明我可以添加对数据库的访问,如下所示:

Add permissions for additional services to the default service role in the IAM console.



因此,我找到了 aws-elasticbeanstalk-service-role角色,并将其添加到托管策略 AmazonDynamoDBFullAccess中。该策略如下所示,为简洁起见,删除了其他操作:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"dynamodb:*",
[removed for brevity]
"lambda:DeleteFunction"
],
"Effect": "Allow",
"Resource": "*"
}
]
}

这肯定看起来像应该授予我所需的访问权限。并且的确, policy simulator对此进行了验证。使用以下参数,可以执行该操作:
  • 角色:aws-elasticbeanstalk-service-role
  • 服务:DynamoDB
  • 操作:PutItem
  • 模拟资源:[从以上日志中提取] arn:aws:dynamodb:us-west-2: [iam ID?] :table/PiggyBanks

  • 更新

    为了回答 filipebarretto的好问题,我实例化DynamoDB对象,如下所示:
    private static DynamoDB createDynamoDB() {
    AmazonDynamoDBClient client = new AmazonDynamoDBClient();
    client.setRegion(Region.getRegion(Regions.US_WEST_2));

    DynamoDB result = new DynamoDB(client);
    return result;
    }

    根据 this documentation,这应该是解决问题的方法,因为它使用的是 default credentials provider chain,然后使用 instance profile credentials

    which exist within the instance metadata associated with the IAM role for the EC2 instance.

    [This option] in the default provider chain is available only when running your application on an EC2 instance, but provides the greatest ease of use and best security when working with EC2 instances.



    我尝试过的其他事情

    related Stack Overflow question的答案表明可能是区域问题。我尝试调整该区域,但没有其他成功。

    我尝试使用以下方法强制使用正确的凭据:
        AmazonDynamoDBClient client = new AmazonDynamoDBClient(new InstanceProfileCredentialsProvider());

    我还尝试从Elastic Beanstalk中创建一个全新的环境。

    最后

    通过日志中的错误,肯定看起来我的Elastic Beanstalk应用程序正在扮演正确的角色。

    而且,根据策略模拟器的结果,看起来该角色应该有权执行我想做的事情。

    所以...请帮助!

    谢谢!

    最佳答案

    更新aws-elasticbeanstalk-ec2-role角色,而不是aws-elasticbeanstalk-service-role。

    salient documentation包含 key :

    When you create an environment, AWS Elastic Beanstalk prompts you to provide two AWS Identity and Access Management (IAM) roles, a service role and an instance profile. The service role is assumed by Elastic Beanstalk to use other AWS services on your behalf. The instance profile is applied to the instances in your environment and allows them to upload logs to Amazon S3 and perform other tasks that vary depending on the environment type and platform.



    换句话说,Beanstalk服务本身使用其中一个角色(-service-role),而将另一个角色(-ec2-role)应用于实际实例。

    后者与您在应用程序代码中所需的任何权限有关。

    关于amazon-web-services - 为什么我的Elastic Beanstalk应用程序尽管具有角色,但仍然拒绝PutItem访问我的DynamoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38555781/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com