More Related Content
Similar to GraphQL backend with AWS AppSync & AWS Lambda (20)
More from Aleksandr Maklakov (14)
GraphQL backend with AWS AppSync & AWS Lambda
- 1. © 2019, Amazon Web Services, Inc. or its Affiliates.© 2019, Amazon Web Services, Inc. or its Affiliates.
Sergey Pugachev
GraphQL backend with
AWS AppSync & AWS Lambda
Solutions Architect, AWS
- 2. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Fullstack
https://www.flickr.com/photos/ginnerobot/2549674908/
- 3. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Backend
Front end
CI/CD
https://www.flickr.com/photos/ginnerobot/2549674908/
- 4. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
No servers to provision
or manage
Scales with usage
Never pay for idle Availability and fault
tolerance built in
Serverless means…
- 6. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cloud Services
Cloud
Less More
Compute Virtual Machine
EC2 Elastic Beanstalk AWS LambdaFargate
Databases MySQL MySQL on EC2
RDS MySQL RDS Aurora Aurora Serverless DynamoDB
Storage Storage
S3
Messaging ESBs
Amazon MQ Kinesis SQS / SNS
Analytics
Hadoop Hadoop on EC2 EMR Elasticsearch Service Athena
- 7. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Docker orchestration in the cloud
Management
Hosting
Amazon Elastic
Container Service
Amazon Elastic
Kubernetes Service
Amazon EC2 AWS Fargate
Image Registry Amazon Elastic
Container Registry
- 8. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Upstream Kubernetes
Use standard Kubernetes APIs.
Works with community tools.
Highly available
Built for production workloads, all clusters are highly
available. Backed by a 99.9% SLA.
Integrated
with the AWS ecosystem: VPC Networking, Elastic Load
Balancing, IAM Permissions, CloudWatch and more
Amazon Elastic Kubernetes Service
- 9. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Managed by AWS
No EC2 Instances to provision, scale or manage
Elastic
Scale up & down seamlessly. Pay only for what you use
Integrated
with the AWS ecosystem: VPC Networking, Elastic Load
Balancing, IAM Permissions, CloudWatch and more. Run
Kubernetes pods or ECS tasks.
AWS Fargate
- 10. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
- 11. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
{API}
- 12. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
InternetMobile &
web apps
Databases &
data stores
Basic API technology stack
Failover
Load balancers
Web & application servers
Message buses
Workers
?API
backend
?API
“server”
- 13. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS
InternetMobile &
web apps
Databases &
data stores
Basic API technology stack
Failover
Load balancers
Web & application servers
Message buses
Workers
?API
backend
?API
“server”
- 14. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
InternetMobile &
web apps
Basic serverless API technology stack
AWS
Serverless
business logic
Serverless
APIs
Databases &
data stores
Fail over
Load balancers
Web/application servers
Message buses
Workers
- 15. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
/posts /comments /authors
REST API
posts comments authors
GraphQL API
What is GraphQL?
- 16. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Queries MutationsTypes
Subscriptions
GraphQL schema and operations
A query language for APIs . . . and a runtime!
type User {
id: ID!
username: String!
firstName: String
lastName: String
daysActive: Int
}
- 17. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Queries
query GetPost {
getPost(id: ”1”) {
id
title
}
}
mutation CreatePost {
createPost(title: “Summit”) {
id
title
}
}
subscription OnCreatePost {
onCreatePost {
id
title
}
}
Mutations Subscriptions
A query language for APIs . . .
- 18. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
- 19. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
- 20. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS AppSync
Managed serverless
GraphQL service
Connect to data
sources in your account
Add data sync, real-time, and
offline capabilities for any data
source or API
GraphQL facade for any
AWS service
Conflict detection and
resolution in the cloud
Enterprise security features:
IAM, Amazon Cognito, OIDC,
API keys
- 21. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
How does AWS AppSync work?
,
,
- 22. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL data flow in AWS AppSync: Resolvers
Request template Response template
Data sources
1 2 3 4 5
- 23. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Pipeline resolvers
- 24. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Real-time data broadcasting
- 25. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Real-time subscriptions
subscription onCreateMessage {
createMessage {…}
}
WebSocket URL and connection payload
Secure WebSocket connection (wss://)
AppSync Data Sources
AWS AppSync
Amazon
DynamoDB
Amazon Elasticsearch
Service
AWS Lambda
Amazon Aurora
HTTP
Local (Pub/Sub)
- 26. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Offline
- 27. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Polyglot backend data access
- 28. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
- 29. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Amplify Framework
- 30. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Amplify Framework
- 31. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Amplify Framework
ü CLI
ü Client libraries (native and JS support)
ü Console – Continuous deployment
and hosting
ü Prebuilt UI components
ü Toolchain
ü JS framework support
- 32. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
• Create, update, and delete cloud services
• Manage multiple environments
• GraphQL Transform
• GraphQL Codegen
Amplify Framework
CLI
- 33. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Categories
Authentication
Analytics
Interactions (chat bots)
API (REST)
API (GraphQL)
Amplify CLI
Serverless functions
Storage
XR
Push notifications
Video
- 34. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
$ amplify add auth
$ amplify push
$ amplify configure auth
$ amplify delete
$ amplify codegen add
Amplify CLI
- 35. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
$ amplify add api
–
# schema.graphql
type Post @model {
id: ID!
title: String!
}
- 36. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
@model
Top-level entity; creates DynamoDB table, resolvers, and additional schema (queries,
mutations, and subscriptions) for base type
@connection
Enables relationships between @model types
@auth
Enables set of authorization rules
@searchable
Handles streaming the data of an @model object type to Amazon Elasticsearch Service
and configures search resolvers
@versioned
Enables versioning
@function
Enables adding a Lambda function as a data source
–
- 37. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL Transform: Mix and match data sources
type Post {
id: ID!
content: String
description: String
ups: Int
downs: Int
}
- 38. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL Transform: Mix and match data sources
createPost
readPost
updatePost
deletePost
type Post
@model {
id: ID!
content: String
description: String
ups: Int
downs: Int
}
- 39. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL Transform: Mix and match data sources
mutations
queries
createPost
readPost
updatePost
deletePost
type Post
@model
@auth(rules: [{allow:
owner}]){
id: ID!
content: String
description: String
ups: Int
downs: Int
}
- 40. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL Transform: Mix and match data sources
searchPosts
mutations
queries
createPost
readPost
updatePost
deletePost
type Post
@model
@auth(rules: [{allow: owner}])
@searchable{
id: ID!
content: String
description: String
ups: Int
downs: Int
}
- 41. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL Transform: Mix and match data sources
searchPosts
mutations
queries
createPost
readPost
updatePost
deletePost
type Post
@model
@auth(rules: [{allow:
owner}])
@searchable{
id: ID!
content: String
description: String
ups: Int
downs: Int
}
- 42. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
$ amplify add codegen [--apiId <api-id>]
// Will generate GraphQL statements
// (queries, mutations, subscriptions)
- 43. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
$ amplify env add
$ amplify env list
$ amplify env checkout
$ amplify env remove
- 44. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Multiple environments and team workflow
• Git-style interaction and project switching
• Share backends between team members or clone for sandbox isolated development
User Users
- 45. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Amplify Framework
• Connect to and interact with cloud services from web and mobile
applications
• Preconfigured components for popular front-end libraries
Client
- 46. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Amplify client
• Opinionated
• Declarative
• Client focused
• High-level abstractions
• Best practices built in
• Built with Amplify CLI in mind
- 47. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Amplify client
import {
Auth, API, Storage, Analytics, XR
} from ‘aws-amplify’
$ npm install aws-amplify
- 48. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Amplify client
import {
Auth
} from ‘aws-amplify’
Auth.signIn(’myusername’, ‘mypassword’)
Auth.signOut()
Auth.currentAuthenticatedUser()
- 49. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Amplify: Native support
- 50. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Amplify: JS framework support
- 51. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Higher-order components
import {withAuthenticator} from ‘aws-amplify-react’
export default withAuthenticator(App)
class App extends Component {
// your code goes here
- 52. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Amazon API Gateway
Amazon Pinpoint
AWS Lambda
AWS AppSync Amazon DynamoDBAmazon Cognito
- 53. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Deploying a Lambda function with API Gateway
1. Create the Lambda function & API
$ amplify add api
// chose REST with Express backend
- 54. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
2. Write code/make changes
// amplify/backend/function/<FUNCTION_NAME>/src/app.js
app.get(‘/items’, function(req, res) {
// new code goes here
})
Deploying a Lambda function with API Gateway
- 55. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
$ amplify push
3. Deploy new API
Deploying a Lambda function with API Gateway
Amazon API Gateway AWS Lambda
- 56. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
import { API } from ‘aws-amplify’
API.get(’/items’)
.then(data => console.log({ data }))
.catch(err => console.log({ err })
4. Interact with the API
Deploying a Lambda function with API Gateway
- 57. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
$ amplify add auth
// choose either default or custom configuration
1. Create the authentication service
Adding authentication
- 58. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
import { Auth } from ‘aws-amplify’
2. Import the Auth component
Adding authentication
- 59. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Auth.signUp(‘username’, ‘password’, {
attributes: { email: ‘jane@doe.com’ }
})
Auth.signIn(’myusername’, ‘mypassword’)
3. Implement Auth methods
Adding authentication
- 60. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
$ amplify add api
// chose GraphQL
1. Create the GraphQL API
Deploying a GraphQL API
- 61. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
type Todo @model {
id: ID!
name: String!
description: String!
completed: Boolean
}
2. Define annotated GraphQL schema
Deploying a GraphQL API
- 62. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
$ amplify push
3. Deploy new API
Deploying a GraphQL API
Amazon Cognito AWS AppSync Amazon DynamoDB
- 63. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
import { API, GraphqlOperation } from ‘aws-amplify’
import { listTodos } from ‘./src/graphql/queries’
API.graphql(graphqlOperation(listTodos))
.then(data => console.log({ data }))
.catch(err => console.log({ err })
4. Interact with API
Deploying a GraphQL API
- 64. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
$ amplify add analytics
$ amplify push
1. Create analytics service
Adding analytics
Amazon Pinpoint
- 65. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
2. Import Analytics API
Adding analytics
import { Analytics } from ‘aws-amplify’
- 66. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
3. Record events
Adding analytics
Analytics.record({ name: 'albumVisit’ })
Analytics.record({
name: 'albumVisit', // Attribute values must be strings
attributes: { genre: ‘nu-metal', artist: ’RATM’ }
})
- 67. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Amazon API Gateway
Amazon Pinpoint
AWS Lambda
AWS AppSync Amazon DynamoDBAmazon Cognito
- 68. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS AppSync JS SDK
• Offline ready
• Built-in helpers for optimistic response,
subscriptions
• Use with React Apollo / Vue Apollo /
Angular Apollo
• https://github.com/awslabs/aws-mobile-appsync-sdk-js
- 69. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
- 70. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
- 71. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Fullstack DevOps for web apps
• Strong isolation barrier between projects and stages
Dedicated AWS accounts for each project and stage (sandbox, staging, prod)
• Source code (and history) for each stage readily available
Git branching used for the source code for each stage
• Minimize version mismatches between front end and backend
Front end (e.g., code) and backend (e.g., AWS CloudFormation templates)
developed and versioned together in same Git repository
• Developers in control
Developers deploy front end *and* backend
- 72. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
DevOps requirements for modern web apps
Web app
per dev
sandbox
Test web app Prod web app
Isolated
backends
Shared backend
- 73. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
DevOps challenges with modern web apps
Lots of moving parts complicate the following:
- 74. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Amplify Console
Build, deploy, and host cloud-powered modern web apps
Optional deployment of backend resources +
fully managed front-end hosting
- 75. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Use cases
Use static site generators like Hugo or Jekyll
to publish a blog. SSGs perform faster than
traditional websites. Deploy SSGs with free
SSL on the AWS Amplify Console.
Add dynamic functionality using GraphQL or
REST APIs. Use the AWS Amplify Console to
deploy your front end and backend in a single
workflow.
PWAs offer native app-like performance, work
offline, support push notifications, and can be
updated over the air. Use AWS Amplify to
deploy PWAs to mobile devices.
- 76. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2019, Amazon Web Services, Inc. or its Affiliates.
Sergey Pugachev
GraphQL backend with
AWS AppSync & AWS Lambda
Solutions Architect, AWS