AWS released data type AuthorizationConfig for HTTP resolvers recently .

The authorization config in case the HTTP endpoint requires authorization.

This means that you can call AWS services without invoking Lambda functions.

AWS AppSync has now been extended to support calling AWS services via HTTP data sources. In order for AWS to identify and authorize HTTP requests, they must be signed with the Signature Version 4 process. Otherwise, those requests are rejected. AWS AppSync can now calculate the signature on your behalf, based on the IAM role that’s provided as part of the HTTP data source configuration. From Josh Kahn

In this post, I show an example of sending SES emails from HTTP resolvers directly.

Getting started with Serverless framework

The example project is based on Serverless framework, You need to add two plugins: serverless-appsync-plugin and serverless-pseudo-parameters.

Updated on 25/04/2019

N̵O̵T̵E̵ ̵t̵h̵a̵t̵ ̵s̵e̵r̵v̵e̵r̵l̵e̵s̵s̵-̵a̵p̵p̵s̵y̵n̵c̵-̵p̵l̵u̵g̵i̵n̵ ̵d̵o̵e̵s̵n̵’̵t̵ ̵s̵u̵p̵p̵o̵r̵t̵ ̵a̵u̵t̵h̵o̵r̵i̵z̵a̵t̵i̵o̵n̵ ̵c̵o̵n̵f̵i̵g̵ ̵f̵o̵r̵ ̵H̵T̵T̵P̵ ̵r̵e̵s̵o̵l̵v̵e̵r̵s̵ ̵y̵e̵t̵.̵ ̵F̵o̵r̵ ̵t̵e̵m̵p̵o̵r̵a̵r̵y̵ ̵s̵o̵l̵u̵t̵i̵o̵n̵,̵ ̵y̵o̵u̵ ̵c̵a̵n̵ ̵g̵e̵t̵ ̵p̵l̵u̵g̵i̵n̵ ̵f̵r̵o̵m̵ ̵m̵y̵ ̵f̵o̵r̵k̵e̵d̵ ̵G̵i̵t̵ ̵r̵e̵p̵o̵.̵

$ npm install serverless-pseudo-parameters

$ npm install serverless-appsync-plugin

Edit the serverless.yml file and add the plugins to plugins section:

plugins:

- serverless-appsync-plugin

- serverless-pseudo-parameters

Define the AWS AppSync Schema

Let’s look at the schema. Schema files are text files, usually named schema.graphql , I only added mutation for sending email in this example:

type Mutation {

sendNotification: String

}

Define the HTTP data source

Next Let’s define the AWS AppSync HTTP data source and sendNotification mutation, Add the following config to the custom section in serverless.yml .

Define the IAM role for HTTP data source

In this example, HTTP resolver invokes Amazon Simple Email Service(SES) to send emails, you need to create an IAM role AppSyncSESserviceRole that allows HTTP resolver access to SES and send email, add following config to resource section in serverless.yml .

Create Mapping Templates

Now we have our yml configured we need to add resolvers, The mapping template files should be located in a directory called mapping-templates relative to the serverless.yml file.

Let’s create the request template for send notification mutation in a file called mapping-templates/Mutation-sendNotification-reques.vtl .



"version": "2018-05-29",

"method": "GET",

"resourcePath": "/",

"params":{

"query":

{

"Action":"SendEmail",

"Source":"noreply@neami.blue",

"Destination.ToAddresses.member.1":"

"Destination.ToAddresses.member.2":"

"Message.Subject.Data":"test subject",

"Message.Body.Text.Data": "test content"

}

}

} ": "2018-05-29",": "GET",": "/",":{":":"SendEmail",":"noreply@neami.blue",":" test1@email.com ",":" test2@email.com ",":"test subject",": "test content"

Then create response template mapping-templates/Mutation-sendNotification-request.vtl .

#if($ctx.error)

$util.error($ctx.error.message, $ctx.error.type)

#end #if($ctx.result.statusCode == 200)

$util.toJson($ctx.result)

#else

$utils.error("Delivery failed")

#end

Let’s deploy the service

$ sls deploy --stage dev

All set to go, Try it out!

You can use the AWS AppSync console to test sendNotification mutation we just deployed.

Then check whether email has been sent and received.

That’s all about it, I hope you have found this article useful, You can find complete project in my GitHub repo.