How to check instance state and status via Lambda function in AWS?

0 votes

Hi Guys, 

I have my EC2 instances in AWS. I want to monitor status of that instances using AWS Lambda function.

How can I do that?

Thank You

Apr 2 in AWS by akhtar
• 10,540 points
289 views

1 answer to this question.

0 votes

Hi@akhtar,

To do this task, you have to use three services (EC2, IAM, Lambda) of AWS. 

  • Create EC2 instances.

  • Create IAM Role with AmazonEC2ReadOnlyAccess and AWSLambdaBasicExecutionRole access.

  • Write below given code in Lambda function and run.

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!')
    }

Thank You

answered Apr 2 by MD
• 22,970 points

Related Questions In AWS

0 votes
1 answer

how to import numpy and pandas inside aws lambda function?

The problem is that your local numpy ...READ MORE

answered Sep 14, 2018 in AWS by Priyaj
• 57,530 points
4,482 views
0 votes
2 answers

How to kill a lambda function in AWS?

$ aws lambda put-function-concurrency --function-name my-function --reserved-concurrent-executions ...READ MORE

answered Mar 28, 2019 in AWS by Shashank
• 1,370 points
6,843 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer