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, 2020 in AWS by akhtar
• 38,260 points
10,524 views

3 answers 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, 2020 by MD
• 95,460 points
How to write this state in DynamoDB table.
If you are trying to use the state in your DynamoDB table then use the connector and store the output in your database.
Hi,

I tried to execute the function but it gives me timeout.

"errorMessage": "2020-11-04T06:54:27.496Z 42ae4cb3-a352-4f5a-aaaf-a2368a72a5fd Task timed out after 3.00 seconds"

Can anyone help...

Thanks.
Hi@tsm,

Check your network connectivity first. If it is fine, then check your security group and allow the traffic. It will work.
0 votes
import boto3
answered Sep 8, 2020 by abc
0 votes
import boto3
from pprint import pprint

def lambda_handler(event, context):
    
    # Obtain information about the instance
    instance = boto3.client("ec2")
    
    ec2status = instance.describe_instance_status(IncludeAllInstances = True)
    pprint(ec2status)
answered Sep 8, 2020 by Shrikant
• 180 points

Related Questions In AWS

0 votes
2 answers

how to import numpy and pandas inside aws lambda function?

this approach worked for me: https://medium.com/@melissa_89553/how-to- ...READ MORE

answered Oct 10, 2020 in AWS by anonymous
11,560 views
0 votes
2 answers
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
23,515 views
0 votes
1 answer

how to get the list of aws services i am used in aws my account by using the lambda function

Hi@shalk, You can create a session in your ...READ MORE

answered Sep 24, 2020 in AWS by MD
• 95,460 points
2,470 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to setup an alarm when Lambda function fails in AWS?

Hi@akhtar, To create an Alarm for Lambda function, ...READ MORE

answered Apr 2, 2020 in AWS by MD
• 95,460 points
4,191 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP