Skip to content

Commit 7827395

Browse files
authored
Merge pull request #2844 from kakakakakku/appsync-notify-subscribers-of-database-updates
appsync-notify-subscribers-of-database-updates: Update runtime to nodejs22.x
2 parents 18f5a10 + 8cfd57a commit 7827395

File tree

5 files changed

+58
-31
lines changed

5 files changed

+58
-31
lines changed

appsync-notify-subscribers-of-database-updates/1-http/template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Parameters:
1717
Client:
1818
Description: Client website for authentication redirects and cors (must start with https://)
1919
Type: String
20-
Default: https://myapp.com
20+
Default: https://aws.amazon.com
2121

2222
Resources:
2323
# Creates a nested stack with the required Cognito requirements

appsync-notify-subscribers-of-database-updates/3-lambda/externalDepsLayer/nodejs/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
"author": "",
1010
"license": "ISC",
1111
"dependencies": {
12-
"aws-appsync": "^4.1.10",
12+
"@aws-sdk/signature-v4": "^3.0.0",
13+
"@smithy/protocol-http": "^4.0.0",
14+
"@aws-sdk/credential-provider-node": "^3.0.0",
15+
"@aws-crypto/sha256-js": "^5.0.0",
1316
"axios": "^1.8.2",
1417
"graphql": "^16.10.0",
15-
"graphql-tag": "^2.12.6",
16-
"isomorphic-fetch": "^3.0.0"
18+
"graphql-tag": "^2.12.6"
1719
}
1820
}

appsync-notify-subscribers-of-database-updates/3-lambda/src/lambdaUsesIAM.js

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
require('isomorphic-fetch');
2-
const AUTH_TYPE = require('aws-appsync').AUTH_TYPE;
3-
const AWSAppSyncClient = require('aws-appsync').default;
1+
const { SignatureV4 } = require('@aws-sdk/signature-v4');
2+
const { HttpRequest } = require('@smithy/protocol-http');
3+
const { defaultProvider } = require('@aws-sdk/credential-provider-node');
4+
const { Sha256 } = require('@aws-crypto/sha256-js');
45
const gql = require('graphql-tag');
5-
6-
const config = {
7-
url: process.env.APPSYNC_ENDPOINT,
8-
region: process.env.AWS_REGION,
9-
auth: {
10-
type: AUTH_TYPE.AWS_IAM,
11-
credentials: {
12-
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
13-
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
14-
sessionToken: process.env.AWS_SESSION_TOKEN
15-
},
16-
},
17-
disableOffline: true
18-
};
6+
const graphql = require('graphql');
7+
const { print } = graphql;
198

209
const createTodo = gql`
2110
mutation MyMutation(
@@ -38,21 +27,48 @@ const createTodo = gql`
3827
}
3928
`;
4029

41-
const client = new AWSAppSyncClient(config);
30+
const signer = new SignatureV4({
31+
credentials: defaultProvider(),
32+
region: process.env.AWS_REGION,
33+
service: 'appsync',
34+
sha256: Sha256
35+
});
4236

4337
exports.handler = async function (event) {
4438
console.log("event ", event);
4539

4640
try {
47-
const result = await client.mutate({
48-
mutation: createTodo,
49-
variables: {
50-
orderId: "123",
51-
prevStatus: "PENDING",
52-
status: "IN_PROGRESS",
53-
updatedAt: "2021-10-07T20:38:18.683Z"
54-
}
41+
const url = new URL(process.env.APPSYNC_ENDPOINT);
42+
const query = print(createTodo);
43+
const variables = {
44+
orderId: "123",
45+
prevStatus: "PENDING",
46+
status: "IN_PROGRESS",
47+
updatedAt: "2021-10-07T20:38:18.683Z"
48+
};
49+
50+
const requestBody = JSON.stringify({ query, variables });
51+
52+
const request = new HttpRequest({
53+
method: 'POST',
54+
headers: {
55+
'Content-Type': 'application/json',
56+
host: url.host
57+
},
58+
hostname: url.hostname,
59+
path: url.pathname,
60+
body: requestBody
5561
});
62+
63+
const signedRequest = await signer.sign(request);
64+
65+
const response = await fetch(process.env.APPSYNC_ENDPOINT, {
66+
method: signedRequest.method,
67+
headers: signedRequest.headers,
68+
body: requestBody
69+
});
70+
71+
const result = await response.json();
5672
console.log("result ", result);
5773
} catch (error) {
5874
console.log("error ", error);

appsync-notify-subscribers-of-database-updates/3-lambda/template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Resources:
3939
# delete old version of layer
4040
RetentionPolicy: Delete
4141
Metadata:
42-
BuildMethod: nodejs16.x
42+
BuildMethod: nodejs22.x
4343

4444
AppSyncLambdaUseIAM:
4545
Type: 'AWS::Serverless::Function'

appsync-notify-subscribers-of-database-updates/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ Important: this application uses various AWS services and there are costs associ
7474
```
7575
sam deploy -g --capabilities CAPABILITY_IAM
7676
```
77+
13. During the prompts:
78+
* Enter a stack name
79+
* Select the desired AWS Region
80+
* Enter a GraphQLApiEndpoint
81+
* Enter an AppSyncApiKey
82+
* Enter a GraphQLApiId
83+
* Enter an OrdersEventBusName
84+
* Enter an OrdersEventBusArn
85+
* Allow SAM to create roles with the required permissions.
7786
7887
## Testing Part 1 - notify via HTTP Request
7988

0 commit comments

Comments
 (0)