Skip to content

Commit a7f5208

Browse files
authored
Uploaded lesson 1 exercises (code+solution)
1 parent 9c9d623 commit a7f5208

File tree

8 files changed

+497
-0
lines changed

8 files changed

+497
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": "logs:CreateLogGroup",
7+
"Resource": "arn:aws:logs:us-east-1:*:*"
8+
},
9+
{
10+
"Effect": "Allow",
11+
"Action": [
12+
"logs:CreateLogStream",
13+
"logs:PutLogEvents"
14+
],
15+
"Resource": [
16+
"arn:aws:logs:us-east-1:*:log-group:/aws/lambda/chaos-monkey:*"
17+
]
18+
},
19+
{
20+
"Effect": "Allow",
21+
"Action": "cloudwatch:PutMetricData",
22+
"Resource": "*"
23+
}
24+
]
25+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const AWS = require('aws-sdk')
2+
const axios = require('axios')
3+
4+
// Name of a service, any string
5+
const serviceName = process.env.SERVICE_NAME
6+
// URL of a service to test
7+
const url = process.env.URL
8+
9+
// CloudWatch client
10+
const cloudwatch = new AWS.CloudWatch();
11+
12+
exports.handler = async (event) => {
13+
let endTime
14+
let requestWasSuccessful
15+
16+
const startTime = timeInMs()
17+
try {
18+
await axios.get(url)
19+
requestWasSuccessful = true
20+
} catch (e) {
21+
requestWasSuccessful = false
22+
} finally {
23+
endTime = timeInMs()
24+
}
25+
26+
const totalTime = endTime - startTime
27+
28+
await cloudwatch.putMetricData({
29+
MetricData: [
30+
{
31+
MetricName: 'Success',
32+
Dimensions: [
33+
{
34+
Name: 'ServiceName',
35+
Value: serviceName
36+
}
37+
],
38+
Unit: 'Count',
39+
Value: requestWasSuccessful ? 1 : 0
40+
}
41+
],
42+
Namespace: 'Udacity/Serveless'
43+
}).promise()
44+
45+
await cloudwatch.putMetricData({
46+
MetricData: [
47+
{
48+
MetricName: 'Latency',
49+
Dimensions: [
50+
{
51+
Name: 'ServiceName',
52+
Value: serviceName
53+
}
54+
],
55+
Unit: 'Milliseconds',
56+
Value: totalTime
57+
}
58+
],
59+
Namespace: 'Udacity/Serveless'
60+
}).promise()
61+
}
62+
63+
function timeInMs() {
64+
return new Date().getTime()
65+
}

course-04/exercises/lesson-1/solution/package-lock.json

Lines changed: 151 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "serverless-http-metrics",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"package": "zip -r http-metrics.zip .",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"axios": "^0.18.0"
14+
},
15+
"devDependencies": {
16+
"aws-sdk": "^2.432.0"
17+
}
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": "logs:CreateLogGroup",
7+
"Resource": "arn:aws:logs:us-east-1:*:*"
8+
},
9+
{
10+
"Effect": "Allow",
11+
"Action": [
12+
"logs:CreateLogStream",
13+
"logs:PutLogEvents"
14+
],
15+
"Resource": [
16+
"arn:aws:logs:us-east-1:*:log-group:/aws/lambda/chaos-monkey:*"
17+
]
18+
},
19+
{
20+
"Effect": "Allow",
21+
"Action": "cloudwatch:PutMetricData",
22+
"Resource": "*"
23+
}
24+
]
25+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const AWS = require('aws-sdk')
2+
const axios = require('axios')
3+
4+
// Name of a service, any string
5+
const serviceName = process.env.SERVICE_NAME
6+
// URL of a service to test
7+
const url = process.env.URL
8+
9+
// CloudWatch client
10+
const cloudwatch = new AWS.CloudWatch();
11+
12+
exports.handler = async (event) => {
13+
// TODO: Use these variables to record metric values
14+
let endTime
15+
let requestWasSuccessful
16+
17+
const startTime = timeInMs()
18+
await axios.get(url)
19+
20+
// Example of how to write a single data point
21+
// await cloudwatch.putMetricData({
22+
// MetricData: [
23+
// {
24+
// MetricName: 'MetricName', // Use different metric names for different values, e.g. 'Latency' and 'Successful'
25+
// Dimensions: [
26+
// {
27+
// Name: 'ServiceName',
28+
// Value: serviceName
29+
// }
30+
// ],
31+
// Unit: '', // 'Count' or 'Milliseconds'
32+
// Value: 0 // Total value
33+
// }
34+
// ],
35+
// Namespace: 'Udacity/Serveless'
36+
// }).promise()
37+
38+
// TODO: Record time it took to get a response
39+
// TODO: Record if a response was successful or not
40+
}
41+
42+
function timeInMs() {
43+
return new Date().getTime()
44+
}

0 commit comments

Comments
 (0)