|
1 | 1 | import * as cdk from "@aws-cdk/core";
|
2 |
| -import * as cdkCore from "@aws-cdk/core"; |
3 |
| -import * as appsync from "@aws-cdk/aws-appsync"; |
4 |
| -import * as dynamoDb from "@aws-cdk/aws-dynamoDb"; |
5 |
| -import * as lambda from "@aws-cdk/aws-lambda"; |
6 |
| -// import { Code, Function, Runtime } from '@aws-cdk/aws-lambda'; |
7 |
| -// import * as sqs from '@aws-cdk/aws-sqs'; |
| 2 | +import { GraphQlAPIConstruct } from "./graph-api.construct"; |
| 3 | +import { LambdaConstruct } from "./lambda.contruct"; |
| 4 | +import { DynamoDBConstruct } from "./dynamo-db.construct"; |
8 | 5 |
|
9 | 6 | export class ChatApiAppSyncStack extends cdk.Stack {
|
10 | 7 | constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
|
11 | 8 | super(scope, id, props);
|
12 | 9 |
|
13 |
| - const MessagesTable = new dynamoDb.Table(this, "MessagesTable", { |
14 |
| - billingMode: dynamoDb.BillingMode.PAY_PER_REQUEST, |
15 |
| - partitionKey: { name: "id", type: dynamoDb.AttributeType.STRING }, |
16 |
| - sortKey: { name: "conversationId", type: dynamoDb.AttributeType.STRING }, |
17 |
| - removalPolicy: cdkCore.RemovalPolicy.DESTROY, |
18 |
| - tableName: "messages-table", |
19 |
| - }); |
| 10 | + const dynamoDBConstruct = new DynamoDBConstruct( |
| 11 | + this, |
| 12 | + `${id}-dynamo-db`, |
| 13 | + props |
| 14 | + ); |
20 | 15 |
|
21 |
| - const ConversationsTable = new dynamoDb.Table(this, "ConversationsTable", { |
22 |
| - billingMode: dynamoDb.BillingMode.PAY_PER_REQUEST, |
23 |
| - partitionKey: { name: "id", type: dynamoDb.AttributeType.STRING }, |
24 |
| - removalPolicy: cdkCore.RemovalPolicy.DESTROY, |
25 |
| - tableName: "conversations-table", |
26 |
| - }); |
| 16 | + const lambdaConstruct = new LambdaConstruct(this, `${id}-lambda`, props); |
27 | 17 |
|
28 |
| - const UsersTable = new dynamoDb.Table(this, "UsersTable", { |
29 |
| - billingMode: dynamoDb.BillingMode.PAY_PER_REQUEST, |
30 |
| - partitionKey: { name: "id", type: dynamoDb.AttributeType.STRING }, |
31 |
| - removalPolicy: cdkCore.RemovalPolicy.DESTROY, |
32 |
| - tableName: "users-table", |
33 |
| - }); |
| 18 | + const graphAPIContruct = new GraphQlAPIConstruct( |
| 19 | + this, |
| 20 | + `${id}-graph-api`, |
| 21 | + props |
| 22 | + ); |
34 | 23 |
|
35 |
| - const api = new appsync.GraphqlApi(this, "ChatsApi", { |
36 |
| - name: "chat-api", |
37 |
| - logConfig: { |
38 |
| - fieldLogLevel: appsync.FieldLogLevel.ERROR, |
39 |
| - }, |
40 |
| - schema: appsync.Schema.fromAsset("src/graphql/schema.graphql"), |
41 |
| - authorizationConfig: { |
42 |
| - defaultAuthorization: { |
43 |
| - authorizationType: appsync.AuthorizationType.API_KEY, |
44 |
| - apiKeyConfig: { |
45 |
| - expires: cdk.Expiration.after(cdk.Duration.days(365)), |
46 |
| - }, |
47 |
| - }, |
48 |
| - }, |
49 |
| - // xrayEnabled: true, |
50 |
| - }); |
| 24 | + // enable the Lambda function to access the DynamoDB table (using IAM) |
| 25 | + dynamoDBConstruct.messagesTable.grantFullAccess(lambdaConstruct.lambda); |
| 26 | + dynamoDBConstruct.notificationTable.grantFullAccess(lambdaConstruct.lambda); |
| 27 | + dynamoDBConstruct.conversationsTable.grantFullAccess( |
| 28 | + lambdaConstruct.lambda |
| 29 | + ); |
| 30 | + dynamoDBConstruct.usersTable.grantFullAccess(lambdaConstruct.lambda); |
| 31 | + |
| 32 | + // Create an environment variable that we will use in the function code |
| 33 | + lambdaConstruct.lambda.addEnvironment( |
| 34 | + "MESSAGES_TABLE", |
| 35 | + dynamoDBConstruct.messagesTable.tableName |
| 36 | + ); |
| 37 | + lambdaConstruct.lambda.addEnvironment( |
| 38 | + "CONVERSATIONS_TABLE", |
| 39 | + dynamoDBConstruct.conversationsTable.tableName |
| 40 | + ); |
| 41 | + lambdaConstruct.lambda.addEnvironment( |
| 42 | + "USERS_TABLE", |
| 43 | + dynamoDBConstruct.usersTable.tableName |
| 44 | + ); |
| 45 | + lambdaConstruct.lambda.addEnvironment( |
| 46 | + "NOTIFICATIONS_TABLE", |
| 47 | + dynamoDBConstruct.notificationTable.tableName |
| 48 | + ); |
51 | 49 |
|
52 | 50 | // Prints out the AppSync GraphQL endpoint to the terminal
|
53 | 51 | new cdk.CfnOutput(this, "GraphQLAPIURL", {
|
54 |
| - value: api.graphqlUrl, |
| 52 | + value: graphAPIContruct.api.graphqlUrl, |
55 | 53 | });
|
56 | 54 |
|
57 | 55 | // Prints out the AppSync GraphQL API key to the terminal
|
58 | 56 | new cdk.CfnOutput(this, "GraphQLAPIKey", {
|
59 |
| - value: api.apiKey || "", |
| 57 | + value: graphAPIContruct.api.apiKey || "", |
60 | 58 | });
|
61 | 59 |
|
62 | 60 | // Prints out the stack region to the terminal
|
63 | 61 | new cdk.CfnOutput(this, "Stack Region", {
|
64 | 62 | value: this.region,
|
65 | 63 | });
|
66 |
| - |
67 |
| - // const chatsLambda = new lambda.Function(this, "AppSyncChatRouteHandler", { |
68 |
| - // runtime: lambda.Runtime.NODEJS_12_X, |
69 |
| - // handler: "routes.handler", |
70 |
| - // code: lambda.Code.fromAsset("src"), |
71 |
| - // memorySize: 1024, |
72 |
| - // }); |
73 |
| - |
74 |
| - // // Set the new Lambda function as a data source for the AppSync API |
75 |
| - // const lambdaDs = api.addLambdaDataSource("lambdaDatasource", chatsLambda); |
76 |
| - |
77 |
| - // lambdaDs.createResolver({ |
78 |
| - // typeName: "Query", |
79 |
| - // fieldName: "getConversations", |
80 |
| - // }); |
81 |
| - |
82 |
| - // lambdaDs.createResolver({ |
83 |
| - // typeName: "Query", |
84 |
| - // fieldName: "getConversation", |
85 |
| - // }); |
86 |
| - |
87 |
| - // lambdaDs.createResolver({ |
88 |
| - // typeName: "Mutation", |
89 |
| - // fieldName: "addConversation", |
90 |
| - // }); |
91 |
| - |
92 |
| - // lambdaDs.createResolver({ |
93 |
| - // typeName: "Mutation", |
94 |
| - // fieldName: "addMessage", |
95 |
| - // }); |
96 |
| - |
97 |
| - // lambdaDs.createResolver({ |
98 |
| - // typeName: "Mutation", |
99 |
| - // fieldName: "addUser", |
100 |
| - // }); |
101 |
| - |
102 |
| - // enable the Lambda function to access the DynamoDB table (using IAM) |
103 |
| - // MessagesTable.grantFullAccess(chatsLambda); |
104 |
| - // ConversationsTable.grantFullAccess(chatsLambda); |
105 |
| - // UsersTable.grantFullAccess(chatsLambda); |
106 |
| - |
107 |
| - // // Create an environment variable that we will use in the function code |
108 |
| - // chatsLambda.addEnvironment("MESSAGES_TABLE", MessagesTable.tableName); |
109 |
| - // chatsLambda.addEnvironment( |
110 |
| - // "CONVERSATIONS_TABLE", |
111 |
| - // ConversationsTable.tableName |
112 |
| - // ); |
113 |
| - // chatsLambda.addEnvironment("USERS_TABLE", UsersTable.tableName); |
114 | 64 | }
|
115 | 65 | }
|
0 commit comments