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?
-
Take a look at my answer [here](https://stackoverflow.com/questions/50957895/manually-sign-appsync-url-to-use-in-lambda-gives-bad-signature-error/51000957#51000957), and see if it's what you need. – robbannn Jun 25 '18 at 17:18
2 Answers
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.

- 3,623
- 1
- 17
- 16
-
-
3In general you will just send a HTTP request to your custom endpoint with an object containing query, variables, and optional operation. Then It depends what auth strategy you have configured. If api_key then you need to provide the key as a header. If IAM then you need to SigV4 sign your request with your IAM credentials. If userpools you will need to login via user pools to get a JWT token and then provide that as an Authorization bearer header. If OIDC then you will provide the JWT that you obtained from your OIDC provider as the Auth bearer token. – mparis Jun 21 '18 at 16:20
-
I'm trying to do almost the same thing. I'm using IAM and trying to do SigV4 but having a hard time. Did you ever do it? I made a ticket actually. Can you have a look please? https://stackoverflow.com/questions/50957895/manually-sign-appsync-url-to-use-in-lambda-gives-bad-signature-error – Tom Jun 21 '18 at 17:20
-
Currently on my phone so can't repro your code just yet but I can point you to the signer code used by the AppSync client. https://github.com/awslabs/aws-mobile-appsync-sdk-js/blob/master/packages/aws-appsync/src/link/signer/signer.ts – mparis Jun 21 '18 at 20:36
-
As another option, I have had good luck with this library in the past https://github.com/mhart/aws4 – mparis Jun 21 '18 at 20:41
-
Hey @mparis, any idea why when I use userpools `bearer = 'Bearer ' + response['AuthenticationResult']['AccessToken']` got from cognito.init_auth using USER_PASSWORD_AUTH and non-SRP enabled I get `{'errors': [{'errorType': 'UnauthorizedException', 'message': 'Valid authorization header not provided.'}]}` ? – hounded Oct 01 '19 at 04:14
-
@mparis One issue I'm having with this approach is dealing with transactions in RDS that can be rolled back. I trigger the Lambda in an AFTER UPDATE trigger on the RDS table, which does the AppSync mutation. However, since AFTER UPDATE runs before the commit, then the transaction could still be rolled back. Now you've done a mutate when the underlying RDS table hasn't actually changed. Any suggestions for addressing this? – Jordan Nov 15 '21 at 15:42
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']))

- 380
- 2
- 10
-
2I edited my post above to better explain to you. Try to do the same and see the result. – rfschroeder Jul 28 '18 at 03:29