AWS provides powerful tools to automate tasks, such as starting and stopping EC2 instances. This can help you save costs by only running instances when they are needed. In this blog, we will walk through the process of setting up an automated system to start and stop EC2 instances using AWS services like IAM, Lambda and EventBridge.

Starting and stopping an EC2 (Elastic Compute Cloud) instance in AWS refers to the process of turning the virtual server on and off, respectively. When an EC2 instance is started, it becomes accessible over the internet and any applications or services running on it can be used. Conversely, when an EC2 instance is stopped, it is turned off and any applications or services running on it are not accessible. Starting and stopping EC2 instances can be useful for various reasons, such as saving costs by only running instances when they are needed, managing resources more efficiently and performing maintenance tasks. By automating the start and stop process, you can ensure that your instances are only running during specific times, such as during business hours, to minimize costs.

Step 1: Create IAM Policies

Click on Create Policies.

Next, click on "Services" and select "EC2" under the Compute section.

select the "Write" under the access level. Check the boxes for "StartInstance" and "StopInstance" actions. Click "Next" to proceed with the next steps in the process. This configuration will enable the specified actions for your AWS resources, allowing your business to manage EC2 instances efficiently.

Name your policy and click on "Create Policy" to finalize the setup.

Step 2: Create IAM Role

Next, create an IAM role that allows Lambda to execute actions on EC2 instances using the policies created in the previous step.

Go to the IAM console and click on "Roles" in the left sidebar.

Click on "Create role."

Now, select the trusted entity type "AWS Service" as the service you want to control access to. For this scenario, choose "Lambda" as the specific service or use case. This selection will allow you to manage permissions specifically for AWS Lambda functions, ensuring that your team can interact with Lambda functions securely and effectively.

In the next step, select the "stopec2" Policy that we just created to attach to the new role. This selection will give the role the necessary permissions to start and stop EC2 instances. Click "Next" to proceed with the next steps in setting up the role.

Finally, give the new role a descriptive name and a description to clarify its purpose. Click on "Create role" to complete the process. This role will now have the necessary permissions to manage EC2 instances through AWS Lambda functions, helping to streamline your AWS operations and improve efficiency.

Step 3: Create Lambda Function

Now, let's create a Lambda function that will start or stop the EC2 instances based on a trigger.

Go to the Lambda console and click on "Create function."

Choose "Author from scratch" to create the function from the ground up. Enter a descriptive name that clearly reflects the function's purpose. Then, select the programming language (Node.js, Python, or Ruby) that best suits your requirements for writing the function. Next, choose the appropriate instruction set architecture to ensure compatibility and optimize performance for your specific use case. This setup will lay the foundation for developing a powerful and efficient function tailored to your business needs.

In the next step, select the existing role that we created earlier to be used by the Lambda function. This role will provide the necessary permissions for the function to interact with other AWS services, ensuring smooth and secure operation within your AWS environment. after that just click on “Create Function” to complete the process.

The Lambda function named “stopec2” has been successfully created.

Now, we need to write the code for the Lambda function to stop the EC2 instance. This code will define the actions the function will take when triggered, ensuring that the EC2 instance is stopped efficiently and according to your business requirements

After coding the Lambda function, when you click on "Test," a popup will appear to configure the test event.

What is a Test Event?

A Test Event is a JSON object that mimics the format of requests made by AWS services to invoke a Lambda Function. It helps simulate real-world scenarios, allowing you to test the behavior of your Lambda Function in a controlled environment before deploying it in production.

Here, you will create a new event with a specific event name and save the details. This event configuration allows you to simulate the triggering of the Lambda function, helping you ensure that it behaves as expected in different scenarios within your business environment.

Step 4: Create Rule in EventBridge

EventBridge allows us to trigger Lambda functions based on scheduled events. We will create a rule to trigger our Lambda function at specific times to start or stop the EC2 instances.

Go to the EventBridge console and click on "Create rule."

To define the rule details, including the name and description, specify the rule type as "schedule" in this case. This means the rule will be triggered based on a schedule you set, rather than an event pattern. This setup allows you to automate the execution of your Lambda function at specific times, enhancing the efficiency of your AWS environment.

The next step is to specify the schedule details, including the schedule name, which in this case is "stopec2," along with its description. This schedule will define when the Lambda function should be triggered to stop the EC2 instance, enabling you to automate this process according to your business requirements.

Next, define the occurrence of the schedule, selecting a recurring schedule. This means the schedule will repeat at regular intervals according to the configuration you set.

Choose your timezone to ensure the schedule aligns with your business operations. Select the schedule type as "cron based schedule" and set the cron expression according to UTC time.

This setup will automate the execution of your Lambda function, ensuring that the EC2 instance is stopped at the specified intervals, providing you with greater control and efficiency in your AWS environment.

Review your schedule details and click on “Create Schedule” to complete the process.

the Schedule named “stopec2” has been created succesfully.

If you navigate back to the EC2 console and check your instance, you will see that it is now in a stopped state. This confirms that the Lambda function we set up has successfully stopped the EC2 instance according to the schedule we configured. This automation helps streamline your AWS management tasks, ensuring that your resources are used efficiently.

Conclusion

In the Conclusion, we have done how to automate the starting and stopping of EC2 instances in AWS using IAM, Lambda and EventBridge. By scheduling these actions, you can optimize your costs and ensure that your instances are only running when needed.

Talk About Your Business