Last active
September 16, 2019 16:29
-
-
Save glm4/150bff52e466f9d60fb30a7907276147 to your computer and use it in GitHub Desktop.
AWS Lambda function to reboot EC2 instances. Ruby 2.5 Runtime. Execution Role needs: Allow: ec2:RebootInstances, Allow: ec2:DescribeInstances, Allow: ec2:DescribeTags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk-ec2' | |
# You can customize the lambda function in AWS console to use ENV variables or to receive | |
# the region, tags or any identification for your EC2 instances in the event message. | |
def lambda_handler(event:, context:) | |
ec2 = Aws::EC2::Resource.new(region: 'us-east-2') | |
instances = ec2.instances(filters: [ | |
{ | |
name: "tag:Role", | |
values: ["API"], | |
}, | |
{ | |
name: "tag:Env", | |
values: ["Production"], | |
} | |
]) | |
instances.batch_reboot | |
"Restarting: #{instances.map(&:id).join(',')}..." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment