【问题标题】:Unable to auth into AppSync GraphQL api from Lambda using IAM and AppSyncClient无法使用 IAM 和 AppSyncClient 从 Lambda 认证到 AppSync GraphQL api
【发布时间】:2022-08-12 05:10:30
【问题描述】:

我正在使用放大堆栈,需要对我的 graphql api 执行一些操作,它后面有 dynamodb。我的 lambda 函数中的请求返回未经授权的错误:“未授权访问类型 SourceSync 的 getSourceSync”,其中 getSourceSync 是 gql 查询,SourceSync 是模型名称。

我的这个特定模型的 schema.graphql 设置如下。注意 auth 规则允许私有提供程序 iam:

type SourceSync @model (subscriptions: { level: off }) @auth(rules: [
    {allow: private, provider: iam}
    {allow: groups, groups: [\"Admins\"], provider: userPools},
    {allow: groups, groups: [\"Users\"], operations: [create], provider: userPools},
    {allow: groups, groupsField: \"readGroups\", operations: [create, read], provider: userPools},
    {allow: groups, groupsField: \"editGroups\", provider: userPools}]) {
    id: ID! @primaryKey
    name: String
    settings_id: ID @index(name: \"bySettingsId\", queryField: \"sourceSyncBySettingsId\")
    settings: Settings @hasOne(fields: [\"settings_id\"])
    childLookup: String
    createdAt: AWSDateTime!
    updatedAt: AWSDateTime!
    _createdBy: String
    _lastChangedBy: String
    _localChanges: AWSJSON
    readGroups: [String]
    editGroups: [String]
}

我的 lambda 函数的角色附加了以下内联策略。 (出于安全目的,本文省略了实际 ID 值):

{
    \"Version\": \"2012-10-17\",
    \"Statement\": [
        {
            \"Action\": [
                \"appsync:GraphQL\"
            ],
            \"Resource\": [
                \"arn:aws:appsync:us-east-1:111myaccountID:apis/11mygraphqlapiID/*\"
            ],
            \"Effect\": \"Allow\"
        },
        {
            \"Action\": [
                \"appsync:GetType\"
            ],
            \"Resource\": [
                \"*\"
            ],
            \"Effect\": \"Allow\"
        }
    ]
}

最后,我的 lambda 函数通过一个简单的查询测试设置如下:

/* stuff */

\"use strict\";
const axios = require(\"axios\");
const awsAppSync = require(\"aws-appsync\").default;
const gql = require(\"graphql-tag\");
require(\"cross-fetch/polyfill\");
const { PassThrough } = require(\"stream\");
const aws = require(\"aws-sdk\");

aws.config.update({
    region: process.env.AWS_REGION,

});

const appSync = new aws.AppSync();

const graphqlClient = new awsAppSync({
    url: process.env.API_GRAPHQLAPIENDPOINTOUTPUT,
    region: process.env.AWS_REGION,
    auth: {
        type: \"AWS_IAM\",
        credentials: aws.config.credentials,
    },
    disableOffline: true
});

exports.handler = async (event, context) => {
    
    console.log(\'context :: \'+JSON.stringify(context));
    
    console.log(\'aws config :: \'+JSON.stringify(aws.config));
    
          const sourceSyncTypes = await appSync
          .getType({
            apiId: process.env.API_GRAPHQLAPIIDOUTPUT,
            format: \"JSON\",
            typeName: \"SourceSync\",
          })
          .promise();
          console.log(\'ss = \'+JSON.stringify(sourceSyncTypes));
    
    try {
    const qs = gql`query GetSourceSync {
  getSourceSync(id: \"ov3\") {
    id
    name
  }
}`;
    const res = await graphqlClient.query({query: qs, fetchPolicy: \'no-cache\'});
    console.log(JSON.stringify(res));
    
    }
    catch(e) {
        console.log(\'ERR :: \'+e);
        console.log(JSON.stringify(e));
    }
    
};
  • 你找到问题了吗?
  • @lionbigcat 是的,发布了答案。

标签: amazon-web-services aws-lambda aws-appsync appsync-apollo-client


【解决方案1】:

找到了解决方案,在允许函数访问 graphql api 后触发 api 上的解析器重建似乎存在问题。但是有一个区别需要注意:

  1. 如果 graphql api 是 amplify 应用程序堆栈的一部分,则只有通过 amplify cli 为该应用程序创建的函数(例如:放大添加功能) 并且通过那里获得对 api 的访问权限将能够访问 api。

    • 另外,在更新期间,当您创建或更新函数以授予其权限时,您必须确保在放大推送操作期间,api 堆栈也将更新。您可以通过简单地在您的 amplify/backend/api//schema.graphql 文件内的注释中添加或删除一个空格来触发此操作。
  2. 如果该函数是直接通过 aws 控制台创建的“临时”,但它正在尝试访问作为 amplify 应用程序堆栈的一部分创建的 graphql api,那么您需要将该函数的角色放在 amplify/backend/api/ <apiname>/custom-roles.json 格式

    { 
      "adminRoleNames": ["<role name>", "<role name 2>", ...] 
    }
    
  3. 如果您的 api 或 lambda 函数都不是使用 amplify cli 作为应用程序堆栈的一部分创建的,那么只需通过内联策略或 pre- 授予对 graphql 资源的访问权限,以进行查询、突变和订阅 lambda 在 IAM 中的角色。明确的政策。

【讨论】:

    猜你喜欢
    • 2020-09-10
    • 2021-09-13
    • 2021-06-01
    • 2020-10-20
    • 2021-04-24
    • 2021-07-18
    • 2021-09-23
    • 2020-04-15
    • 2021-03-26
    相关资源
    最近更新 更多