Start or stop an ec2 instance manually using aws lambda. brief overview of each function within the Ec2Toggle service:
-
- Purpose: List all EC2 instance information in all regions, including details such as instance name, ID, region, and status. This information is later passed to the startOrStopAnInstance function to start/stop an instance manually.
-
- Purpose: Switches the current state of a given EC2 instance from start to stop an instance manually and vice versa(event include instanceId and region).
-
- Purpose: Get all the instances from all the regions and check if the network activity of each instance is less than zero for the past 6 hours. If it is zero, deactivate the instance.
- Event: Scheduled to run every 6 hours.
In order to deploy the example, you need to run the following command:
$ serverless deploy
After successful deployment, you can invoke the deployed function by using the following command:
serverless invoke --function ListInstances
You can invoke your function locally by using the following command:
serverless invoke local --function ListInstances
serverless invoke local --function startOrStopAnInstance -p event.json
Which should result in response similar to the following:
{
"statusCode": 200,
"body": "{\n \"message\": \"List of EC2 instances retrieved successfully!\",\n \"instances\": [{\"id\": \"i-123456sad7890abcdef0\", \"name\": \"example-instance\", \"region\": \"us-east-1\", \"status\": \"running\"}]\n}"
}