Publish messages to SNS Topic using AppSync resolvers with HTTP datasources

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": "arn:aws:sns:us-east-1:<AWS_ACCOUNT_ID>:<SNS_TOPIC_NAME>"
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "appsync.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
{
"endpoint": "https://sns.<REGION>.amazonaws.com",
"authorizationConfig": {
"authorizationType": "AWS_IAM",
"awsIamConfig": {
"signingRegion": "us-east-1",
"signingServiceName": "sns"
}
}
}
# aws appsync create-data-source --api-id <YOUR_API_ID> --name <NAME_FOR_DATA_SORUCE> --type HTTP --http-config file://http.json --service-role-arn arn:aws:iam::<AWS_ACCOUNT_ID>:role/<IAM_ROLE_NAME>
type Mutation {
publishMessage(message: String!): Boolean
}
#set($message = $utils.urlEncode($ctx.args.message))
#set($topic = $utils.urlEncode("arn:aws:sns:us-east-1:<AWS_ACCOUNT_ID>:<SNS_TOPIC_NAME>"))
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/",
"params": {
"body": "Action=Publish&Version=2010-03-31&TopicArn=$topic&Message=$message",
"headers": {
"content-type": "application/x-www-form-urlencoded"
}
}
}
## Raise a GraphQL field error in case of a datasource invocation error
#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type)
#end
## If the response is not 200 then return an false. Else return true
#if($ctx.result.statusCode == 200)
true
#else
false
#end
# aws appsync create-resolver --api-id <YOUR_API_ID> --type-name Mutation --field-name publishMessage --data-source-name <HTTP_DATA_SOURCE> --request-mapping-template file://request.json --response-mapping-template file://response.json
mutation publishMessage {
publishMessage(message:"Sending message to SNS Topic directly from AppSync")
}
{
"data": {
"publishMessage": true
}
}

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store