Sample code for 30+ languages & platforms
Zig

AWS KMS List Keys

See more AWS KMS Examples

Gets a list of all KMS keys in the caller's AWS account and Region.

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 REST server.
    // Make sure to use the region that is correct for you.
    // such as https://kms.us-west-2.amazonaws.com/
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("kms.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 our URL above..
    auth_aws.setRegion("us-west-2");
    auth_aws.setServiceName("kms");

    rest.setAuthAws(auth_aws) catch {};

    rest.addHeader("X-Amz-Target", "TrentService.ListKeys") catch {};
    rest.addHeader("Content-Type", "application/x-amz-json-1.1") catch {};

    const str_json = rest.fullRequestString(alloc, "POST", "/", "{}") 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", .{str_json});
        return;
    }

    // Examine the successful JSON response (shown below)
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(str_json) catch {};
    json.setEmitCompact(false);

    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Sample output:

    // {
    //   "KeyCount": 4,
    //   "Keys": [
    //     {
    //       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/082f8520-7afc-4a09-b703-89b7243072e5",
    //       "KeyId": "082f8520-7afc-4a09-b703-89b7243072e5"
    //     },
    //     {
    //       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/17432483-ff08-4950-93d3-f46ebb5e17d1",
    //       "KeyId": "17432483-ff08-4950-93d3-f46ebb5e17d1"
    //     },
    //     {
    //       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/1b0e5b3c-0675-4510-adb6-a75b40a93da0",
    //       "KeyId": "1b0e5b3c-0675-4510-adb6-a75b40a93da0"
    //     },
    //     {
    //       "KeyArn": "arn:aws:kms:us-west-2:954491834127:key/265e3993-428b-4581-9466-b1030a53062f",
    //       "KeyId": "265e3993-428b-4581-9466-b1030a53062f"
    //     },
    //   ],
    //   "Truncated": false
    // }

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var key_arn: [:0]const u8 = "";
    var key_id: [:0]const u8 = "";

    _ = json.intOf("KeyCount");
    _ = json.boolOf("Truncated");
    var i: i32 = 0;
    const count_i = json.sizeOfArray("Keys");
    while (i < count_i) {
        json.setI(i);
        key_arn = try json.stringOf(alloc, "Keys[i].KeyArn");
        key_id = try json.stringOf(alloc, "Keys[i].KeyId");
        i = i + 1;
    }
}