Voiced by Amazon Polly
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.
2. Click on Add policy and select “Ec2ReadOnlyAccess” for instances metadata. Select another policy called “LambdaBasicExecutionRole” for executing the lambda function.
- Click on create to get your required role in the console.
Steps to Create a Lambda Function
- Go to the lambda Function Dashboard in AWS Console, there Click on Create Function.
- Select the “Author from Scratch” option, give a name to the function, and select a programming language for writing the lambda function.
- Select the IAM permission that we created previously, then click create.
4. After creating Lambda Function, we can write our function code and create a test event to run the function.
- Import boto3 and pprint library and assign boto3 client to EC2.
from pprint import pprint;
def lambda_handler(event, context):
client = boto3.client("ec2")
status = client.describe_instance_status(IncludeAllInstances =True)
for i in status["InstanceStatuses"]:
print("Availability Zone :",["AvailabilityZone"])
print("Instance Id:", i["InstanceId"])
print("Instance State : ",i["InstanceState"])
print("Instance Status: ", i["InstanceStatus"])
print("System status: ",i["SystemStatus"])
6. Let us check the instances that exist in our EC2 console.
- Run the test event, and we will get the details of resources available in the account. verify the details with existing resources.
When we run the test event, lambda code will be executed and fetched the meta-data as a response.
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.
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.
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.
1. Can only EC2 resources be tracked using lambda?
ANS: – No, we can also track other running resources of AWS, by just assigning the boto client to the required resource name
2. Are these EC2 resources from all the regions in the account?
ANS: – The list of resources given is just from a single AWS region.
3. Can we track another resource in the same function?
ANS: – Yes, writing a different resource tracking code in the same function can help to track multiple resources using the same function.
WRITTEN BY Sandeep Cheruku