Monitoring EC2 instance status using AWS Lambda and Boto Library

Monitoring EC2 instance status using AWS Lambda and Boto Library

Monitoring EC2 instance status using AWS Lambda and Boto Library

As more businesses and organizations turn to cloud computing for their infrastructure needs, it becomes essential to keep track of the status of their resources in real time. Amazon Web Services (AWS) provides a powerful suite of tools to manage resources, including EC2 instances. However, keeping an eye on the status of your EC2 instances can be a time-consuming task.

Fortunately, AWS Lambda offers an efficient solution to automate this process. In this blog, we will explore how to monitor EC2 instance status using AWS Lambda and help you keep track of your resources with minimal effort. So, whether you’re a developer, an IT professional, or a business owner, this blog is for you!

Step 1: We must first create a role and grant the required permission:

Click on create new role under AWS IAM Roles. Keep the other setting set to its default value. Choose the Lambda use case, after which select next.

Step 2: Add the following 2 permission policies: AWSLambdaBasicExecutionRole and AmazonEC2FullAccess. Choose next. Give your role a suitable name and click the “create role” button on the following screen.

We have now successfully created a position and allocated the necessary permissions.

Step 3: We must now create a lambda function. Click “Create function” on the AWS Lambda page.

Complete the basic details

Step 4: Click Change default execution role, which is located beneath the permissions section.

The role that we previously created can be chosen by selecting Use an Existing role. (If you’d like, you may also create a new role similar to the one we just made.) then select the create function button.

Now, our function is created. The above code will return the basic details and status of AWS EC2 instances.

When you Test your function, you will be able to see all instances with the status of the instance.

import json

import boto3

from pprint import pprint

def lambda_handler(event, context):

# TODO implement

client = boto3.client("ec2")

status = client.describe_instance_status(IncludeAllInstances = True)

#pprint(status)

for i in status["InstanceStatuses"]:

print("AvailabilityZone :", i["AvailabilityZone"])

print("InstanceId :", i["InstanceId"])

print("InstanceState :", i["InstanceState"])

print("InstanceStatus", i["InstanceStatus"])

return {

'statusCode': 200,

'body': json.dumps('Hello from Lambda!')

}

Reference:

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html

EC2 - Boto3 Docs 1.26.74 documentation

A low-level client representing Amazon Elastic Compute Cloud (EC2) Amazon Elastic Compute Cloud (Amazon EC2) provides…

boto3.amazonaws.com

I hope this article was informative and provided you with the details you required. If you have any questions related while reading the blog, message me on Instagram or LinkedIn.For any kind of work related to DevOps, Site Reliability you can contact me at helpmeanubhav@gmail.com

Special credits to my team members: Gaurav Kachariya and Krisha Amlani.

Thank You…

As more businesses and organizations turn to cloud computing for their infrastructure needs, it becomes essential to keep track of the status of their resources in real time. Amazon Web Services (AWS) provides a powerful suite of tools to manage resources, including EC2 instances. However, keeping an eye on the status of your EC2 instances can be a time-consuming task.

Fortunately, AWS Lambda offers an efficient solution to automate this process. In this blog, we will explore how to monitor EC2 instance status using AWS Lambda and help you keep track of your resources with minimal effort. So, whether you’re a developer, an IT professional, or a business owner, this blog is for you!

Step 1: We must first create a role and grant the required permission:

Click on create new role under AWS IAM Roles. Keep the other setting set to its default value. Choose the Lambda use case, after which select next.

Step 2: Add the following 2 permission policies: AWSLambdaBasicExecutionRole and AmazonEC2FullAccess. Choose next. Give your role a suitable name and click the “create role” button on the following screen.

We have now successfully created a position and allocated the necessary permissions.

Step 3: We must now create a lambda function. Click “Create function” on the AWS Lambda page.

Complete the basic details

Step 4: Click Change default execution role, which is located beneath the permissions section.

The role that we previously created can be chosen by selecting Use an Existing role. (If you’d like, you may also create a new role similar to the one we just made.) then select the create function button.

Now, our function is created. The above code will return the basic details and status of AWS EC2 instances.

When you Test your function, you will be able to see all instances with the status of the instance.

import json

import boto3

from pprint import pprint

def lambda_handler(event, context):

# TODO implement

client = boto3.client("ec2")

status = client.describe_instance_status(IncludeAllInstances = True)

#pprint(status)

for i in status["InstanceStatuses"]:

print("AvailabilityZone :", i["AvailabilityZone"])

print("InstanceId :", i["InstanceId"])

print("InstanceState :", i["InstanceState"])

print("InstanceStatus", i["InstanceStatus"])

return {

'statusCode': 200,

'body': json.dumps('Hello from Lambda!')

}

Reference:

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html

EC2 - Boto3 Docs 1.26.74 documentation

A low-level client representing Amazon Elastic Compute Cloud (EC2) Amazon Elastic Compute Cloud (Amazon EC2) provides…

boto3.amazonaws.com

I hope this article was informative and provided you with the details you required. If you have any questions related while reading the blog, message me on Instagram or LinkedIn.For any kind of work related to DevOps, Site Reliability you can contact me at helpmeanubhav@gmail.com

Special credits to my team members: Gaurav Kachariya and Krisha Amlani.

Thank You…

As more businesses and organizations turn to cloud computing for their infrastructure needs, it becomes essential to keep track of the status of their resources in real time. Amazon Web Services (AWS) provides a powerful suite of tools to manage resources, including EC2 instances. However, keeping an eye on the status of your EC2 instances can be a time-consuming task.

Fortunately, AWS Lambda offers an efficient solution to automate this process. In this blog, we will explore how to monitor EC2 instance status using AWS Lambda and help you keep track of your resources with minimal effort. So, whether you’re a developer, an IT professional, or a business owner, this blog is for you!

Step 1: We must first create a role and grant the required permission:

Click on create new role under AWS IAM Roles. Keep the other setting set to its default value. Choose the Lambda use case, after which select next.

Step 2: Add the following 2 permission policies: AWSLambdaBasicExecutionRole and AmazonEC2FullAccess. Choose next. Give your role a suitable name and click the “create role” button on the following screen.

We have now successfully created a position and allocated the necessary permissions.

Step 3: We must now create a lambda function. Click “Create function” on the AWS Lambda page.

Complete the basic details

Step 4: Click Change default execution role, which is located beneath the permissions section.

The role that we previously created can be chosen by selecting Use an Existing role. (If you’d like, you may also create a new role similar to the one we just made.) then select the create function button.

Now, our function is created. The above code will return the basic details and status of AWS EC2 instances.

When you Test your function, you will be able to see all instances with the status of the instance.

import json

import boto3

from pprint import pprint

def lambda_handler(event, context):

# TODO implement

client = boto3.client("ec2")

status = client.describe_instance_status(IncludeAllInstances = True)

#pprint(status)

for i in status["InstanceStatuses"]:

print("AvailabilityZone :", i["AvailabilityZone"])

print("InstanceId :", i["InstanceId"])

print("InstanceState :", i["InstanceState"])

print("InstanceStatus", i["InstanceStatus"])

return {

'statusCode': 200,

'body': json.dumps('Hello from Lambda!')

}

Reference:

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html

EC2 - Boto3 Docs 1.26.74 documentation

A low-level client representing Amazon Elastic Compute Cloud (EC2) Amazon Elastic Compute Cloud (Amazon EC2) provides…

boto3.amazonaws.com

I hope this article was informative and provided you with the details you required. If you have any questions related while reading the blog, message me on Instagram or LinkedIn.For any kind of work related to DevOps, Site Reliability you can contact me at helpmeanubhav@gmail.com

Special credits to my team members: Gaurav Kachariya and Krisha Amlani.

Thank You…