11 June 2022

Configure an Event Bridge to stop EC2 instance

Sometimes it is hard to keep track of your EC2 instance status and when you fail to do so you will end up
getting high bills based on their usage. 

You can cut down your bills by scheduling EventBridge rules for the instances you are not using.

Go to Amazon EventBridge > Create Rule

  1. Create a new rule and select Rule type as Schedule.

  2. Select Schedule Pattern as - A fine-grained schedule that runs at a specific time

  3. Define the cron expression for instance shutdown schedule.

     For eg. cron(30 17 * * ? *)  this will stop your EC2 instance at 17:30 everyday

    select time zone either as UTC or Local Time Zone 

  4. For Target1, select:

    • Target Type as "AWS service"
    • Target as "EC2 StopInstances API call", and
    • provide Instance ID to shut down as per schedule.
    • Finally, select Create a new role for this specific resource

  5. Review the schedule and instance detail and finish creating the rule.



    You can use the method mentioned above (using lambda) to start the same instance again.

Known errors -


while adding an eventBridge rule you might get execution role error as show below -


which can be fixed by updating the trust relationship for the role attached to your instance.

update the attached role to assume scheduler as trusted principal.


{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "ec2.amazonaws.com",
                    "scheduler.amazonaws.com"
                ]
             },
             "Action": "sts:AssumeRole"
        }
     ]
}



to be continued...

No comments:

Post a Comment