Skip to content

Commit 479c012

Browse files
committed
Update
1 parent e5e7cce commit 479c012

File tree

10 files changed

+202
-7
lines changed

10 files changed

+202
-7
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
.aws-sam
2-
packaged.yaml
1+
shopping-cart-app/.aws-sam
2+
shopping-cart-app/packaged.yaml
3+
results.bin
34

45
# Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
56

Monitoring-Debugging/CloudWatch-Custom-Metrics/README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,31 @@ sam deploy \
3737

3838
## Locate API Gateway Endpoint URLs
3939

40-
1. Open the AWS CloudFormation console at [https://console\.aws\.amazon\.com/cloudformation](https://console.aws.amazon.com/cloudformation/)\.
40+
1. Open the AWS CloudFormation console at <https://console.aws.amazon.com/cloudformation>.
4141
1. Choose the AWS CloudFormation stack that you created in the preceding step from the list shown\.
4242
1. Under **Outputs**, note the API Gateway endpoint URLs.
4343
1. Browse each and observe the JSON responses.
4444

4545
## Generate Traffic
4646

47-
Using the API Gateway endpoint URLs in the previous step, generate load against each of these endpoints.
47+
Using the API Gateway endpoint URLs in the previous step, generate traffic against each of these endpoints.
48+
49+
Run an HTTP testing tool like [vegeta](https://github.com/tsenart/vegeta) to generate traffic to your API gateway endpoints:
4850

4951
Modify `URLs.txt` to use the endpoint URLs in your account.
5052

51-
Run [Apache Bench](http://httpd.apache.org/docs/2.4/programs/ab.html) using [parallel](http://www.gnu.org/software/parallel/):
53+
Run a test for 60 minutes:
5254

5355
```sh
54-
cat URLs.txt | parallel 'ab -c 350 -n 20000 {}'
56+
cat URLS.txt | vegeta attack -duration=60m | tee results.bin | vegeta report
5557
```
5658

57-
## View Custom Metrics
59+
## View Custom Metrics
60+
61+
You may view custom metric data while a load test is in progress.
62+
63+
1. Open the CloudWatch console at <https://console.aws.amazon.com/cloudwatch>.
64+
1. Navigate to **Metrics**.
65+
1. Under **All metrics**, select **ShoppingCartApp**.
66+
1. Select **Metrics with no dimensions**.
67+
1. Select **ItemsAddedToCart**, **OrderTotal**, and **ViewProduct**.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GET https://q4mss37szc.execute-api.us-east-2.amazonaws.com/Prod/cart/add
2+
GET https://q4mss37szc.execute-api.us-east-2.amazonaws.com/Prod/cart/purchase
3+
GET https://q4mss37szc.execute-api.us-east-2.amazonaws.com/Prod/products/123456
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import json
2+
import random
3+
4+
import boto3
5+
6+
cloudwatch = boto3.client('cloudwatch')
7+
8+
9+
def lambda_handler(event, context):
10+
11+
cloudwatch.put_metric_data(
12+
MetricData=[
13+
{
14+
'MetricName': 'ItemsAddedToCart',
15+
'Value': random.randint(1, 20)
16+
},
17+
],
18+
Namespace='ShoppingCartApp'
19+
)
20+
21+
return {
22+
"statusCode": 200,
23+
"body": json.dumps(
24+
[
25+
{"itemID": "123456"},
26+
{"itemID": "234567"},
27+
{"itemID": "345678"}
28+
]
29+
),
30+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boto3
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import decimal
3+
import random
4+
5+
import boto3
6+
7+
cloudwatch = boto3.client('cloudwatch')
8+
9+
10+
def lambda_handler(event, context):
11+
12+
cloudwatch.put_metric_data(
13+
MetricData=[
14+
{
15+
'MetricName': 'OrderTotal',
16+
'Value': decimal.Decimal(random.randrange(100, 50000))/100
17+
},
18+
],
19+
Namespace='ShoppingCartApp'
20+
)
21+
22+
return {
23+
"statusCode": 200,
24+
"body": json.dumps({
25+
"OrderID": "123456",
26+
"OrderTotal": "543.21",
27+
}),
28+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boto3
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: Shopping cart app
4+
5+
Globals:
6+
Function:
7+
Handler: app.lambda_handler
8+
Runtime: python3.7
9+
Tracing: Active
10+
Timeout: 3
11+
12+
Resources:
13+
14+
AddToCartFunction:
15+
Type: AWS::Serverless::Function
16+
Properties:
17+
CodeUri: add_to_cart/
18+
Events:
19+
AddToCart:
20+
Type: Api
21+
Properties:
22+
Path: /cart/add
23+
Method: get
24+
Policies:
25+
- Statement:
26+
- Sid: CloudWatchPutMetricDataPolicy
27+
Effect: Allow
28+
Action:
29+
- cloudwatch:PutMetricData
30+
Resource: '*'
31+
32+
PurchaseFunction:
33+
Type: AWS::Serverless::Function
34+
Properties:
35+
CodeUri: purchase/
36+
Events:
37+
Purchase:
38+
Type: Api
39+
Properties:
40+
Path: /cart/purchase
41+
Method: get
42+
Policies:
43+
- Statement:
44+
- Sid: CloudWatchPutMetricDataPolicy
45+
Effect: Allow
46+
Action:
47+
- cloudwatch:PutMetricData
48+
Resource: '*'
49+
50+
ViewProductFunction:
51+
Type: AWS::Serverless::Function
52+
Properties:
53+
CodeUri: view_product/
54+
Events:
55+
ViewProduct:
56+
Type: Api
57+
Properties:
58+
Path: /products/123456
59+
Method: get
60+
Policies:
61+
- Statement:
62+
- Sid: CloudWatchPutMetricDataPolicy
63+
Effect: Allow
64+
Action:
65+
- cloudwatch:PutMetricData
66+
Resource: '*'
67+
68+
Outputs:
69+
AddToCartApi:
70+
Description: "API Gateway endpoint URL for Prod stage for AddToCart function"
71+
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/cart/add"
72+
AddToCartFunction:
73+
Description: "AddToCart Lambda Function ARN"
74+
Value: !GetAtt AddToCartFunction.Arn
75+
AddToCartFunctionIamRole:
76+
Description: "Implicit IAM Role created for AddToCart function"
77+
Value: !GetAtt AddToCartFunctionRole.Arn
78+
PurchaseApi:
79+
Description: "API Gateway endpoint URL for Prod stage for Purchase function"
80+
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/cart/purchase"
81+
PurchaseFunction:
82+
Description: "Purchase Lambda Function ARN"
83+
Value: !GetAtt PurchaseFunction.Arn
84+
PurchaseFunctionIamRole:
85+
Description: "Implicit IAM Role created for Purchase function"
86+
Value: !GetAtt PurchaseFunctionRole.Arn
87+
ViewProductApi:
88+
Description: "API Gateway endpoint URL for Prod stage for ViewProduct function"
89+
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/products/123456"
90+
ViewProductFunction:
91+
Description: "ViewProduct Lambda Function ARN"
92+
Value: !GetAtt ViewProductFunction.Arn
93+
ViewProductFunctionIamRole:
94+
Description: "Implicit IAM Role created for ViewProduct function"
95+
Value: !GetAtt ViewProductFunctionRole.Arn
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import json
2+
3+
import boto3
4+
5+
cloudwatch = boto3.client('cloudwatch')
6+
7+
8+
def lambda_handler(event, context):
9+
10+
cloudwatch.put_metric_data(
11+
MetricData=[
12+
{
13+
'MetricName': 'ViewProduct',
14+
'Value': 1
15+
},
16+
],
17+
Namespace='ShoppingCartApp'
18+
)
19+
20+
return {
21+
"statusCode": 200,
22+
"body": json.dumps({
23+
"ProductID": "123456",
24+
}),
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boto3

0 commit comments

Comments
 (0)