visit
$ sls create --template aws-nodejs --path simpleCMS
In the app folder, there is a serverless.yml file. This file is needed to configure how our application will behave.
npm install serverless-appsync-plugin
Add serverless-appsync-plugin to the plugins section of serverless.yml
plugins:
- serverless-appsync-plugin
Add the following config to the custom section of serverless.yml
custom:
appSync:
region: ${self:provider.region}
name: ${self:service.name}-${self:provider.stage}
authenticationType: AWS_IAM
serviceRole: "${self:service.name}-AppSyncServiceRole"
userPoolConfig:
awsRegion: "${self:provider.region}"
userPoolId: ${self:custom.userPool.${self:provider.stage}}
defaultAction: ALLOW
mappingTemplates:
- type: Query
dataSource: LambdaSource
field: getPosts
request: "Query-getPosts-request.vtl"
response: "Query-getPosts-response.vtl"
- type: Post
dataSource: LambdaSource
field: author
request: "Query-author-request.vtl"
response: "Query-author-response.vtl"
- type: Mutation
field: addPost
request: Mutation-addPost-request.vtl
response: common-response.vtl
kind: PIPELINE
functions:
- addPost
- sendNotification
- type: Mutation
dataSource: LambdaSource
field: deletePost
request: Mutation-deletePost-request.vtl
response: common-response.vtl
functionConfigurations:
- dataSource: LambdaSource
name: "addPost"
request: "Function-addPost-request.vtl"
response: "common-response.vtl"
- dataSource: LambdaSource
name: "sendNotification"
request: "Function-sendNotification-request.vtl"
response: "Function-sendNotification-response.vtl"
dataSources:
- type: AWS_LAMBDA
name: LambdaSource
description: "Lambda DataSource"
config:
functionName: graphql
lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] }
serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
You can get all mapping templates from .
Next, create a IAM service role for Appsync: <a href="//medium.com/media/c0c0586f44892991396b65c048873c69/href">//medium.com/media/c0c0586f44892991396b65c048873c69/href</a>InitialSystemAdminUser:
Type: AWS::Cognito::UserPoolUser
Properties:
UserPoolId:
Ref: CognitoUserPool
Username: "[email protected]"
DesiredDeliveryMediums:
- EMAIL
ForceAliasCreation: true
UserAttributes:
- Name: email
Value: "[email protected]"
- Name: name
Value: Admin
To link Appsync lambda datasource to RDS, you will need to add Aurora RDS’ VPC config we just configured to providers section
provider:
vpc:
securityGroupIds:
- Fn::GetAtt: [DatabaseVpcSecurityGroup, GroupId]
subnetIds:
- Ref: PrivateSubnet1
- Ref: PrivateSubnet2
You can find full serverless.yml .
$ sls deploy --stage dev
It will automagically provision resources on AWS, package up and push all code to S3 from where it will be sent to the Lambdas. The terminal should show output similar to this
I hope you have found this article useful, You can find complete project in my GitHub repo: