Automating Instance Management Using AWS Lambda and EventBridge
Published: 2024-07-08 05:38:24

## Introduction:

Automating instance management with AWS Lambda and EventBridge involves using EventBridge to trigger Lambda functions based on specific events or schedules. This setup can automatically start, stop, or manage EC2 instances, optimizing resource usage and costs by responding to predefined conditions or times.

## What are AWS Lambda?

AWS Lambda is a serverless computing service that lets you run code without provisioning or managing servers. You write your code and upload it to Lambda, and it automatically scales and executes in response to events, charging only for compute time consumed. It supports various programming languages.

## What are Amazon EventBridge?

Amazon EventBridge is a serverless event bus service that makes it easy to connect applications using data from your applications, integrated SaaS applications, and AWS services. It allows you to build event-driven architectures with real-time data processing, routing events to targets like Lambda functions, and automating workflows.

## Architecture:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vthes11kffrah4pt0s2e.jpeg)

## Let’s get down to business!

The next thing is going through the steps from scratch in order to implement our solution. You need not worry about the Python3 code as you only need to copy-paste it.

## STEP 1:
Create one Windows/Linux instance.

## STEP 2:
 Open the IAM service and create a role to access lambda services.

• Select AWS services 
• Choose lambda use case. 
• Click on permission and use two policies: AWSLambdaBasicExecutionRole (allow to use cloudwatch) and AmazonEC2Full Access. Note: You can create your policies, or you can select existing policies 
• Provide a tag. 
• Provide Role name and role description.

## STEP 3:
 Open Lambda services and create a new lambda function. 

• Provide function name. 
• Use Python as a runtime environment. 
• Go to permissions and use the created role in Task 2. 
• Now basic function code window will appear, and we need to write code for starting and stopping EC2 instances. 
 

## Code:
```
#Start instance: 
import boto3 
region = 'us-east-2' 
instances = ['i-0f019ff0664238dcb'] 
ec2 = boto3.client('ec2', region_name=region) 

def lambda_handler(event, context): 
ec2.start_instances(InstanceIds=instances) 
print('starting your instances: ' + str(instances)) 
```
```
#Stop Instance: 
import boto3 
region = 'us-east-2' 
instances = ['i-0f019ff0664238dcb'] 
ec2 = boto3.client('ec2', region_name=region) 

def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances) 
print('stopped your instances: ' + str(instances))
```

## STEP 4:
 Save the code. Select a test event and configure the test event.

## STEP 5:
 Test the EC2 events by clicking on the startEC2 event and stopEC2 event.

## STEP 6:
 Provide the execution details of the lambda function.
 
## STEP 7:
 Select the event bridge and click on create a rule to trigger the created lambda function. 

• Provide a name and description for the rule. 
• Define pattern as an event by providing Cron expression. 
• Select targets as Lambda function, provide the name of the function, and copy the JSON text from the event window. 
• Create the rule now.

## Conclusion

Congratulations! You've successfully Automated a Instance Using AWS Lambda and EventBridge.Automating instance management with AWS Lambda and EventBridge enhances efficiency, reduces costs, and optimizes resource allocation by automating responses to events. This streamlines operations and ensures instances are managed according to specific conditions or schedules.