Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New serverless pattern - Eventbridge bus to Appsync Subscription #2046

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions eventbridge-appsync-subscription-sam/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Amazon Eventbridge to Appsync

This pattern shows how to create an Appsync API that allows you to listen to events going into an Eventbridge event bus. This is especially useful if you want your frontend users to be aware of changes happening in your backend. Those events coming in your event bus can either be pushed as custom events or can come from AWS services.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/eventbridge-appsync-subscription-sam

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

## Requirements

* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
```
git clone https://github.com/aws-samples/serverless-patterns
```
1. Change directory to the pattern directory:
```
cd eventbridge-appsync-subscription-sam
```
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
```
sam deploy --guided
```
1. During the prompts:
* Enter a stack name
* Enter the desired AWS Region
* Allow SAM CLI to create IAM roles with the required permissions.

Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults.

1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.

## How it works

Whenever an event comes into your Amazon Eventbridge event bus, it will trigger an AWS Appsync subscription on your API and notify all subscribers of the change. You can further enhance the solution by creating specific rules that listen to specific events. The current example listens to (and publishes) all events coming in the event bus.

## Testing

1. Navigate to the Appsync console
2. Select the API we have just created (`EventBridgeAppsyncApi`)
3. Click on `Queries` in the left pane
4. Enter the following AppSync query:
```graphql
subscription MySubscription {
onEbUpdate {
message
}
}
```
5. Click on `Run` > `MySubscription`
![appsync-1](images/appsync-1.png)

On another tab:
1. Navigate to the Eventbridge console
2. Select the event bus we have created (`EventBridgeAppsyncEventBus`)
3. Click on `Send events`
![eventbridge-1](images/eventbridge-1.png)
4. Create an event with following fields
1. Event Source: `test`
2. Detail Type: `test`
3. Event detail
```json
{
"message": "Hello from Eventbridge"
}
```
5. Click on `Send event`
![eventbridge-2](images/eventbridge-2.png)
6. You should see the following confirmation
![eventbridge-3](images/eventbridge-3.png)

On the Appsync tab with the subscription running, you should see the message coming from the Eventbridge event we just created.
![appsync-2](images/appsync-2.png)

## Cleanup

1. Delete the stack
```bash
sam delete
```
----
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"title": "Eventbridge rule to Appsync target",
"description": "Listen to Eventbridge events from an Appsync subscription",
"language": "YAML",
"level": "200",
"framework": "SAM",
"introBox": {
"headline": "How it works",
"text": [
"This pattern shows how to create an Appsync API that allows you to listen to events going into an Eventbridge event bus.",
"This pattern is especially useful if you want your frontend users to be aware of changes happening in your backend. Those events coming in your event bus can either be pushed as custom events or can come from AWS services."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-appsync-subscription-sam",
"templateURL": "serverless-patterns/eventbridge-appsync-subscription-sam",
"projectFolder": "eventbridge-appsync-subscription-sam",
"templateFile": "template.yaml"
}
},
"resources": {
"bullets": [
{
"text": "AWS AppSync",
"link": "https://aws.amazon.com/appsync/"
},
{
"text": "Amazon Eventbridge",
"link": "https://aws.amazon.com/eventbridge/"
},
{
"text": "Appsync as Eventbridge target",
"link": "https://aws.amazon.com/about-aws/whats-new/2024/01/amazon-eventbridge-appsync-target-buses/"
}
]
},
"deploy": {
"text": [
"sam deploy"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>sam delete</code>."
]
},
"authors": [
{
"name": "Alexis Philippart de Foy",
"bio": " I am a Startup Solutions Architect, Serverless enthusiast and former Startup Founder.",
"image": "https://ca.slack-edge.com/E015GUGD2V6-U03KALDS3T6-7e7172b8ee1d-512",
"linkedin": "apdf",
"twitter": "@aphilippartd"
}
],
"patternArch": {
"icon1": {
"x": 20,
"y": 50,
"service": "eventbridge",
"label": "EventBridge"
},
"icon2": {
"x": 80,
"y": 50,
"service": "appsync",
"label": "AWS AppSync"
},
"line1": {
"from": "icon1",
"to": "icon2",
"label": "EventBridge target"
}
}
}
62 changes: 62 additions & 0 deletions eventbridge-appsync-subscription-sam/example-pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"title": "Eventbridge rule to Appsync target",
"description": "Listen to Eventbridge events from an Appsync subscription",
"language": "YAML",
"level": "200",
"framework": "SAM",
"introBox": {
"headline": "How it works",
"text": [
"This pattern shows how to create an Appsync API that allows you to listen to events going into an Eventbridge event bus.",
"This pattern is especially useful if you want your frontend users to be aware of changes happening in your backend. Those events coming in your event bus can either be pushed as custom events or can come from AWS services."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-appsync-subscription-sam",
"templateURL": "serverless-patterns/eventbridge-appsync-subscription-sam",
"projectFolder": "eventbridge-appsync-subscription-sam",
"templateFile": "template.yaml"
}
},
"resources": {
"bullets": [
{
"text": "AWS AppSync",
"link": "https://aws.amazon.com/appsync/"
},
{
"text": "Amazon Eventbridge",
"link": "https://aws.amazon.com/eventbridge/"
},
{
"text": "Appsync as Eventbridge target",
"link": "https://aws.amazon.com/about-aws/whats-new/2024/01/amazon-eventbridge-appsync-target-buses/"
}
]
},
"deploy": {
"text": [
"sam deploy"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>sam delete</code>."
]
},
"authors": [
{
"name": "Alexis Philippart de Foy",
"image": "https://media.licdn.com/dms/image/C4D03AQFyN5GLrFk-0g/profile-displayphoto-shrink_400_400/0/1586853008714?e=1710979200&v=beta&t=Cwsaq7BQtFlztLuWDJAnE4VBTIJCWXOAwJ0bvsAhDmc",
"bio": " I am a Startup Solutions Architect, Serverless enthusiast and former Startup Founder.",
"linkedin": "apdf",
"twitter": "@aphilippartd"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions eventbridge-appsync-subscription-sam/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type Update @aws_iam {
message: String!
}

type Mutation {
onEbUpdate (
message: String!
): Update @aws_iam
}

type Subscription {
onEbUpdate(message: String): Update
@aws_subscribe(mutations: ["onEbUpdate"])
}

# Any Query you need for your application
type QueryOutput { output: String! }
type Query { myQuery(input: String!): QueryOutput }
91 changes: 91 additions & 0 deletions eventbridge-appsync-subscription-sam/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Serverless patterns - Eventbridge Appsync Subscription

Resources:

##########################################################################
# AppSync API #
##########################################################################
AppSyncApi:
Type: AWS::Serverless::GraphQLApi
Properties:
Name: EventBridgeAppsyncApi
SchemaUri: ./schema.graphql
ApiKeys:
TestApiKey:
Description: Test Api Key
Auth:
Type: API_KEY
Additional:
- Type: AWS_IAM
Functions:
onEbUpdateFunction:
Runtime:
Name: APPSYNC_JS
Version: 1.0.0
DataSource: None
InlineCode: |
export function request(_) { return {}; }
export function response(ctx) { return ctx.args; }
Resolvers:
Mutation:
onEbUpdate:
Runtime:
Name: APPSYNC_JS
Version: "1.0.0"
Pipeline:
- onEbUpdateFunction


##########################################################################
# Eventbridge #
##########################################################################

EBRule:
Type: AWS::Events::Rule
Properties:
EventBusName: !GetAtt EBBus.Name
EventPattern:
source:
- exists: true
Name: EventBridgeAppsyncEBRule
State: ENABLED
Targets:
- Id: EventBridgeAppsyncEBRuleTarget
Arn: !GetAtt AppSyncApi.GraphQLEndpointArn
RoleArn: !GetAtt EBExecutionRole.Arn
InputTransformer:
InputPathsMap:
message: $.detail.message
InputTemplate: '{"message": <message>}'
AppSyncParameters:
GraphQLOperation: >-
mutation
onEbUpdate($message:String!){onEbUpdate(message:$message){message}}

EBBus:
Type: AWS::Events::EventBus
Properties:
Name: "EventBridgeAppsyncEventBus"

EBExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: events.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: EBExecutionPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'appsync:GraphQL'
Resource:
- !Sub 'arn:aws:appsync:${AWS::Region}:${AWS::AccountId}:apis/${AppSyncApi.ApiId}/types/Mutation/fields/onEbUpdate'