0% found this document useful (0 votes)
16 views10 pages

Task 6 Stop An Ec2 Instance Using Aws Lambda Function Tags

The document outlines a step-by-step guide to create an AWS Lambda function that automatically stops EC2 instances based on specific tags. It includes instructions for setting up the Lambda function, attaching necessary IAM permissions, adding code, tagging EC2 instances, creating an EventBridge rule to trigger the function, and testing the setup. This guide is intended for users familiar with AWS services and Python programming.

Uploaded by

sanjudote4545
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Task 6 Stop An Ec2 Instance Using Aws Lambda Function Tags

The document outlines a step-by-step guide to create an AWS Lambda function that automatically stops EC2 instances based on specific tags. It includes instructions for setting up the Lambda function, attaching necessary IAM permissions, adding code, tagging EC2 instances, creating an EventBridge rule to trigger the function, and testing the setup. This guide is intended for users familiar with AWS services and Python programming.

Uploaded by

sanjudote4545
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

AWS Cloud and Devops

by Mr. Mahendran Selvakumar

Generate code to automatically stop an EC2 instance


using a Lambda function with tags and Amazon Q
Developer.

Name: YUVADHARANIE M Class: CSE-C(III year)

Organized by KPR Institute of Engineering and Technology


Department of Computer Science and Engineering
STEP-1 Set Up AWS Lambda
1. Log in to AWS Console and go to Lambda.
2. Click Create Function.
3. Select Author from Scratch.
4. Enter a function name, e.g., StopEC2Instances.
5. Choose Python 3.9 or later as the runtime.
6. Select Create a new role with basic Lambda permissions.
7. Click Create function.
STEP-2 Attach IAM Permissions to Lambda

1. Open the IAM Console


2. Click on your role name:StopEC2Instances-role-ixm8w2l5
3. Go to the Permissions tab.
4. Click Add permissions → Attach policies.
5. In the search bar, type: AmazonEC2FullAccess.
6. Check the box next to AmazonEC2FullAccess.
7. Click Add permissions.
STEP-3 Add Code to Lambda

1. In the Code section of your Lambda function, delete the sample code.
2. Paste the following Python code:
3. Click Deploy.

STEP-4 Tag EC2 Instances

1. Go to EC2 → Instances.
2. Select the instance you want to stop automatically.
3. Click Tags → Manage Tags → Add Tag.
4. Key: AutoStop
5. Value: True
6. Click Save.

STEP-5 (Create an EventBridge Rule to Trigger Lambda)


1. Go to Amazon EventBridge → Rules.
2. Click Create Rule.
3. Enter a name (e.g., StopEC2Scheduler).
4. Select Schedule and choose Cron expression.
5. Example: 0 18 * * ? * (Stops instances at 6 PM UTC daily).
6. Click Next and select Lambda Function as the target.
7. Choose StopEC2Instances.
8. Click Create Rule.
STEP-6 (Test the Lambda Function)

1. Click Test in the Lambda function.


2. Choose Create a new test event.
3. Enter a name (e.g., TestStopEC2).
4. Click Save → Test.
5. If an EC2 instance with the tag exists and is running, it should stop.

You might also like