AWS SDK for Ruby
Developer Guide

Terminating an Amazon EC2 Instance

The following example terminates the Amazon EC2 instance i-123abc in the us-west-2 region.

require 'aws-sdk-ec2' # v2: require 'aws-sdk' ec2 = Aws::EC2::Resource.new(region: 'us-west-2') i = ec2.instance('i-123abc') if i.exists? case i.state.code when 48 # terminated puts "#{id} is already terminated" else i.terminate end end