Slide 1

Slide 1 text

he!o! ServerlessDays Virtual

Slide 2

Slide 2 text

every story n"ds a hero

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Our hero

Slide 5

Slide 5 text

Works in a sma! team on a mid-sized application

Slide 6

Slide 6 text

Most of the time enjoys the job

Slide 7

Slide 7 text

Just sent a test push notification to thousands of customers

Slide 8

Slide 8 text

We need to build this new complex app.

Slide 9

Slide 9 text

Ok, no problem

Slide 10

Slide 10 text

But we have a short deadline.

Slide 11

Slide 11 text

Ok, how short?

Slide 12

Slide 12 text

It needs to be ready for yesterday. And it needs to be scalable and real-time!

Slide 13

Slide 13 text

Slide 14

Slide 14 text

How should I build this?

Slide 15

Slide 15 text

Don't worry, we have a famous consultant that will help you architecture the app.

Slide 16

Slide 16 text

Great!

Slide 17

Slide 17 text

multi-cloud guru

Slide 18

Slide 18 text

Just use Kubernetes! multi-cloud guru

Slide 19

Slide 19 text

But I don't know Kubernetes.

Slide 20

Slide 20 text

I don't have time to learn Kubernetes now.

Slide 21

Slide 21 text

What should I do?

Slide 22

Slide 22 text

Am I the only one who don't know Kubernetes?

Slide 23

Slide 23 text

Maybe I am not good enough for this!

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Serverless GraphQL You should try

Slide 26

Slide 26 text

Serverless GraphQL?

Slide 27

Slide 27 text

Serverless GraphQL with AppSync The Power of

Slide 28

Slide 28 text

@slobodan_ Before we continue…

Slide 29

Slide 29 text

Slobodan Stojanović CTO @ Cloud Horizon & CTO @ Vacation Tracker co-author of Serverless Applications with Node.js book AWS Serverless Hero @slobodan_

Slide 30

Slide 30 text

Serverless GraphQL with AppSync The Power of

Slide 31

Slide 31 text

Serverless GraphQL ?

Slide 32

Slide 32 text

What's GraphQL?

Slide 33

Slide 33 text

@slobodan_ "Our biggest mistake was betting too much on HTML5" Mark Zuckerberg, 2012

Slide 34

Slide 34 text

@slobodan_

Slide 35

Slide 35 text

@slobodan_ HTML HTML

Slide 36

Slide 36 text

@slobodan_ "Our biggest mistake was betting too much on HTML5" Mark Zuckerberg, 2012

Slide 37

Slide 37 text

@slobodan_ REST? FQL? REST? FQL?

Slide 38

Slide 38 text

@slobodan_ "We were frustrated with the differences between the data we wanted to use in our apps and the server queries they required." L" Byron, 2015 GraphQL: A data query language article

Slide 39

Slide 39 text

@slobodan_ Data app needs Data app needs

Slide 40

Slide 40 text

@slobodan_

Slide 41

Slide 41 text

Nice. But, why should I care about GraphQL?

Slide 42

Slide 42 text

I don't have the same problems as Facebook.

Slide 43

Slide 43 text

Ok, but let's s" if you have one of the fo!owing problems

Slide 44

Slide 44 text

Distinct frontend clients for multiple platforms (i.e., web and mobile) has different data requirements.

Slide 45

Slide 45 text

A backend serves data to clients from different sources.

Slide 46

Slide 46 text

A complex state and caching managements for both frontend and backend.

Slide 47

Slide 47 text

Slow pages especia!y on mobile, caused by multiple dependant HTTP requests.

Slide 48

Slide 48 text

These are just some "symptoms" that GraphQL can cure.

Slide 49

Slide 49 text

• Defines a data shape • Hierarchical • Strongly typed • Protocol, not storage • Introspective • Version fr"

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Types type Author { id: Int name: String posts: [Post] } type Post { id: Int title: String text: String author: Author }

Slide 53

Slide 53 text

Schema type Query { getAuthor(id: Int): Author getPostsByTitle(titleContains: String): [Post] } schema { query: Query }

Slide 54

Slide 54 text

Resolvers getAuthor(_, args){ return sql.raw('SELECT * FROM authors WHERE id = %s', args.id); } posts(author){ return request(`https://api.blog.io/by_author/${author.id}`); }

Slide 55

Slide 55 text

Queries { getAuthor(id: 5){ name posts { title author { # this will be the same as the name above name } } } }

Slide 56

Slide 56 text

1. Parse 2. Validate 3. Execute

Slide 57

Slide 57 text

Result { "name": "Slobodan", "posts": [{ "title": "Hello World", "author": { "name": "Slobodan" } }, { "title": "GraphQL", "author": { "name": "Slobodan" } }] }

Slide 58

Slide 58 text

GraphQL supports: • Queries • Mutations • Subscriptions

Slide 59

Slide 59 text

Interesting!

Slide 60

Slide 60 text

Serverless GraphQL

Slide 61

Slide 61 text

Serverless GraphQL Why?

Slide 62

Slide 62 text

GraphQL using Kubernetes We!, you can deploy

Slide 63

Slide 63 text

BUT there's an easier way

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

You sti! n"d to scale these

Slide 66

Slide 66 text

Unless you make it serverless

Slide 67

Slide 67 text

How can I make my GraphQL app serverless?

Slide 68

Slide 68 text

You can make it manua!y

Slide 69

Slide 69 text

Or you can use AWS AppSync!

Slide 70

Slide 70 text

What's AWS AppSync?

Slide 71

Slide 71 text

AppSync is a managed service that uses GraphQL to make it easy for applications to get exactly the data they n"d.

Slide 72

Slide 72 text

AppSync to develop your app faster You can use

Slide 73

Slide 73 text

I can? How?

Slide 74

Slide 74 text

• Define GraphQL schema • Automatically provision a DynamoDB data source and connect resolvers • Write GraphQL queries and mutations • Use the API in the frontend app

Slide 75

Slide 75 text

It's easy to start with Let's give it a try

Slide 76

Slide 76 text

You can start with the guided schema wizard on AWS Web Console

Slide 77

Slide 77 text

But you should use AWS Amplify, CloudFormation or CDK for more serious apps

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

> amplify init > amplify add api > amplify push

Slide 80

Slide 80 text

Wow, that was fast!

Slide 81

Slide 81 text

I should show it to the consultant.

Slide 82

Slide 82 text

That will never work! Amplify is not good enough. multi-cloud guru

Slide 83

Slide 83 text

You can always start with Amplify and then move to CloudFormation or CDK

Slide 84

Slide 84 text

AppSync with CDK import * as cdk from '@aws-cdk/core'; import * as appsync from '@aws-cdk/aws-appsync'; export class AppsyncCdkAppStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Creates the AppSync API const api = new appsync.GraphqlApi(this, 'Api', { name: 'my-awesome-app', schema: appsync.Schema.fromAsset('graphql/schema.graphql'), }); // Prints out the AppSync GraphQL endpoint to the terminal new cdk.CfnOutput(this, "GraphQLAPIURL", { value: api.graphqlUrl }); } }

Slide 85

Slide 85 text

Ok, but the app needs to be real-time multi-cloud guru

Slide 86

Slide 86 text

AWS AppSync lets you specify which part of your data should be available in a real-time manner using GraphQL Subscriptions.

Slide 87

Slide 87 text

Real-time subscriptions type Subscription { addedPost: Post @aws_subscribe(mutations: ["addPost"]) updatedPost: Post @aws_subscribe(mutations: ["updatePost"]) deletedPost: Post @aws_subscribe(mutations: ["deletePost"]) }

Slide 88

Slide 88 text

And it needs to be scalable! multi-cloud guru

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

But what if you need a search functionality? multi-cloud guru

Slide 91

Slide 91 text

AWS AppSync supports Amazon Elasticsearch! the GraphQL operations can be simple lookups, complex queries & mappings, fu! text searches, fuzzy/keyword searches or geo lookups.

Slide 92

Slide 92 text

But what if you need to connect to an existing app? multi-cloud guru

Slide 93

Slide 93 text

You can connect AWS AppSync to AWS Lambda!

Slide 94

Slide 94 text

What about roles and permissions? multi-cloud guru

Slide 95

Slide 95 text

• API_KEY Authorization • AWS_IAM Authorization • OPENID_CONNECT Authorization • AMAZON_COGNITO_USER_POOLS Authorization

Slide 96

Slide 96 text

Multi-tenant? multi-cloud guru

Slide 97

Slide 97 text

You can use Cognito Groups.

Slide 98

Slide 98 text

use Resolver Mapping Template For fine-grained permissions

Slide 99

Slide 99 text

? multi-cloud guru

Slide 100

Slide 100 text

?

Slide 101

Slide 101 text

You can use Apache Velocity Template Language (VTL)

Slide 102

Slide 102 text

What's VTL?

Slide 103

Slide 103 text

I would say it’s an alien language (we!, its Java-based) hard to test in isolation in a serverless application, especia!y in AWS CloudForamation.

Slide 104

Slide 104 text

Alien language a.k.a. VTL #if($context.result["Owner"] == $context.identity.username) $utils.toJson($context.result) #else $utils.unauthorized() #end

Slide 105

Slide 105 text

How do you test that? multi-cloud guru

Slide 106

Slide 106 text

Testing Alien language Templates import { AppSyncMockFile } from 'amplify-appsync-simulator' import { VelocityTemplate } from 'amplify-appsync-simulator/lib/velocity' import { readFileSync } from 'fs' import { join } from 'path' import { getAppSyncSimulator } from './helpers/get-appsync-simulator' import { getVelocityRendererParams } from './helpers/get-velocity-renderer-params' // Read the VTL file from the disc const vtl = readFileSync(join(__dirname, '..', 'get-company-resolver-request.vtl'), 'utf8') const template: AppSyncMockFile = { content: vtl } // Create a simulator instance const simulator = getAppSyncSimulator() // Create a VelocityTemplate instance const velocity = new VelocityTemplate(template, simulator)

Slide 107

Slide 107 text

Testing Alien language Templates describe('some-file.vtl', () => { const { ctxValues, requestContext, info } = getVelocityRendererParams('username', { 'custom:companyId': 'company', }) test('should render a template', () => { const result = velocity.render(ctxValues, requestContext, info) expect(result).toEqual({ errors: [], isReturn: false, stash: {}, result: { version: '2018-05-29', operation: 'GetItem', key: { id: { S: 'company' }, }, }, }) }) })

Slide 108

Slide 108 text

Ha! I don't like that! multi-cloud guru

Slide 109

Slide 109 text

Me neither!

Slide 110

Slide 110 text

AppSync supports Direct Lambda Resolvers, which skips VTL completely

Slide 111

Slide 111 text

But what if you want to reuse some logic? multi-cloud guru

Slide 112

Slide 112 text

You can do that in AWS Lambda or use Pipeline Resolvers

Slide 113

Slide 113 text

Ok, ok, but this is still complex for the frontend multi-cloud guru

Slide 114

Slide 114 text

AWS Amplify gives us nice libraries for the frontend

Slide 115

Slide 115 text

And it can automatica!y generate queries, mutations, subscriptions and types for us!

Slide 116

Slide 116 text

I give up! multi-cloud guru

Slide 117

Slide 117 text

But this is not a!!

Slide 118

Slide 118 text

AppSync supports caching and offline data synchronization

Slide 119

Slide 119 text

Ok, but how do you test it? multi-cloud guru

Slide 120

Slide 120 text

It depends on your approach. It's good to have end-to-end tests, but you can test your Lambda code or VTL templates in isolation.

Slide 121

Slide 121 text

You can also run the app loca!y using AWS Amplify

Slide 122

Slide 122 text

What if we need to use more complex architecture, i.e. CQRS multi-cloud guru

Slide 123

Slide 123 text

Mutation Event storage Subscription Query Read-only table EventBridge Business logic Business logic

Slide 124

Slide 124 text

Mutation Event storage Subscription Query Read-only table EventBridge Business logic Business logic

Slide 125

Slide 125 text

Mutation Event storage Subscription Query Read-only table EventBridge Business logic Business logic

Slide 126

Slide 126 text

multi-cloud guru

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

Serverless GraphQL Guru

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

Quick Summary: • GraphQL makes your frontend and backend connection effortless • AppSync makes GraphQL management effortless • Serverless GraphQL makes you a super hero

Slide 131

Slide 131 text

@slobodan_ vacationtracker.io