Sample code for 30+ languages & platforms
Objective-C

EC2 Stop Instances

See more Amazon EC2 Examples

Demonstrates how to send a request to stop Amazon EC2 instances.

Chilkat Objective-C Downloads

Objective-C
#import <CkoRest.h>
#import <CkoAuthAws.h>
#import <NSString.h>
#import <CkoXml.h>

BOOL success = NO;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

CkoRest *rest = [[CkoRest alloc] init];

// Connect to the Amazon AWS server.
// such as https://ec2.amazonaws.com/
BOOL bTls = YES;
int port = 443;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"ec2.us-west-2.amazonaws.com" port: [NSNumber numberWithInt: port] tls: bTls autoReconnect: bAutoReconnect];

// Provide AWS credentials for the REST call.
CkoAuthAws *authAws = [[CkoAuthAws alloc] init];
authAws.AccessKey = @"AWS_ACCESS_KEY";
authAws.SecretKey = @"AWS_SECRET_KEY";
// the region should match the region part of the domain passed to the REST connect method (above).
authAws.Region = @"us-west-2";
authAws.ServiceName = @"ec2";

[rest SetAuthAws: authAws];

[rest AddQueryParam: @"Action" value: @"StopInstances"];
[rest AddQueryParam: @"Version" value: @"2016-11-15"];
// If DryRun is true then checks whether you have the required permissions for the action, 
// without actually making the request, and provides an error response. If you have the required permissions, 
// the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.
// Omit the DryRun query parameter to actually start the instance.  (Or set DryRun equal to "false")
[rest AddQueryParam: @"DryRun" value: @"true"];
[rest AddQueryParam: @"InstanceId.1" value: @"i-999719995399c9999"];

// See EC2 Stop Instances Reference Documentation
// for more information about the Force and Hibernate query params, which are optional and true/false just like DryRun.

NSString *responseXml = [rest FullRequestNoBody: @"GET" uriPath: @"/"];
if (rest.LastMethodSuccess != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

// A successful response will have a status code equal to 200.
if ([rest.ResponseStatusCode intValue] != 200) {
    NSLog(@"%@%d",@"response status code = ",[rest.ResponseStatusCode intValue]);
    NSLog(@"%@%@",@"response status text = ",rest.ResponseStatusText);
    NSLog(@"%@%@",@"response header: ",rest.ResponseHeader);
    NSLog(@"%@%@",@"response body: ",responseXml);
    return;
}

// Examine the successful XML response.
CkoXml *xml = [[CkoXml alloc] init];
[xml LoadXml: responseXml];
NSLog(@"%@",[xml GetXml]);