I have a client app subscribed to Appysync events. The data source is a Lambda function to RDS. Is it possible to Invoke the mutate from Lambda function which gets triggered on RDS update?
Subscriptions in AWS AppSync are invoked as a response to a mutation. This means that you can make any data source in AWS AppSync real time by specifying a GraphQL schema directive on a mutation. The AWS Amplify client libraries automatically handle subscription connection management.
And thanks to AWS signatures this can happen in a secure way: AppSync can call the API as it can use an IAM Role with the necessary permissions, but for the public it's not available. In this article, we'll see how to configure AppSync to send a signed request to an API Gateway HTTP API using the HTTP data source.
The default concurrency limit per AWS Region is 1,000 invocations at any given time. The default burst concurrency quota per Region is between 500 and 3,000, which varies per Region. There is no maximum concurrency limit for Lambda functions.
If I understand you correctly you would like to invoke a mutation from a lambda function that is triggered via an update in RDS with the intention of notifying subscribed clients of the change in RDS. If this is incorrect stop me now.
I'll assume you are doing something like described here to trigger the lambda when something changes in RDS (https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/AuroraMySQL.Integrating.Lambda.html).
To finish the workflow what I recommend doing is creating a Local resolver (a resolver that points to a 'None' data source) and having your lambda function invoke that mutation resolver with the data you want pushed to client apps whenever something changes in RDS. Your clients would then subscribe to the mutation field with the local resolver (instead of the resolver to the RDS lambda) using the @aws_subscribe directive. This way your connected clients will be informed of any RDS changes that might be made via AppSync or might be made via a direct connection to your SQL instance.
Hope this helps.
I found a question, very similar to yours, answered in this post:
How to send GraphQL mutation from one server to another?
Resuming, you can simply dispatch a HTTP request to your Graphql Server (like an API Gateway) and include the Graphql query (can be a mutation too) as a payload. It seems works for me :)
EDIT:
This lambda below I used when trigged by insert on a DynamoDB table:
import json
import requests
GRAPHQL_URL = 'https://XXXXXXXXXXXXXXXXX.appsync-api.ap-southeast-2.amazonaws.com/graphql'
GRAPHQL_API_KEY = "************************************" (secret)
HEADERS = {
"X-Api-Key":GRAPHQL_API_KEY,
}
def lambda_handler(event, context):
for record in event['Records']:
if record['eventName'] == 'INSERT':
item = record['dynamodb']
res = {}
data = {"operationName":None,"variables":{"id":item['NewImage']['id']['S'],"name":item['NewImage']['name']['S'],"when":item['NewImage']['when']['S'],"where":item['NewImage']['where']['S'],"description":item['NewImage']['description']['S']},"query":"mutation ($id: ID!, $name: String!, $when: String!, $where: String!, $description: String!) {\n eventCreated(id: $id, name: $name, when: $when, where: $where, description: $description) {\n id\n name\n where\n when\n description\n comments {\n items {\n commentId\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"}
try:
res = requests.post(
GRAPHQL_URL,
headers=HEADERS,
data=json.dumps(data)
)
except Exception as e:
print('error: ', e)
print('Graphql request response: ', res)
return 'Successfully processed {} records.'.format(len(event['Records']))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With