【发布时间】:2021-07-18 12:51:44
【问题描述】:
我目前正在使用 AppSync 在 AWS Lambda 中实施订阅变更。我想使用 IAM 并避免使用任何其他类型的 AUTH 机制,因为我在 AWS 堆栈中调用它。不幸的是,我收到以下 403 错误:
(摘自 SQS 的 CloudWatch 日志)
{
"errorMessage": "Response not successful: Received status code 403",
"name": "ServerError",
"errorType": "UnrecognizedClientException",
"message": "The security token included in the request is invalid."
}
我已尝试遵循这些无济于事,但我不知道我错过了什么:
- https://medium.com/@jan.hesters/how-to-use-aws-appsync-in-lambda-functions-e593a9cef1d5
- https://www.edwardbeazer.com/using-appsync-client-from-lambda/
- https://adrianhall.github.io/cloud/2018/10/26/backend-graphql-trigger-appsync/
- How to send GraphQL mutation from one server to another?
- AWS Appsync + HTTP DataSources + AWS IAM
- AWS Appsync Invoke mutate from Lambda?
这是我当前调用它的代码:
import AWS from "aws-sdk";
import { AWSAppSyncClient } from "aws-appsync";
import { Mutation, mutations } from "./mutations/";
import "cross-fetch/polyfill";
/**
*
*/
AWS.config.update({
region: Config.region,
});
export class AppSyncClient {
client: AWSAppSyncClient<any>;
constructor() {
if (!env.APPSYNC_ENDPOINT) {
throw new Error("APPSYNC_ENDPOINT not defined");
}
/**
* We create the AppSyncClient with the AWS_IAM
* authentication.
*/
this.client = new AWSAppSyncClient({
url: env.APPSYNC_ENDPOINT,
region: Config.region,
auth: {
credentials: AWS.config.credentials!,
type: "AWS_IAM",
},
disableOffline: true,
});
}
/**
* Sends a mutation on the AppSync Client
* @param mutate The Mutation that will be sent with the variables.
* @returns
*/
sendMutation(mutate: Mutation) {
const mutation = mutations[mutate.type] as any;
const variables = mutate.variables;
console.log("Sending the mutation");
console.log("Variables is ", JSON.stringify(variables));
return this.client.mutate({
mutation,
fetchPolicy: "network-only",
variables,
});
}
}
这是来自 Lambda SQS 的当前 IAM:
{
"Statement": [
{
"Action": [
"appsync:GraphQL"
],
"Effect": "Allow",
"Resource": [
"arn:aws:appsync:us-east-2:747936726382:apis/myapi"
]
}
],
"Version": "2012-10-17"
}
我知道这不是 lambda 的 IAM 问题,因为我曾尝试暂时授予它完全访问权限,但仍然出现 403 错误。
我还验证了 AppSync 已配置 IAM 权限(作为附加提供者)。
你们有什么想法吗?我印象深刻,这是一个几乎没有配置参考的鬼主题。
【问题讨论】:
标签: amazon-web-services aws-lambda graphql aws-appsync