Skip to content

Commit b59949b

Browse files
committed
serverless ms updated
1 parent c3e903a commit b59949b

20 files changed

+7978
-10812
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ node_modules
44

55
# CDK asset staging directory
66
.cdk.staging
7-
cdk.out
7+
cdk.out

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# Welcome to your CDK TypeScript project!
1+
# Welcome to your CDK TypeScript project
22

33
This is a blank project for TypeScript development with CDK.
44

55
The `cdk.json` file tells the CDK Toolkit how to execute your app.
66

77
## Useful commands
88

9-
* `npm run build` compile typescript to js
10-
* `npm run watch` watch for changes and compile
11-
* `npm run test` perform the jest unit tests
12-
* `cdk deploy` deploy this stack to your default AWS account/region
13-
* `cdk diff` compare deployed stack with current state
14-
* `cdk synth` emits the synthesized CloudFormation template
9+
* `npm run build` compile typescript to js
10+
* `npm run watch` watch for changes and compile
11+
* `npm run test` perform the jest unit tests
12+
* `cdk deploy` deploy this stack to your default AWS account/region
13+
* `cdk diff` compare deployed stack with current state
14+
* `cdk synth` emits the synthesized CloudFormation template

lib/apigateway.ts

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,55 @@ interface SwnApiGatewayProps {
88
orderingMicroservices: IFunction
99
}
1010

11-
export class SwnApiGateway extends Construct {
11+
export class SwnApiGateway extends Construct {
1212

13-
constructor(scope: Construct, id: string, props: SwnApiGatewayProps){
13+
constructor(scope: Construct, id: string, props: SwnApiGatewayProps) {
1414
super(scope, id);
1515

1616
// Product api gateway
1717
this.createProductApi(props.productMicroservice);
1818
// Basket api gateway
19-
this.createBasketApi(props.basketMicroservice);
19+
this.createBasketApi(props.basketMicroservice);
2020
// Ordering api gateway
2121
this.createOrderApi(props.orderingMicroservices);
2222
}
2323

2424
private createProductApi(productMicroservice: IFunction) {
25-
26-
// Product microservices api gateway
27-
// root name = product
28-
29-
// GET /product
30-
// POST /product
31-
32-
// Single product with id parameter
33-
// GET /product/{id}
34-
// PUT /product/{id}
35-
// DELETE /product/{id}
36-
37-
const apigw = new LambdaRestApi(this, 'productApi', {
38-
restApiName: 'Product Service',
39-
handler: productMicroservice,
40-
proxy: false
41-
});
42-
43-
const product = apigw.root.addResource('product');
44-
product.addMethod('GET'); // GET /product
45-
product.addMethod('POST'); // POST /product
46-
47-
const singleProduct = product.addResource('{id}');
48-
singleProduct.addMethod('GET'); // GET /product/{id}
49-
singleProduct.addMethod('PUT'); // PUT /product/{id}
50-
singleProduct.addMethod('DELETE'); // DELETE /product/{id}
51-
52-
return singleProduct;
25+
// Product microservices api gateway
26+
// root name = product
27+
28+
// GET /product
29+
// POST /product
30+
31+
// Single product with id parameter
32+
// GET /product/{id}
33+
// PUT /product/{id}
34+
// DELETE /product/{id}
35+
36+
const apigw = new LambdaRestApi(this, 'productApi', {
37+
restApiName: 'Product Service',
38+
handler: productMicroservice,
39+
proxy: false
40+
});
41+
42+
const product = apigw.root.addResource('product');
43+
product.addMethod('GET'); // GET /product
44+
product.addMethod('POST'); // POST /product
45+
46+
const singleProduct = product.addResource('{id}'); // product/{id}
47+
singleProduct.addMethod('GET'); // GET /product/{id}
48+
singleProduct.addMethod('PUT'); // PUT /product/{id}
49+
singleProduct.addMethod('DELETE'); // DELETE /product/{id}
5350
}
5451

5552
private createBasketApi(basketMicroservice: IFunction) {
56-
5753
// Basket microservices api gateway
5854
// root name = basket
5955

6056
// GET /basket
6157
// POST /basket
6258

63-
// Single basket with userName parameter
59+
// // Single basket with userName parameter - resource name = basket/{userName}
6460
// GET /basket/{userName}
6561
// DELETE /basket/{userName}
6662

@@ -87,10 +83,14 @@ export class SwnApiGateway extends Construct {
8783
}
8884

8985
private createOrderApi(orderingMicroservices: IFunction) {
90-
91-
// GET /order
92-
// GET /order/{userName}
93-
86+
// Ordering microservices api gateway
87+
// root name = order
88+
89+
// GET /order
90+
// GET /order/{userName}
91+
// expected request : xxx/order/swn?orderDate=timestamp
92+
// ordering ms grap input and query parameters and filter to dynamo db
93+
9494
const apigw = new LambdaRestApi(this, 'orderApi', {
9595
restApiName: 'Order Service',
9696
handler: orderingMicroservices,
@@ -107,5 +107,4 @@ export class SwnApiGateway extends Construct {
107107

108108
return singleOrder;
109109
}
110-
111110
}

lib/aws-microservices-stack.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export class AwsMicroservicesStack extends Stack {
1010
constructor(scope: Construct, id: string, props?: StackProps) {
1111
super(scope, id, props);
1212

13-
const database = new SwnDatabase(this, 'Database');
14-
13+
const database = new SwnDatabase(this, 'Database');
14+
1515
const microservices = new SwnMicroservices(this, 'Microservices', {
1616
productTable: database.productTable,
1717
basketTable: database.basketTable,
@@ -23,15 +23,15 @@ export class AwsMicroservicesStack extends Stack {
2323
basketMicroservice: microservices.basketMicroservice,
2424
orderingMicroservices: microservices.orderingMicroservice
2525
});
26-
26+
2727
const queue = new SwnQueue(this, 'Queue', {
2828
consumer: microservices.orderingMicroservice
2929
});
3030

3131
const eventbus = new SwnEventBus(this, 'EventBus', {
3232
publisherFuntion: microservices.basketMicroservice,
33-
targetQueue: queue.orderQueue
34-
});
33+
targetQueue: queue.orderQueue
34+
});
3535

3636
}
3737
}

lib/database.ts

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,50 @@ export class SwnDatabase extends Construct {
88
public readonly basketTable: ITable;
99
public readonly orderTable: ITable;
1010

11-
constructor(scope: Construct, id: string){
11+
constructor(scope: Construct, id: string) {
1212
super(scope, id);
13-
14-
//product table
15-
this.productTable = this.createProductTable();
16-
//basket table
17-
this.basketTable = this.createBasketTable();
18-
//order table
19-
this.orderTable = this.createOrderTable();
13+
14+
//product table
15+
this.productTable = this.createProductTable();
16+
//basket table
17+
this.basketTable = this.createBasketTable();
18+
//order table
19+
this.orderTable = this.createOrderTable();
2020
}
2121

22+
// Product DynamoDb Table Creation
2223
// product : PK: id -- name - description - imageFile - price - category
2324
private createProductTable() : ITable {
24-
const dynamoTable = new Table(this, 'product', {
25-
partitionKey: {
26-
name: 'id',
27-
type: AttributeType.STRING
28-
},
29-
tableName: 'product',
30-
removalPolicy: RemovalPolicy.DESTROY,
31-
billingMode: BillingMode.PAY_PER_REQUEST
32-
});
33-
return dynamoTable;
25+
const productTable = new Table(this, 'product', {
26+
partitionKey: {
27+
name: 'id',
28+
type: AttributeType.STRING
29+
},
30+
tableName: 'product',
31+
removalPolicy: RemovalPolicy.DESTROY,
32+
billingMode: BillingMode.PAY_PER_REQUEST
33+
});
34+
return productTable;
3435
}
3536

36-
// basket : PK: userName -- items (SET-MAP object) { quantity - color - price - productId - productName }
37+
// Basket DynamoDb Table Creation
38+
// basket : PK: userName -- items (SET-MAP object)
39+
// item1 - { quantity - color - price - productId - productName }
40+
// item2 - { quantity - color - price - productId - productName }
3741
private createBasketTable() : ITable {
38-
const basketTable = new Table(this, 'basket', {
39-
partitionKey: {
40-
name: 'userName',
41-
type: AttributeType.STRING,
42-
},
43-
tableName: 'basket',
44-
removalPolicy: RemovalPolicy.DESTROY,
45-
billingMode: BillingMode.PAY_PER_REQUEST
46-
});
47-
return basketTable;
42+
const basketTable = new Table(this, 'basket', {
43+
partitionKey: {
44+
name: 'userName',
45+
type: AttributeType.STRING,
46+
},
47+
tableName: 'basket',
48+
removalPolicy: RemovalPolicy.DESTROY,
49+
billingMode: BillingMode.PAY_PER_REQUEST
50+
});
51+
return basketTable;
4852
}
4953

54+
// Order DynamoDb Table Creation
5055
// order : PK: userName - SK: orderDate -- totalPrice - firstName - lastName - email - address - paymentMethod - cardInfo
5156
private createOrderTable() : ITable {
5257
const orderTable = new Table(this, 'order', {
@@ -64,4 +69,6 @@ export class SwnDatabase extends Construct {
6469
});
6570
return orderTable;
6671
}
67-
}
72+
73+
}
74+

lib/eventbus.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1-
import { Construct } from "constructs";
2-
import { IFunction } from "aws-cdk-lib/aws-lambda";
31
import { EventBus, Rule } from "aws-cdk-lib/aws-events";
4-
import { LambdaFunction, SqsQueue } from "aws-cdk-lib/aws-events-targets";
2+
import { SqsQueue } from "aws-cdk-lib/aws-events-targets";
3+
import { IFunction } from "aws-cdk-lib/aws-lambda";
54
import { IQueue } from "aws-cdk-lib/aws-sqs";
5+
import { Construct } from "constructs";
66

77
interface SwnEventBusProps {
8-
publisherFuntion: IFunction;
9-
targetQueue: IQueue;
8+
publisherFuntion: IFunction;
9+
targetQueue: IQueue;
1010
}
1111

1212
export class SwnEventBus extends Construct {
13-
14-
constructor(scope: Construct, id: string, props: SwnEventBusProps) {
15-
super(scope, id);
1613

17-
//eventbus
18-
const bus = new EventBus(this, 'SwnEventBus', {
19-
eventBusName: 'SwnEventBus'
20-
});
14+
constructor(scope: Construct, id: string, props: SwnEventBusProps) {
15+
super(scope, id);
16+
17+
//eventbus
18+
const bus = new EventBus(this, 'SwnEventBus', {
19+
eventBusName: 'SwnEventBus'
20+
});
21+
22+
const checkoutBasketRule = new Rule(this, 'CheckoutBasketRule', {
23+
eventBus: bus,
24+
enabled: true,
25+
description: 'When Basket microservice checkout the basket',
26+
eventPattern: {
27+
source: ['com.swn.basket.checkoutbasket'],
28+
detailType: ['CheckoutBasket']
29+
},
30+
ruleName: 'CheckoutBasketRule'
31+
});
2132

22-
const checkoutBasketRule = new Rule(this, 'CheckoutBasketRule', {
23-
eventBus: bus,
24-
enabled: true,
25-
description: 'When Basket microservice checkout the basket',
26-
eventPattern: {
27-
source: ['com.swn.basket.checkoutbasket'],
28-
detailType: ['CheckoutBasket']
29-
},
30-
ruleName: 'CheckoutBasketRule'
31-
});
33+
// // need to pass target to Ordering Lambda service
34+
// checkoutBasketRule.addTarget(new LambdaFunction(props.targetFuntion));
35+
36+
// need to pass target to Ordering Lambda service
37+
checkoutBasketRule.addTarget(new SqsQueue(props.targetQueue));
38+
39+
bus.grantPutEventsTo(props.publisherFuntion);
40+
// AccessDeniedException - is not authorized to perform: events:PutEvents
3241

33-
//checkoutBasketRule.addTarget(new LambdaFunction(props.targetFuntion)); // need to pass target to Ordering Lambda service
34-
checkoutBasketRule.addTarget(new SqsQueue(props.targetQueue)); // need to pass target to Ordering Lambda service
42+
}
3543

36-
bus.grantPutEventsTo(props.publisherFuntion);
37-
}
3844
}

0 commit comments

Comments
 (0)