无服务器:sqs:CreateQueue 访问...被拒绝

Serverless: sqs:CreateQueue Access ... denied

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

我创建了一个拥有我需要的所有权限的 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"
        }
    ]
}

我做错了什么?

I created an IAM user with all the permissions I need and placed their access-key in ~/.aws/credentials.

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

如果您仍然遇到此错误,请post整个 IAM 策略。

这是您的 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 on 运行,尝试创建在 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

谢谢大家,不过问题最终变得很简单。在 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 的结尾部分。

谢谢大家的回复。