#!/usr/bin/perl use Net::Amazon::EC2; my $ec2 = Net::Amazon::EC2->new( AWSAccessKeyId => 'XXXX', SecretAccessKey => 'XXXX' ); my $running_instances = $ec2->describe_instances; foreach my $reservation (@$running_instances) { foreach my $instance ($reservation->instances_set) { my $instance_id = $instance->instance_id; my $result = $ec2->terminate_instances(InstanceId => $instance_id); print "terminating" . $instance_id . "n"; } }
Alternate method in bash:
#!/bin/bash export JAVA_HOME=/usr export EC2_HOME=/opt/ec2/tools export EC2_PRIVATE_KEY=/root/ec2/pk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem export EC2_CERT=/root/ec2/cert-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem echo "Terminating Origin Server..." if test -f /root/ec2/origin.iid then date >> /root/term.log /opt/ec2/tools/bin/ec2-terminate-instances `cat /root/ec2/origin.iid` >> /root/term.log else echo "Instance ID not found!" fi
PHP (requires AWS SDK for PHP):
<?php error_reporting(-1); header("Content-type: text/html; charset=utf-8"); require_once '/usr/share/php/AWSSDKforPHP/sdk.class.php'; define('AWS_KEY', 'access key here'); define('AWS_SECRET_KEY', 'secret key here']); $startup_region = 'us-east-1'; $ec2 = new AmazonEC2(); ?> <HTML> <HEAD> <TITLE>Instance Termination</TITLE> <link href="loginmodule.css" rel="stylesheet" type="text/css" /> </TITLE> </HEAD> <BODY> <H1>Terminate Instances</H1> <?php $instances[] = 'i-xxxxxxxx'; $term_result = $ec2->terminate_instances($instances); if ($term_result->status != "200") { print "Termination error<BR>"; print_r($term_result); die('ERROR'); print "Instance termination complete. \n"; ?> </BODY> </HTML>