Slide 1

Slide 1 text

he!o! AWS Community Day Bosnia

Slide 2

Slide 2 text

every story n"ds a hero

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Our hero

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Most of the time enjoys the job

Slide 8

Slide 8 text

Just sent a test push notification to thousands of customers

Slide 9

Slide 9 text

We need to build this new complex app.

Slide 10

Slide 10 text

Ok, no problem

Slide 11

Slide 11 text

But we have a short deadline.

Slide 12

Slide 12 text

Ok, how short?

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Slide 15

Slide 15 text

How should I build this?

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Great!

Slide 18

Slide 18 text

multi-cloud guru

Slide 19

Slide 19 text

Just use Kubernetes! multi-cloud guru

Slide 20

Slide 20 text

But I don't know Kubernetes.

Slide 21

Slide 21 text

I don't have time to learn Kubernetes now.

Slide 22

Slide 22 text

What should I do?

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Maybe I am not good enough for this!

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Serverless GraphQL You should try

Slide 27

Slide 27 text

Serverless GraphQL?

Slide 28

Slide 28 text

Serverless GraphQL with AppSync The Power of

Slide 29

Slide 29 text

@slobodan_ Before we continue…

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Serverless GraphQL with AppSync The Power of

Slide 32

Slide 32 text

Serverless GraphQL

Slide 33

Slide 33 text

Serverless GraphQL ?

Slide 34

Slide 34 text

What's GraphQL?

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

@slobodan_

Slide 37

Slide 37 text

@slobodan_ HTML HTML

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

@slobodan_ REST? FQL? REST? FQL?

Slide 40

Slide 40 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 41

Slide 41 text

@slobodan_ Data app needs Data app needs

Slide 42

Slide 42 text

@slobodan_

Slide 43

Slide 43 text

Nice. But, why should I care about GraphQL?

Slide 44

Slide 44 text

I don't have the same problems as Facebook.

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

A backend serves data to clients from different sources.

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 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 57

Slide 57 text

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

Slide 58

Slide 58 text

1. Parse 2. Validate 3. Execute

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

GraphQL supports: • Queries • Mutations • Subscriptions

Slide 61

Slide 61 text

Interesting!

Slide 62

Slide 62 text

Serverless GraphQL

Slide 63

Slide 63 text

Serverless GraphQL Why?

Slide 64

Slide 64 text

GraphQL using Kubernetes We!, you can deploy

Slide 65

Slide 65 text

BUT there's an easier way

Slide 66

Slide 66 text

You sti! n"d to scale these

Slide 67

Slide 67 text

Unless you make it serverless

Slide 68

Slide 68 text

How can I make my GraphQL app serverless?

Slide 69

Slide 69 text

You can make it manua!y

Slide 70

Slide 70 text

Or you can use AWS AppSync!

Slide 71

Slide 71 text

What's AWS AppSync?

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

AppSync to develop your app faster You can use

Slide 74

Slide 74 text

I can? How?

Slide 75

Slide 75 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 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

> amplify init > amplify add api > amplify push

Slide 81

Slide 81 text

Wow, that was fast!

Slide 82

Slide 82 text

I should show it to the consultant.

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 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 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 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 89

Slide 89 text

And it needs to be scalable! multi-cloud guru

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

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

Slide 92

Slide 92 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 93

Slide 93 text

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

Slide 94

Slide 94 text

You can connect AWS AppSync to AWS Lambda!

Slide 95

Slide 95 text

What about roles and permissions? multi-cloud guru

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

Multi-tenant? multi-cloud guru

Slide 98

Slide 98 text

You can use Cognito Groups.

Slide 99

Slide 99 text

use Resolver Mapping Template For fine-grained permissions

Slide 100

Slide 100 text

? multi-cloud guru

Slide 101

Slide 101 text

?

Slide 102

Slide 102 text

You can use Apache Velocity Template Language (VTL)

Slide 103

Slide 103 text

What's VTL?

Slide 104

Slide 104 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 105

Slide 105 text

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

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

Me neither!

Slide 108

Slide 108 text

AppSync supports Direct Lambda Resolvers, which skips VTL completely

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

You can do that in AWS Lambda or use Pipeline Resolvers

Slide 111

Slide 111 text

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

Slide 112

Slide 112 text

AWS Amplify gives us nice libraries for the frontend

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

I give up! multi-cloud guru

Slide 115

Slide 115 text

But this is not a!!

Slide 116

Slide 116 text

AppSync supports caching and offline data synchronization

Slide 117

Slide 117 text

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

Slide 118

Slide 118 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 119

Slide 119 text

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

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

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

Slide 122

Slide 122 text

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

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

multi-cloud guru

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

Serverless GraphQL Guru

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

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

Slide 129

Slide 129 text

@slobodan_ vacationtracker.io