Managing Running Instances Made Easier with AWS Lambda

November 17, 2022 | Comments(0) |

TABLE OF CONTENT

1. Introduction
2. Permissions Required to Perform the Task
3. Steps to Create a Lambda Function
4. Pricing
5. Conclusion
6. About CloudThat
7. FAQs

 

Introduction

EC2 instances are the major AWS resources, in which applications’ data can be stored, run, and deployed. What if we want to send our running AWS Instances (servers) information to our team in form of logs for any purposes? Also, if you want to track unwanted launched instances in your account. We can do it through a single click by creating a function in AWS lambda. In this post, I will cover how to call instances of meta-data using Lambda.

AWS Lambda: AWS Lambda is a serverless computing technology that allows you to run code without provisioning or managing servers Lambda executes code only when needed and scales automatically. Without any additional configuration, AWS Lambda scales the infrastructure without difficulty. The amount of operational work is decreased. It provides fault tolerance for both the code- and function-running services. You need not be concerned about the application being unavailable.

Primary use cases for Lambda:

  • Data processing
  • Real-time file processing
  • Real-time stream processing

AWS EC2: Elastic Compute Cloud (EC2) Amazon Elastic Compute Cloud provides virtual machines instantly. It is a Platform as a Service-oriented resource. EC2 is a customizable cloud server. On Amazon’s EC2, an instance is a virtual server used to run applications. It can also be viewed as a tiny component of a bigger computer, a tiny component with its own Hard drive, network connection, operating system, etc. But everything is actually virtual. On a single physical system, you can have several “small” computers, and all of these little computers are referred to as Instances.

Primary use cases for EC2:

  • Hosting applications
  • Storing a Backup data
  • Development and test environments

Permissions Required to Perform the Task

 The Lambda function needs IAM permission to access the EC2 instances.

Creating an IAM Role with required policies.

For creating an IAM role go to the IAM dashboard in AWS Console. There you can see the roles tab, click on the “Create role” option. Select the use case as Lambda.

IAM1

2. Click on Add policy and select “Ec2ReadOnlyAccess” for instances metadata. Select another policy called “LambdaBasicExecutionRole” for executing the lambda function.

IAM2

3. Click on create to get your required role in the console.

IAM3

Steps to Create a Lambda Function

  1. Go to the lambda Function Dashboard in AWS Console, there Click on Create Function.
  2. Select the “Author from Scratch” option, give a name to the function, and select a programming language for writing the lambda function.

lambda1

  1. Select the IAM permission that we created previously, then click create.

lambda24. After creating Lambda Function, we can write our function code and create a test event to run the function.

lambda3

  1. Import boto3 and pprint library and assign boto3 client to EC2.

lambda4

Code:

 

6. Let us check the instances that exist in our EC2 console.

lambda5

  1. Run the test event, and we will get the details of resources available in the account. verify the details with existing resources.

lambda6

When we run the test event, lambda code will be executed and fetched the meta-data as a response.

Pricing

Pricing of AWS Lambda is calculated as pay per calls:

  • First one million requests are free.
  • $0.20 per 1 million requests thereafter ($0.0000002 per request)

 It is usually cheap to run AWS Lambda. So, you can use it liberally.

Conclusion

AWS lambda can execute functions and fetch details fast. That is because of the hidden server configuration used by AWS to run lambda. Managing regular cloud instances is made easier with lambda. We can also store the output of lambda as a log into an S3 bucket.

About CloudThat

CloudThat is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best-in-industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding Lambda Functions and I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.

FAQs

  1. Can only EC2 resources be tracked using lambda?

A. No, we can also track other running resources of AWS, by just assigning the boto client to the required resource name.

  1. Are these EC2 resources from all the regions in the account?

A. The list of resources given is just from a single AWS region.

  1. Can we track another resource in the same function?

A. Yes, writing a different resource tracking code in the same function can help to track multiple resources using the same function.


Leave a Reply