Sample code for 30+ languages & platforms
Zig

EC2 Start Instances

See more Amazon EC2 Examples

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

Chilkat Zig Downloads

Zig
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", "StartInstances") 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 {};

    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)});
}