Skip to content

Instantly share code, notes, and snippets.

@glm4
Last active September 16, 2019 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glm4/150bff52e466f9d60fb30a7907276147 to your computer and use it in GitHub Desktop.
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
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