Skip to content

Commit 8eddb13

Browse files
authored
Merge pull request #2351 from Tessot/tessot-feature-apigateway-appsync-dynamodb-sam
New serverless pattern - apigw-appsync-dynamodb
2 parents cfe6305 + f1ab1ca commit 8eddb13

File tree

12 files changed

+599
-0
lines changed

12 files changed

+599
-0
lines changed
44 KB
Loading
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Amazon API Gateway as a proxy to GraphQL API on AWS AppSync with Amazon DynamoDB as the data source
2+
3+
This pattern shows how to access a GraphQL API on AWS AppSync using API Gateway HTTP API as a proxy to the GraphQL API. Amazon API Gateway HTTP API is setup via integration to route all requests to the AppSync API GraphQL endpoint. This implementation will support all GraphQL `queries` and `mutations` defined in the AppSync API GraphQL schema however will not support `subscriptions`. To demonstrate this pattern, the template will deploy a simple Restaurant API with Amazon DynamoDB as a data source.
4+
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigateway-appsync-dynamodb-sam
6+
7+
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.
8+
9+
## Requirements
10+
11+
- [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.
12+
- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
- [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
- [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
15+
16+
## Deployment Instructions
17+
18+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
19+
```
20+
git clone https://github.com/aws-samples/serverless-patterns
21+
```
22+
2. Change directory to the pattern directory:
23+
```
24+
cd apigateway-appsync-dynamodb-sam
25+
```
26+
3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
27+
```
28+
sam deploy --guided
29+
```
30+
4. During the prompts:
31+
32+
- Enter a stack name
33+
- Enter the desired AWS Region
34+
- Allow SAM CLI to create IAM roles with the required permissions.
35+
36+
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.
37+
38+
5. Note the outputs from the SAM deployment process. Two of the outputs `AppSync API Key` and `API Gateway Endpoint` will be used to test this pattern.
39+
40+
## How it works
41+
42+
This patterns creates Amazon API Gateway HTTP API which is used as a proxy to AWS AppSync GraphQL API with Amazon DynamoDB as the data source. To demonstrate the pattern, we have built a simple Restaurant API where users can add, delete, update, get and list restaurants. All requests to the API Gateway endpoint is sent to the AppSync API endpoint where the request is fulfilled and the response sent back to the requester.
43+
44+
API Key is used as the authorization mode for the AppSync API however it is not recommended to use API Key for production application, kindly refer to other authorization modes supported by AppSync in the [documentation](https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html)
45+
46+
## Testing
47+
48+
You can easily test this pattern using any command prompt that supports the `curl` command. Refer to the outputs from deploying the SAM application which will be used for testing.
49+
50+
1. Open your command prompt
51+
2. To add a new restaurant entry, run the `curl` command below by pasting it in your command prompt. Remember to replace the values for {ApiGatewayEndpoint} and {AppSyncApiKey} which are part of the output generated after deploying the SAM template
52+
53+
```curl {ApiGatewayEndpoint} \
54+
-H "x-api-key:{AppSyncApiKey}" \
55+
-H "Content-Type: application/json" \
56+
-d '{
57+
"query": "mutation AddRestaurant($input: AddRestaurantInput!) { addRestaurant(input: $input) { cuisine name restaurantId state zip } }",
58+
"variables": {
59+
"input": {
60+
"cuisine": "Chinese",
61+
"name": "My food",
62+
"state": "NY",
63+
"zip": "234"
64+
}
65+
}
66+
}'
67+
```
68+
69+
3. To get the list of restaurants, paste the following command in your command prompt. Remember to replace the values for {ApiGatewayEndpoint} and {AppSyncApiKey} which are part of the output generated after deploying the SAM template
70+
71+
```curl {ApiGatewayEndpoint} \
72+
-H "x-api-key:{AppSyncApiKey}" \
73+
-H "Content-Type: application/json" \
74+
-d '{"query": "query MyQuery {listRestaurants {items {name state restaurantId zip cuisine }}}","variables":"{}"}'
75+
```
76+
77+
4. To get restaurant based on restaurant ID, paste the following command in your command prompt. Remember to replace the values for {ApiGatewayEndpoint} and {AppSyncApiKey} which are part of the output generated after deploying the SAM template and also {RestaurantID} for an existing restaurant you have already created.
78+
79+
```curl '{ApiGatewayEndpoint}' \
80+
-H 'x-api-key: {AppSyncApiKey}' \
81+
-H 'Content-Type: application/json' \
82+
-d '{
83+
"query": "query MyQuery($restaurantId: ID!) { getRestaurant(restaurantId: $restaurantId) { cuisine name restaurantId state zip } }",
84+
"variables": {
85+
"restaurantId": "{RestaurantID}"
86+
}
87+
}'
88+
```
89+
90+
## Cleanup
91+
92+
1. Delete the stack
93+
```bash
94+
sam delete
95+
```
96+
97+
---
98+
99+
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
100+
101+
SPDX-License-Identifier: MIT-0
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{
2+
"title": "Amazon API Gateway to AWS AppSync to Amazon DynamoDB ",
3+
"description": "Create an Amazon API Gateway HTTP API as a proxy to AWS AppSync GraphQL API with Amazon DynamoDB as the API data source",
4+
"language": "YAML",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This patterns creates Amazon API Gateway HTTP API which is used as a proxy to AWS AppSync GraphQL API with Amazon DynamoDB as the data source.",
11+
"To demonstrate the pattern, we have built a simple Restaurant API where users can add, delete, update, get and list restaurants. All requests to the API Gateway endpoint is sent to the AppSync API endpoint where the request is fulfilled.",
12+
"This implementation will support all GraphQL queries and mutations defined in the AppSync API GraphQL schema however will not support subscriptions"
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-appsync-dynamodb-sam",
18+
"templateURL": "serverless-patterns/apigw-appsync-dynamodb-sam",
19+
"projectFolder": "apigw-appsync-dynamodb-sam",
20+
"templateFile": "template.yaml"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "API Gateway documentation:",
27+
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html"
28+
},
29+
{
30+
"text": "AppSync documentation:",
31+
"link": "https://docs.aws.amazon.com/appsync/latest/devguide/what-is-appsync.html"
32+
},
33+
{
34+
"text": "DynamoDB documentation:",
35+
"link": "https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html"
36+
}
37+
]
38+
},
39+
"deploy": {
40+
"text": [
41+
"sam deploy --guided"
42+
]
43+
},
44+
"testing": {
45+
"text": [
46+
"See the GitHub repo for detailed testing instructions."
47+
]
48+
},
49+
"cleanup": {
50+
"text": [
51+
"Delete the stack: <code>sam delete</code>."
52+
]
53+
},
54+
"authors": [
55+
{
56+
"name": "Ozioma Uzoegwu",
57+
"image": "./Ouzoegwu.jpeg",
58+
"bio": "I am a Principal Solutions Architect working at AWS",
59+
"linkedin": "ouzoegwu",
60+
"twitter": "iam_tessot"
61+
}
62+
],
63+
"patternArch": {
64+
"icon1": {
65+
"x": 20,
66+
"y": 50,
67+
"service": "apigw",
68+
"label": "Amazon API Gateway"
69+
},
70+
"icon2": {
71+
"x": 50,
72+
"y": 50,
73+
"service": "appsync",
74+
"label": "AWS AppSync"
75+
},
76+
"icon3": {
77+
"x": 80,
78+
"y": 50,
79+
"service": "dynamodb",
80+
"label": "Amazon DynamoDB"
81+
},
82+
"line1": {
83+
"from": "icon1",
84+
"to": "icon2",
85+
"label": ""
86+
},
87+
"line2": {
88+
"from": "icon2",
89+
"to": "icon3",
90+
"label": ""
91+
}
92+
}
93+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"title": "Amazon API Gateway to AWS AppSync to Amazon DynamoDB ",
3+
"description": "Create an Amazon API Gateway HTTP API as a proxy to AWS AppSync GraphQL API with Amazon DynamoDB as the API data source",
4+
"language": "Yaml",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This patterns creates Amazon API Gateway HTTP API which is used as a proxy to AWS AppSync GraphQL API with Amazon DynamoDB as the data source.",
11+
"To demonstrate the pattern, we have built a simple Restaurant API where users can add, delete, update, get and list restaurants. All requests to the API Gateway endpoint is sent to the AppSync API endpoint where the request is fulfilled.",
12+
"This implementation will support all GraphQL queries and mutations defined in the AppSync API GraphQL schema however will not support subscriptions"
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-appsync-dynamodb-sam",
18+
"templateURL": "serverless-patterns/apigw-appsync-dynamodb-sam",
19+
"projectFolder": "apigw-appsync-dynamodb-sam",
20+
"templateFile": "template.yaml"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "API Gateway documentation:",
27+
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html"
28+
},
29+
{
30+
"text": "AppSync documentation:",
31+
"link": "https://docs.aws.amazon.com/appsync/latest/devguide/what-is-appsync.html"
32+
},
33+
{
34+
"text": "DynamoDB documentation:",
35+
"link": "https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html"
36+
}
37+
]
38+
},
39+
"deploy": {
40+
"text": [
41+
"sam deploy --guided"
42+
]
43+
},
44+
"testing": {
45+
"text": [
46+
"See the GitHub repo for detailed testing instructions."
47+
]
48+
},
49+
"cleanup": {
50+
"text": [
51+
"Delete the stack: <code>sam delete</code>."
52+
]
53+
},
54+
"authors": [
55+
{
56+
"name": "Ozioma Uzoegwu",
57+
"image": "./Ouzoegwu.jpeg",
58+
"bio": "I am a Principal Solutions Architect working at AWS",
59+
"linkedin": "ouzoegwu",
60+
"twitter": "iam_tessot"
61+
}
62+
]
63+
}
64+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
input AddRestaurantInput {
2+
name: String!
3+
state: String
4+
zip: String
5+
cuisine: CuisineType!
6+
}
7+
8+
enum CuisineType {
9+
Multi
10+
Indian
11+
Chinese
12+
Italian
13+
Thai
14+
American
15+
Continental
16+
}
17+
18+
input DeleteRestaurantInput {
19+
restaurantId: ID!
20+
}
21+
22+
type Restaurant {
23+
restaurantId: ID!
24+
name: String!
25+
state: String
26+
zip: String
27+
cuisine: CuisineType
28+
}
29+
30+
type RestaurantConnection {
31+
items: [Restaurant]
32+
nextToken: String
33+
}
34+
35+
input UpdateRestaurantInput {
36+
restaurantId: ID!
37+
name: String
38+
state: String
39+
zip: String
40+
cuisine: CuisineType
41+
}
42+
43+
type Mutation {
44+
addRestaurant(input: AddRestaurantInput!): Restaurant
45+
updateRestaurant(input: UpdateRestaurantInput!): Restaurant
46+
deleteRestaurant(input: DeleteRestaurantInput!): Restaurant
47+
}
48+
49+
type Query {
50+
listRestaurants(limit: Int, nextToken: String): RestaurantConnection
51+
getRestaurant(restaurantId: ID!): Restaurant
52+
}
53+
54+
schema {
55+
query: Query
56+
mutation: Mutation
57+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
openapi: "3.0.1"
2+
info:
3+
title: "HTTP API proxy to Restaurant API"
4+
version: "1.0"
5+
paths:
6+
/restaurant:
7+
post:
8+
responses:
9+
default:
10+
description: "Default response for POST /restaurant"
11+
x-amazon-apigateway-integration:
12+
payloadFormatVersion: "1.0"
13+
type: "http_proxy"
14+
httpMethod: "ANY"
15+
uri:
16+
Fn::GetAtt: [AppsyncGraphQLApi, GraphQLUrl]
17+
connectionType: "INTERNET"
18+
x-amazon-apigateway-importexport-version: "1.0"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as ddb from '@aws-appsync/utils/dynamodb';
2+
3+
export function request(ctx) {
4+
const key = { restaurantId: util.autoId() };
5+
const item = ctx.args.input;
6+
const condition = { restaurantId: { attributeExists: false } };
7+
return ddb.put({ key, item, condition });
8+
}
9+
10+
export const response = (ctx) => ctx.result;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as ddb from '@aws-appsync/utils/dynamodb';
2+
3+
export const request = (ctx) => ddb.remove({ key: { restaurantId: ctx.args.input.restaurantId } });
4+
export const response = (ctx) => ctx.result;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as ddb from '@aws-appsync/utils/dynamodb';
2+
3+
export const request = (ctx) => ddb.get({ key: { restaurantId: ctx.args.restaurantId } });
4+
5+
export const response = (ctx) => ctx.result;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as ddb from '@aws-appsync/utils/dynamodb';
2+
3+
export function request(ctx) {
4+
const { limit = 10, nextToken } = ctx.args;
5+
return ddb.scan({ limit, nextToken });
6+
}
7+
8+
export function response(ctx) {
9+
const { items, nextToken } = ctx.result;
10+
return { items: items ?? [], nextToken };
11+
}

0 commit comments

Comments
 (0)