Tuesday 30 May 2023

Introduction to Amazon CloudWatch Synthetics

 In this article, we will get an introduction to Amazon Cloudwatch Synthetics. With this feature, you can create different kinds of Canaries to continually verify your user experience even when you don’t have traffic, monitor and test for unusual behavior, and trace issues to their source for faster resolution.

Let’s start by navigating to CloudWatch synthetics.

Amazon CloudWatch synthetics allows you to create configurable scripts called canaries to continuously monitor your application endpoints and APIs. By using canaries you can discover issues with your website before your customers do. 

Here we already have two canaries monitoring our sample pet adoption site. 

Let’s create a third Canary. The canaries can be created from pre-existing blueprints from scripts that can be edited inline or by using a workflow builder or a canary recorder. 

For this canary, we’ll use the Heartbeat monitoring blueprint. The heartbeat scripts load the specified URL and store a screenshot of the page, an HTTP archive file, and logs of accessed URLs. We’ll enter the URL for our sample pet adoption site in this case.

Let’s move on without inputting any environment variables.

We’ll also retain the default settings for scheduling.

We’ll also keep the default timeout settings. If you have specific data retention policies you can change the default settings here to maintain compliance.

Under access permissions, you can choose whether to create a new identity and access management or IAM role that’s specific to this canary or use an existing role. We’ll create a new role.

You can allow synthetics to create CloudWatch alarms for your canary automatically. Let’s add an alarm that will notify us via an Amazon SNS, for if our canary fails two or more times in 15 minutes.

If your endpoint is under a virtual private cloud you can select it here.

CloudWatch synthetics can be integrated with AWS X-ray and CloudWatch service lens service maps. Let’s enable the integration and create the canary.

Our heartbeat monitoring canary has been successfully created. 

Canaries with tracing enabled also appear on the service map in both CloudWatch service lens and an X-ray. Returning to the top of the page we can select the monitoring tab to view graphs of the CloudWatch metrics published by this canary for the selected time range. The configuration tab details current configuration information for the canary including when it was last modified. 

Features:

  1. Canary Tests: CloudWatch Synthetics allows you to create canary tests that simulate user behavior and test the availability and performance of your applications, APIs, and websites. You can create canary tests using pre-built blueprints or by writing your own custom scripts.
  2. Monitoring: CloudWatch Synthetics monitors your canary tests in real-time and provides detailed metrics and logs for each test. It also alerts you when a test fails or experiences performance issues.
  3. Scripting: CloudWatch Synthetics provides a powerful scripting environment that allows you to create custom scripts to test your applications, APIs, and websites. You can use scripting languages such as JavaScript, Python, and PowerShell to write your scripts.
  4. Integrated with CloudWatch: CloudWatch Synthetics is fully integrated with Amazon CloudWatch, which means that you can use CloudWatch to view and analyze your canary test results alongside other metrics and logs.
  5. Simple Pricing: CloudWatch Synthetics is offered at a simple, pay-as-you-go pricing model. You only pay for what you use, and there are no upfront costs or minimum fees.

Introduction to Amazon Cloudwatch

 Amazon CloudWatch is a service used for monitoring and observing resources in real-time, built for DevOps engineers, developers, site reliability engineers (SREs), and IT managers. CloudWatch provides users with data and actionable insights to monitor their respective applications, stimulate system-wide performance changes, and optimize resource utilization. CloudWatch collects monitoring and operational data in the form of logs, metrics, and events, providing its users with an aggregated view of AWS resources, applications, and services that run on AWS. The CloudWatch can also be used to detect anomalous behavior in the environments, set warnings and alarms, visualize logs and metrics side by side, take automated actions and troubleshoot issues.

Terminologies related to Amazon Cloudwatch

Metrics

  • It represents a time-ordered set of data points that are published to Amazon CloudWatch
  • All data point is marked with a timestamp
  • Metric is a variable that is monitored and data points are the value of that variable over time
  • They are uniquely defined by a name, namespace, and zero or more dimensions
  • Metric math is used to query multiple cloudwatch metrics and use math expressions to create new time-series based on these metrics

Dimensions

  • A dimension is a name/value pair which uniquely identifies a metric
  • Dimensions are the unique identifiers for a metric, so whenever you add a unique name/value pair to one of the metrics, you are creating a new variation of that metric.

Statistics

  • Statistics are metric data aggregations over specified periods of time
  • The few available statistics on Cloudwatch are maximum, minimum, sum, average, and sample count.

Alarm

  • It is used  to automatically initiate actions on our behalf
  • It watches a single metric over a specified time period and performs one or more specified actions based on the value of the metric
  • The estimated AWS charges can also be monitored using the alarm

Percentiles

  • It represents the relative weightage of the data in a dataset
  • It helps the user to get a better understanding of the distribution of metric data

Cloudwatch dashboard

  • A user-friendly Cloudwatch console is available which is used for monitoring resources in a single view.
  • There is no limit on the number of cloudwatch dashboards you can create.
  • These dashboards are global and not region-specific

Cloudwatch agent

  • It is required to be installed
  • It collects logs and system-level metrics from EC2 instances and on-premises servers

Cloudwatch Events:

  • Cloudwatch events help you to create a set of rules that match with any event(i.e stopping of EC2 instance).
  • These events can be routed to one or more targets like AWS Lambda functions, Amazon SNS Topics, Amazon SQS queues, and other target types.
  • Cloudwatch Events observes the operational events continuously and whenever there is any change in the state of the event, it performs the action by sending notifications, activating lambda, etc.
  • An event indicates a change in the AWS environment. Whenever there is a change in the state of AWS resources, events are generated.
  • Rules are used for matching events and routing to targets.
  • Target process events. They include Amazon EC2 instances, AWS Lambda functions, etc. A target receives the events in JSON format.

Cloudwatch logs:

  • Amazon Cloudwatch logs enable you to store, monitor, and access files from AWS resources like Amazon EC2 instances, Route53, etc.
  • It also helps you to troubleshoot your system errors and maintain the logs in highly durable storage.
  • It also creates log of information about the DNS queries that Route 53 receives

Amazon Cloudwatch Create

Notifying gfg website management team when the instance on which gfg website is hosted stops Whenever the CPU utilization of instance (on which GeeksForGeeks website is hosted ) goes above 80%, cloudwatch event is triggered. This cloudwatch event then activates the SNS topic which sends the alert email to the attached gfg subscribers.

Step 1: Let us assume that you have already launched an instance with the name tag ‘instance’. 

Step 2: Go to SNS topic dashboard and click on create a topic

Step 3: You will be directed to this dashboard. Now specify the name and display name.

Step 4: Scroll down and click on create the topic.

Step 5: The SNS topic is created successfully. 

Step 6: Go to the SNS topic dashboard and click on gfgtopic link.

Step 7: Under the subscriptions section, Click on Create subscription.

Step 8: Select Email as protocol and specify the email address of subscribers in Endpoint. Click on create the subscription. Now Go to the mailbox of the specified email id and click on Subscription confirmed.

Step 9: Go to the cloudwatch dashboard on the AWS management console. Click on Metrics in the left pane.

Step 10: In All metrics section click on EC2 

Step 11: Click on Per-instance metrics

Step 12: Select the instance you launched

Step 13: Go to Graphed metrics, click on the bell icon

Step 14: This dashboard shows the components of Amazon Cloudwatch such as Namespace, Metric Name, Statistics, etc

Step 15: Select the greater threshold. Also, specify the amount( i.e 80 ) of the threshold value. Click on Next.

Step 16: Click on Select an existing SNS topic, also mention the name of the SNS topic you created now.

Step 17: Specify the name of alarm and description which is completely optional. Click on Next and then click on Create alarm.

Step 18: The alarm is successfully created.

Step 19: You can see the graph which notifies whenever CPU utilization goes above 80%.

 Use cases for CloudWatch

  • CloudWatch can be used to monitor the performance of AWS resources, applications, and infrastructure components in real-time
  • CloudWatch allows users to set up alarms that trigger notifications or automated actions in response to changes in the state of their resources.
  • CloudWatch can be used to store, search, and analyze log data from various AWS services, applications, and infrastructure components.
  • CloudWatch can be used to monitor the performance of EC2 instances, RDS databases, and other resources, which can then be used to trigger automatic scaling events.

Advantages of Amazon Cloudwatch

  • A large amount of data is produced by web applications nowadays so amazon cloudwatch acts as a dashboard that contains the organized collection of whole data.
  • It improves the total cost of ownership by providing alarms and also takes automated actions when there is an error in limits provided.
  • Applications and resources can be optimized by examining the logs and metric data.
  • Detailed Insights from the application are provided through data like CPU utilization, capacity utilization, memory utilization, etc
  • It provides a great platform to compare and contrast the data produced by various AWS services.

Disadvantages of Amazon Cloudwatch

  • Cloud Watch can be expensive, especially for large-scale monitoring and logging needs.
  • Cloud Watch may not be able to handle large amounts of log data, especially during spikes in usage, making it difficult to maintain a consistent level of monitoring and logging.
  • The monitoring and logging processes of CloudWatch can consume significant system resources, impacting the overall performance of an application.
  • Integrating CloudWatch with other AWS services and third-party tools can be challenging.
  • Setting up and managing CloudWatch can be complex, especially for users who are not familiar with cloud-based systems.

Insert Data Using AWS Lambda

 In this article, we will look into the process of inserting data into a DynamoDB table using AWS Lambda. Amazon DynamoDB is a completely owned NoSQL proprietary provider that helps key-value and textual statistics systems and is supplied via way of means of Amazon.com as a part of Amazon Web Services. AWS Lambda is an event-driven, serverless computing platform supplied via way means of Amazon as part of Amazon Web Services. It is a computing service that runs code without us worrying about servers. Users can simply use the AWS Lambda function so as to process the records in an Amazon DynamoDB stream. With the help of DynamoDB Streams, Users can easily trigger the Lambda function so as to perform additional work every time a DynamoDB table will be updated. Some permissions are also needed  to manage the resources related to your DynamoDB stream like dynamodb:GetRecords, dynamodb:GetShardIterator, dynamodb:ListStreams and dynamodb:DescribeStream. User have to Add this into their function’s execution role. Users can create an event for source mapping to tell that their  Lambda sends the records from your stream to a Lambda function. By default, the Lambda can invokes your function as soon as the records are available. The Lambda polls also shards in your DynamoDB stream service for the records having base rate of 4 times per second.

Implementation:

Follow the below steps to insert data into the DynamoDB table using AWS lambda:

Step 1: Login into AWS console.

Step 2: Search for dynamodb.

 

Step 3: Select Dynamodb and press on create table

 

Step 4: Now give the table name and keys accordingly to your requirement 

 

Now table will be created.

Step 5: Now we need to create Identity and Access Management(IAM) role for that go and search for IAM role.

Step 6: Click on role in access management and click on create role.

 

 

Step 7: Here we need to select AWS service and lambda.

 

Step 8: Here we need to add permission, as we are using dynamo db we need to add AmazonDynamoDBFullAccess Permissions policies

 

Step 9: Now give the role name and select create role

 

Step 10: Press on create function.

 

Step 11: Give name and Runtime.

 

Step 12: Change the Execution role to Use an existing role and select your role.

 

Step 13: Now go to the code section and add the below code.

#importing packages
import json
import boto3
#function definition
def lambda_handler(event,context):
    dynamodb = boto3.resource('dynamodb')
    #table name
    table = dynamodb.Table('sample')
    #inserting values into table
    response = table.put_item(
       Item={
            'sample': 'bhagi',
             
        }
    )
    return response

Output: