Skip to content

bhavyaidr/AWS-EC2-Scripts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Stop and Start Amazon EC2 instances at regular intervals using Lambda

Get the IDs of the EC2 instances that you want to stop and start, and then follow these instructions

Create an IAM policy and role

  1. Create an IAM policy using the JSON policy editor. Paste this JSON policy document into the policy editor:

image

  1. Create an IAM role for Lambda. When attaching a permissions policy, search for and choose the IAM policy that you created.

image

Create Lambda functions that stop and start your EC2 instances

  1. In the Lambda console, choose Create function.

  2. Choose Author from scratch.

  3. Under Basic information, add the following:

  • For Function name, enter a name that identifies it as the function used to stop your EC2 instances. For example, "StopEC2Instances".
  • For Runtime, choose Python 3.7.
  • Under Permissions, expand Choose or create an execution role.
  • Under Execution role, choose Use an existing role.
  • Under Existing role, choose the IAM role that you created.
  1. Choose Create function.

  2. Copy this code, and then under Function code, paste it into the editor pane in the code editor (lambda_function). This code stops the EC2 instances that you identify.

  3. Under Basic settings, set Timeout to 10 seconds.

Note: Configure the Lambda function settings as needed for your use case. For example, if you want to stop and start multiple instances, you might need a different value for Timeout, as well as Memory.

  1. Choose Save.

  2. Repeat steps 1-7 to create another function. Do the following differently so that this function starts your EC2 instances:

  • In step 3, enter a Function name it as the function used to start your EC2 instances. For example, "StartEC2Instances".
  • In step 5, copy and paste this code into the editor pane in the code editor (lambda_function):

Note: For region and instances, use the same values that you used for the code to stop your EC2 instances.

image

Test your Lambda functions

  1. In the Lambda console, choose Functions.

  2. Select one of the functions that you created.

  3. Choose Actions, and then choose Test.

  4. In the Configure test event dialog, choose Create new test event.

  5. Enter an Event name, and then choose Create.

Note: You don't need to change the JSON code for the test event—the function doesn't use it.

  1. Choose Test to execute the function.

  2. Repeat steps 1-6 for the other function that you created.

Tip: You can check the status of your EC2 instances before and after testing to confirm that your functions work as expected.

Create rules that trigger your Lambda functions

  1. Open the CloudWatch console

  2. In the left navigation pane, under Events, choose Rules.

  3. Choose Create rule.

  4. Under Event Source, choose Schedule.

  5. Do either of the following:

For Fixed rate of, enter an interval of time in minutes, hours, or days. For Cron expression, enter an expression that tells Lambda when to stop your instances. For information on the syntax of expressions, see Schedule Expressions for Rules

Note: Cron expressions are evaluated in UTC. Be sure to adjust the expression for your preferred time zone.

  1. Under Targets, choose Add target.

  2. Choose Lambda function.

  3. For Function, choose the function that stops your EC2 instances.

  4. Choose Configure details.

  5. Under Rule definition, do the following:

For Name, enter a name to identify the rule, such as "StopEC2Instances". (Optional) For Description, describe your rule. For example, "Stops EC2 instances every night at 10 PM." For State, select the Enabled check box.

  1. Choose Create rule.

  2. Repeat steps 1-11 to create a rule to start your EC2 instances. Do the following differently: In step 5, for Cron expression, enter an expression that tells Lambda when to start your instances. In step 8, for Function, choose the function that starts your EC2 instances. In step 10, under Rule definition, enter a Name like "StartEC2Instances", and optionally enter a Description like "Starts EC2 instances every morning at 6 AM."

image

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published