AWS Cognito和API网关身份验证

时间:2016-04-26 15:48:02

标签: amazon-web-services aws-lambda aws-api-gateway amazon-cognito amazon-iam

我在API网关(Auth:AWS_IAM)下设置了GET方法,并拥有一个具有开发者身份的Cognito池。我在get方法后面有一个lambda。

当我打电话给Cognito时,我获得了临时凭证,我承担了一个角色。我假设的角色具有执行和访问API网关上所有内容的适当权限。

       ...
       {
            "Effect": "Allow",
            "Action": [
                "execute-api:Invoke"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "apigateway:GET"
            ],
            "Resource": [
                "*"
            ]
        }
        ...

当我使用此设置调用API网关时,我收到500,内部服务器错误。

如果我从策略中删除了上述API网关权限,那么我会获得403 error forbidden (User: arn:aws:sts::xxxxx:assumed-role/Cogn_Auth_Role/xxx is not authorized to perform: execute-api:Invoke on resource: arn:aws:execute-api:us-east-1:xxxx:xxx/xxx/GET/events

如果我将AdminAccess附加到此角色,那么一切正常。这是什么交易?我错过了什么吗?

3 个答案:

答案 0 :(得分:3)

因此,在修改了像这样的认知角色政策后,它开始正常运作。

  {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "lambda:InvokeFunction"
                ],
                "Resource": [
                    "*"
                ]
            },
            {
                "Effect": "Allow",
                "Action": [
                    "execute-api:Invoke"
                ],
                "Resource": [
                    "*"
                ]
            }
        ]
    }

使它成功的重要部分:

  {
        "Effect": "Allow",
        "Action": [
            "lambda:InvokeFunction"
        ],
        "Resource": [
            "*"
        ]
    }

仍然不确定为什么我应该调用所有lambdas的权限。

答案 1 :(得分:2)

如果您只是尝试使用Cognito凭据调用API Gateway API,那么您的策略中可能不需要“apigateway:GET”。由于这用于管理您的API,例如获取有关API资源的信息。

如果您只是想创建一个角色以便调用您的API,您可以尝试从您的策略中删除“apigateway:GET”并查看它是否有效。更多information

答案 2 :(得分:0)

这也可能是由于您的API网关资源策略中有一个条件,该条件仅允许满足要求的客户端访问。如果您访问API网关以使用Postman进行测试,则将收到“未经授权”错误。例如,

"Condition": {
        "StringLike": {
            "aws:Referer": [
                "https://example.com/*",
                "example.com/*"
            ]
          }
         }