Important Interview Questions and Answers on AWS CloudWatch EC2
Q: What is AWS CloudWatch?
AWS CloudWatch is a monitoring and observability service that provides data and actionable insights to monitor applications, respond to system-wide performance changes, optimize resource utilization, and get a unified view of operational health.
Q: How does CloudWatch monitor AWS resources?
CloudWatch collects monitoring and operational data in the form of logs, metrics, and events. You can create CloudWatch alarms to trigger actions like scaling EC2 instances or sending notifications based on specified thresholds.
Q: What are CloudWatch metrics?
Metrics are data points collected at regular intervals to provide information about the performance of your AWS resources. Each AWS service sends metrics to CloudWatch. For example, EC2 sends metrics such as CPU utilization, disk I/O, and network traffic.
Q: How do you create a CloudWatch alarm for an EC2 instance's CPU utilization?
Here is the code.
import boto3
cloudwatch = boto3.client('cloudwatch')
response = cloudwatch.put_metric_alarm(
AlarmName='EC2_CPU_Utilization_Alarm',
AlarmDescription='Alarm when server CPU exceeds 70%',
ActionsEnabled=True,
AlarmActions=[
'arn:aws:sns:us-west-2:123456789012:my-sns-topic'
],
MetricName='CPUUtilization',
Namespace='AWS/EC2',
Statistic='Average',
Dimensions=[
{
'Name': 'InstanceId',
'Value': 'i-0123456789abcdef0'
},
],
Period=300,
EvaluationPeriods=1,
Threshold=70.0,
ComparisonOperator='GreaterThanThreshold'
)
print(response)
Q: How can you retrieve log data from CloudWatch Logs?
Here is the code.
import boto3
logs = boto3.client('logs')
response = logs.get_log_events(
logGroupName='/aws/lambda/my-function',
logStreamName='2020/10/19/[$LATEST]abcdef1234567890abcdef1234567890'
)
for event in response['events']:
print(event['message'])
Q: What is Amazon EC2?
Amazon Elastic Compute Cloud (EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers.
Q: How do you launch an EC2 instance using the AWS Management Console?
- Open the EC2 Dashboard.
- Click "Launch Instance."
- Choose an Amazon Machine Image (AMI).
- Choose an instance type.
- Configure instance details.
- Add storage.
- Add tags.
- Configure security group.
- Review and launch.
Q: How can you programmatically start and stop an EC2 instance using Boto3?
Here is the code.
import boto3
ec2 = boto3.client('ec2')
# Start instance
start_response = ec2.start_instances(
InstanceIds=['i-0123456789abcdef0'],
)
print("Starting instance: ", start_response)
# Stop instance
stop_response = ec2.stop_instances(
InstanceIds=['i-0123456789abcdef0'],
)
print("Stopping instance: ", stop_response)
Q: How do you create an AMI from an existing EC2 instance?
Here is the code.
import boto3
ec2 = boto3.client('ec2')
response = ec2.create_image(
InstanceId='i-0123456789abcdef0',
Name='My server backup',
NoReboot=True
)
print("AMI created: ", response['ImageId'])
Q: What are security groups and how do they work in EC2?
Security groups act as virtual firewalls for your instance to control inbound and outbound traffic. When you launch an instance, you can specify one or more security groups. Each instance in your VPC is associated with a security group, which allows you to specify rules to allow or deny traffic to/from the instance.
Q: Describe how you would set up monitoring for an EC2 instance and react to high CPU utilization.
- Set Up Monitoring: Use CloudWatch to collect CPU utilization metrics from the EC2 instance.
- Create an Alarm: Create a CloudWatch alarm that triggers when CPU utilization exceeds a threshold (e.g., 70%).
- Trigger Actions: Configure the alarm to send notifications via SNS or auto-scale by adding or terminating instances based on the alarm state.
Code Example:
- Set Up Monitoring and Alarm:
import boto3
cloudwatch = boto3.client('cloudwatch')
# Create CloudWatch alarm
alarm_response = cloudwatch.put_metric_alarm(
AlarmName='High_CPU_Utilization',
MetricName='CPUUtilization',
Namespace='AWS/EC2',
Statistic='Average',
Dimensions=[{'Name': 'InstanceId', 'Value': 'i-0123456789abcdef0'}],
Period=300,
EvaluationPeriods=1,
Threshold=70.0,
ComparisonOperator='GreaterThanThreshold',
AlarmActions=['arn:aws:sns:us-west-2:123456789012:my-sns-topic']
)
print("Alarm created: ", alarm_response)
- Auto-scaling Example (Assuming Auto Scaling Group is Set Up):
import boto3
autoscaling = boto3.client('autoscaling')
# Update Auto Scaling group to use the CloudWatch alarm
response = autoscaling.put_scaling_policy(
AutoScalingGroupName='my-auto-scaling-group',
PolicyName='ScaleUp',
AdjustmentType='ChangeInCapacity',
ScalingAdjustment=1,
Cooldown=300
)
print("Auto-scaling policy created: ", response)