From 86d9246b1f3ebaed7380b11a448249f33fb7d8af Mon Sep 17 00:00:00 2001 From: Abhishek Veeramalla Date: Wed, 19 Jul 2023 22:54:13 +0530 Subject: [PATCH] add cloud watch metrics --- day-16/README.md | 40 +++++++++++ .../custom_metrics_demo/cloudwatch_metrics.py | 66 +++++++++++++++++++ day-16/custom_metrics_demo/requirements.txt | 2 + 3 files changed, 108 insertions(+) create mode 100644 day-16/README.md create mode 100644 day-16/custom_metrics_demo/cloudwatch_metrics.py create mode 100644 day-16/custom_metrics_demo/requirements.txt diff --git a/day-16/README.md b/day-16/README.md new file mode 100644 index 00000000..b9d985a6 --- /dev/null +++ b/day-16/README.md @@ -0,0 +1,40 @@ +# AWS CLOUD WATCH + +Welcome back to our "30 Days AWS Zero to Hero" series. Today, on Day 16, we will deep dive into AWS CloudWatch. + +What is AWS CloudWatch? + +AWS CloudWatch is a powerful monitoring and observability service provided by Amazon Web Services. It enables you to gain insights into the performance, health, and operational aspects of your AWS resources and applications. CloudWatch collects and tracks metrics, collects and monitors log files, and sets alarms to alert you on certain conditions. + +Advantages of AWS CloudWatch: + + Comprehensive Monitoring: CloudWatch allows you to monitor various AWS resources such as EC2 instances, RDS databases, Lambda functions, and more. You get a unified view of your entire AWS infrastructure. + + Real-Time Metrics: It provides real-time monitoring of metrics, allowing you to respond quickly to any issues or anomalies that might arise. + + Automated Actions: With CloudWatch Alarms, you can set up automated actions like triggering an Auto Scaling group to scale in or out based on certain conditions. + + Log Insights: CloudWatch Insights lets you analyze and search log data from various AWS services, making it easier to troubleshoot problems and identify trends. + + Dashboards and Visualization: Create custom dashboards to visualize your application and infrastructure metrics in one place, making it easier to understand the overall health of your system. + +Problem Solving with AWS CloudWatch: + +CloudWatch helps address several critical challenges, including: + + Resource Utilization: Tracking resource utilization and performance metrics to optimize your AWS infrastructure efficiently. + Proactive Monitoring: Identifying and resolving issues before they impact your applications or users. + Troubleshooting: Analyzing logs and metrics to troubleshoot problems and reduce downtime. + Scalability: Automatically scaling resources based on demand to ensure optimal performance and cost efficiency. + +Practical Use Cases of AWS CloudWatch: + + Auto Scaling: CloudWatch can trigger Auto Scaling actions based on defined thresholds. For example, you can automatically scale in or out based on CPU utilization or request counts. + + Resource Monitoring: Monitor EC2 instances, RDS databases, DynamoDB tables, and other AWS resources to gain insights into their performance and health. + + Application Insights: Track application-specific metrics to monitor the performance of your applications and identify potential bottlenecks. + + Log Analysis: Use CloudWatch Logs Insights to analyze log data, identify patterns, and troubleshoot issues in real-time. + + Billing and Cost Monitoring: CloudWatch can help you monitor your AWS billing and usage patterns, enabling you to optimize costs. \ No newline at end of file diff --git a/day-16/custom_metrics_demo/cloudwatch_metrics.py b/day-16/custom_metrics_demo/cloudwatch_metrics.py new file mode 100644 index 00000000..70d64180 --- /dev/null +++ b/day-16/custom_metrics_demo/cloudwatch_metrics.py @@ -0,0 +1,66 @@ +from flask import Flask +import time +import random +import boto3 + + +app = Flask(__name__) + +# Initialize AWS CloudWatch client +cloudwatch = boto3.client('cloudwatch', region_name='us-east-1') + +# Sample product data for our online store +products = { + '1': {'name': 'Product 1', 'price': 10.99}, + '2': {'name': 'Product 2', 'price': 19.99}, + '3': {'name': 'Product 3', 'price': 5.49} +} + +@app.route('/') +def index(): + start_time = time.time() + + # Simulate processing time + time.sleep(random.uniform(0.1, 0.5)) + + # Log the page view metric to CloudWatch + log_metric('PageViews', 1) + + # Log the response time metric to CloudWatch + response_time = (time.time() - start_time) * 1000 + log_metric('ResponseTime', response_time) + + return "Welcome to our Online Store!" + +@app.route('/product/') +def product(product_id): + start_time = time.time() + + # Simulate processing time + time.sleep(random.uniform(0.2, 0.8)) + + # Log the page view metric to CloudWatch + log_metric('PageViews', 1) + + # Log the response time metric to CloudWatch + response_time = (time.time() - start_time) * 1000 + log_metric('ResponseTime', response_time) + + if product_id in products: + return f"Product: {products[product_id]['name']}, Price: ${products[product_id]['price']}" + else: + return "Product not found." + +def log_metric(metric_name, value): + # Send custom metric to CloudWatch + cloudwatch.put_metric_data( + Namespace='OnlineStore', + MetricData=[{ + 'MetricName': metric_name, + 'Value': value, + 'Unit': 'Count' + }] + ) + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000) diff --git a/day-16/custom_metrics_demo/requirements.txt b/day-16/custom_metrics_demo/requirements.txt new file mode 100644 index 00000000..ddb60c6e --- /dev/null +++ b/day-16/custom_metrics_demo/requirements.txt @@ -0,0 +1,2 @@ +flask +boto3 \ No newline at end of file