[英]AWS Lambda function unable to access AppSync GraphQL API - permission denied
我無法通過 AWS Lambda function 調用 AppSync GraphQL 查詢。 我一直在使用本文中的代碼,特別是使用IAM權限的后半部分: https://docs.amplify.aws/lib/graphqlapi/graphql-from-nodejs/q/platform/js#signing-a-request -來自-lambda
const https = require("https"); const AWS = require("aws-sdk"); const urlParse = require("url").URL; const appsyncUrl = process.env.API_MYAPP_GRAPHQLAPIENDPOINTOUTPUT; const region = process.env.REGION; const endpoint = new urlParse(appsyncUrl).hostname.toString(); const graphqlQuery = require("./query.js").query; exports.handler = async (event) => { const req = new AWS.HttpRequest(appsyncUrl, region); req.method = "POST"; req.path = "/graphql"; req.headers.host = endpoint; req.headers["Content-Type"] = "application/json"; req.body = JSON.stringify({ query: graphqlQuery, operationName: "list", }); const signer = new AWS.Signers.V4(req, "appsync", true); signer.addAuthorization(AWS.config.credentials, AWS.util.date.getDate()); const data = await new Promise((resolve, reject) => { const httpRequest = https.request({...req, host: endpoint }, (result) => { result.on("data", (data) => { resolve(JSON.parse(data.toString())); }); }); httpRequest.write(req.body); httpRequest.end(); }); return { statusCode: 200, body: data, }; };
我正在使用 Amplify CLI。 我使用 CLI 創建了 function 並確保它可以訪問 GraphQL API。
我在 Lambda 中得到的具體錯誤是:
{
"statusCode": 200,
"body": {
"errors": [
{
"errorType": "UnauthorizedException",
"message": "Permission denied"
}
]
}
}
GraphQL 設置為使用 Cognito 用戶池作為身份驗證,我通過 Amplify CLI 添加了 IAM 作為輔助身份驗證機制。 AWS GraphQL 控制台顯示我將 Cognito 作為主要身份驗證機制,將 IAM 作為輔助。
The Lambda function appears to be permissioned okay as it shows 4 resources (create, update, delete, read) corresponding to the API and Allow: appsync:GraphQL as the Action.
如果我使用amplify mock function myfunction
,那么它可以正常執行,並且 GraphQL 查詢的結果正確返回。
當我選擇 IAM 作為身份驗證機制時,我還可以通過 AppSync UI 成功運行相同的查詢。
我正在訪問的表在我的 schema.graphql 中定義為:
type Business
@model
@auth(
rules: [
{ allow: owner }
{ allow: groups, groups: ["Admin"] }
{ allow: private, provider: iam }
]
) {
id: ID!
owner: String!
name: String!
emailSuffix: String!
shortCode: String!
}
我已經從 model 中刪除了 Auth,這並沒有什么不同。
我已經刪除了 function 並重新創建它,以防權限以某種方式搞砸了。
所以我不認為這是代碼,似乎是權限錯誤。 我不知道問題出在哪里
更新我已經在 IAM 管理器中修改了 amplify-lambda-execution 策略的權限策略,這似乎已經解決了這個問題。
Amplify 最初添加的權限策略采用以下形式:
arn:aws:appsync:MYREGION:MYID:apis/MYAPIID/types/create/*
arn:aws:appsync:MYREGION:MYID:apis/MYAPIID/types/read/*
arn:aws:appsync:MYREGION:MYID:apis/MYAPIID/types/edit/*
arn:aws:appsync:MYREGION:MYID:apis/MYAPIID/types/delete/*
將此修改為:
arn:aws:appsync:MYREGION:MYID:apis/MYAPIID/types/*/fields/*
arn:aws:appsync:MYREGION:MYID:apis/MYAPIID
允許 lambda function 在我的表上執行並成功執行 GraphQL 查詢。 因此,Amplify 添加到 function 的權限似乎存在問題。 手動覆蓋這些不是一個很好的解決方案。
安裝后問題開始 - 放大cli“4.45.2”。 lambda cloudformation 模板中自動生成權限的舊代碼如下所示。
{
"Effect": "Allow",
"Action": [
"appsync:Create*",
"appsync:StartSchemaCreation",
"appsync:GraphQL",
"appsync:Get*",
"appsync:List*",
"appsync:Update*",
"appsync:Delete*"
],
"Resource": [
{
"Fn::Join": [
"",
[
"arn:aws:appsync:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":apis/",
{
"Ref": "myApiGraphQLAPIIdOutput"
},
"/*"
]
]
}
]
}
但是在我開始放大 cli "4.45.2" 並為 lambda 授予 appsync 權限之后。 它產生了:
{
"Effect": "Allow",
"Action": [
"appsync:GraphQL"
],
"Resource": [
{
"Fn::Join": [
"",
[
"arn:aws:appsync:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":apis/",
{
"Ref": "myApiGraphQLAPIIdOutput"
},
"/types/create/*"
]
]
},
{
"Fn::Join": [
"",
[
"arn:aws:appsync:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":apis/",
{
"Ref": "myApiGraphQLAPIIdOutput"
},
"/types/read/*"
]
]
},
{
"Fn::Join": [
"",
[
"arn:aws:appsync:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":apis/",
{
"Ref": "myApiGraphQLAPIIdOutput"
},
"/types/update/*"
]
]
}
]
}
enter code here
嘗試從 lambda 訪問 appsync 時出現錯誤“權限被拒絕”。
我認為新 cli 在為 lambda 生成 appsync 權限時存在一些錯誤。
我手動移回 lambda cloudformation 文件中生成的舊代碼,它工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.