【问题标题】:Serverless: sqs:CreateQueue Access ... denied无服务器:sqs:CreateQueue 访问...被拒绝
【发布时间】:2021-02-01 21:25:51
【问题描述】:

我正在尝试将我的无服务器框架应用程序部署到多个阶段。

我创建了一个具有我需要的所有权限的 IAM 用户,并将他们的访问密钥放在 ~/.aws/credentials

当我部署到阶段 beta 时,部署工作完美:

sls deploy -s beta --verbose --profile myProfile

当我部署到阶段 dev 时,我收到拒绝访问错误。这个命令:

sls deploy -s dev --verbose --profile myProfile

产生这些错误:

CloudFormation - UPDATE_FAILED - AWS::SQS::Queue - MyQueue
CloudFormation - CREATE_IN_PROGRESS - AWS::SNS::Topic - MyTopic
CloudFormation - UPDATE_FAILED - AWS::SNS::Topic - MyTopic
CloudFormation - UPDATE_FAILED - AWS::IAM::Role - IamRoleLambdaExecution
...
Serverless Error ---------------------------------------

  An error occurred: MyQueue - API: sqs:CreateQueue Access to the resource https://sqs.xx-xxxx-2.amazonaws.com/ is denied..

我的serverless.yml 看起来像这样:

# serverless.yml

service: "my-lambda"
app: "my-lambda"
org: "my-org"

package:
  individually: true

custom:
  topicName: "my-topic--${opt:stage, self:provider.stage}"
  queueName: "my-queue--${opt:stage, self:provider.stage}"

provider:
  name: "aws"
  runtime: "nodejs12.x"
  region: "xx-xxxx-2"
  profile: "myProfile"
  environment:
    NODE_ENV: "${opt:stage, self:provider.stage}"
    SNS_TOPIC_NAME: "${self:custom.topicName}"
    SQS_QUEUE_NAME: "${self:custom.queueName}"
    SLS_DEBUG: "*"

functions:
  myFunc:
    handler: "index.handler"
    events:
      - sns:
          arn: !Ref MyTopic
          topicName: "${self:custom.topicName}"
          redrivePolicy:
            deadLetterTargetRef: "MyQueue"

resources:
  Resources:
    MyTopic:
      Type: "AWS::SNS::Topic"
      Properties:
        TopicName: "${self:custom.topicName}"
    MyQueue:
      Type: "AWS::SQS::Queue"
      Properties:
        QueueName: "${self:custom.queueName}"

这是myProfile 的政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ssm:GetParameters",
            "Resource": "arn:aws:ssm:xx-xxxx-2::parameter/Config/*"
        },
        {
            "Effect": "Allow",
            "Action": "sns:SetSubscriptionAttributes",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "sns:*",
            "Resource": "arn:aws:sns:xx-xxxx-2::my-topic"
        },
        {
            "Effect": "Allow",
            "Action": "sqs:*",
            "Resource": "arn:aws:sqs:xx-xxxx-2::my-queue"
        }
    ]
}

我做错了什么?

【问题讨论】:

  • 您是否提供了在两个阶段或仅测试版上创建 sqs 的权限。根据您的模板,您需要在“my-queue--beta”和“my-queue--dev”上授予创建访问权限。从错误来看,您似乎只授予“my-queue--beta”

标签: amazon-web-services amazon-iam serverless-framework


【解决方案1】:
I created an IAM user with all the permissions I need and placed their access-key in ~/.aws/credentials.

请验证您创建的用户是否有权创建 SQS 队列 - CloudFormation 告诉您事实并非如此。如果您配置了多个 AWS 角色,请确保将环境变量 AWS_PROFILE 设置为正确的角色。

如果您仍然遇到此错误,请发布整个 IAM 政策。

【讨论】:

    【解决方案2】:

    这是您的 AWS 用户配置文件政策问题。

    resources:   
     Resources:
        MyTopic:
          Type: "AWS::SNS::Topic"
          Properties:
            TopicName: "${self:custom.topicName}"
        MyQueue:
          Type: "AWS::SQS::Queue"
          Properties:
            QueueName: "${self:custom.queueName}"
    
    

    sls deploy 在运行时,尝试创建 serverless.yml 配置的资源下定义的所有资源。

    即使您提供对个人资料的管理员访问权限,它也不起作用。 正如您在配置文件策略中提到的,您似乎已经创建了队列和主题。

    通过在 serverless.yml 的资源下定义它们,只需指定您要再次创建这些资源。

    由于您现在没有管理员权限,这表明您无权创建 sqs 队列。

    尝试在您的 aws 配置文件中添加管理员角色,您将再次看到错误,即队列和主题已存在。

    尝试删除已经存在的资源,然后sls deploy,就可以了。 或者干脆不使用资源并直接指定 ARN。

    functions:
     myFunc:
       handler: index.handler
       events:
          - sns:
              arn: arn:xxx
              redrivePolicy:
                deadLetterTargetArn: arn:aws:sqs:xxx:DLQ
    
    

    参考这个 https://github.com/serverless/serverless/issues/3183

    【讨论】:

      【解决方案3】:

      谢谢各位,但问题最终变得非常简单。在serverless.yml 中,我这样定义了我的主题和队列名称:

      custom:
        topicName: "my-topic--${opt:stage, self:provider.stage}"
        queueName: "my-queue--${opt:stage, self:provider.stage}"
      

      但在我的政策中,我写了arn:aws:sns:xx-xxxx-2::my-topicarn:aws:sqs:xx-xxxx-2::my-queue

      解决方案很简单,只需将--stage 后缀添加到我的策略中的资源,或使用* 作为 ARN 的尾随段。

      谢谢大家的回复。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-22
        • 2018-05-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多