Zig
Zig
EC2 Stop Instances
See more Amazon EC2 Examples
Demonstrates how to send a request to stop Amazon EC2 instances.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect to the Amazon AWS server.
// such as https://ec2.amazonaws.com/
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
try rest.connect("ec2.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect);
// Provide AWS credentials for the REST call.
const auth_aws = try chilkat.AuthAws.init();
defer auth_aws.deinit();
auth_aws.setAccessKey("AWS_ACCESS_KEY");
auth_aws.setSecretKey("AWS_SECRET_KEY");
// the region should match the region part of the domain passed to the REST connect method (above).
auth_aws.setRegion("us-west-2");
auth_aws.setServiceName("ec2");
rest.setAuthAws(auth_aws) catch {};
rest.addQueryParam("Action", "StopInstances") catch {};
rest.addQueryParam("Version", "2016-11-15") catch {};
// 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", "true") catch {};
rest.addQueryParam("InstanceId.1", "i-999719995399c9999") catch {};
// 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.
const response_xml = rest.fullRequestNoBody(alloc, "GET", "/") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// A successful response will have a status code equal to 200.
if (rest.getResponseStatusCode() != 200) {
std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("response body: {s}\n", .{response_xml});
return;
}
// Examine the successful XML response.
const xml = try chilkat.Xml.init();
defer xml.deinit();
xml.loadXml(response_xml) catch {};
std.debug.print("{s}\n", .{try xml.getXml(alloc)});
}