Automating Instance Management Using AWS Lambda and EventBridge

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 is 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 is 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.

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)) 
Enter fullscreen mode
Exit fullscreen mode
#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))
Enter fullscreen mode
Exit fullscreen mode

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.


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top