Sample code for 30+ languages & platforms
Zig

AWS Secrets Manager - Create Secret

See more AWS Secrets Manager Examples

Creates a new secret. A secret is a set of credentials, such as a user name and password, that you store in an encrypted form in Secrets Manager.

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.

    // Sends the following sample request.
    // Note: Chilkat will automatically add Content-Length, X-Amz-Date, Accept-Encoding, and Authorization

    // POST / HTTP/1.1
    // Host: secretsmanager.region.domain
    // Accept-Encoding: identity
    // X-Amz-Target: secretsmanager.CreateSecret
    // Content-Type: application/x-amz-json-1.1
    // X-Amz-Date: <date>
    // Authorization: AWS4-HMAC-SHA256 Credential=<credentials>,SignedHeaders=<headers>, Signature=<signature>
    // Content-Length: <payload-size-bytes>
    //
    // {
    //   "Name": "MyTestDatabaseSecret2",
    //   "Description": "My test database secret created with the CLI",
    //   "SecretString": "{\"username\":\"david\",\"password\":\"BnQw!XDWgaEeT9XGTT29\"}", "ClientRequestToken": "EXAMPLE1-90ab-cdef-fedc-ba987SECRET1"
    // }

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect to the Amazon AWS REST server.
    // such as https://secretsmanager.us-west-2.amazonaws.com/
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("secretsmanager.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("secretsmanager");

    rest.setAuthAws(auth_aws) catch {};

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

    const json = try chilkat.JsonObject.init();
    defer json.deinit();

    json.updateString("Name", "MyTestDatabaseSecret2") catch {};
    json.updateString("Description", "My test database secret created with the CLI") catch {};
    json.updateString("SecretString", "{\"username\":\"david\",\"password\":\"BnQw!XDWgaEeT9XGTT29\"}") catch {};
    json.updateString("ClientRequestToken", "EXAMPLE1-90ab-cdef-fedc-ba987SECRET1") catch {};

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

    const sb_request_body = try chilkat.StringBuilder.init();
    defer sb_request_body.deinit();
    json.emitSb(sb_request_body) catch {};
    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    rest.fullRequestSb("POST", "/", sb_request_body, sb_response_body) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const resp_status_code = rest.getResponseStatusCode();
    std.debug.print("response status code = {d}\n", .{resp_status_code});

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.setEmitCompact(false);
    j_resp.loadSb(sb_response_body) catch {};

    if (resp_status_code >= 400) {
        std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("Response Body:\n", .{});
        std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
        return;
    }

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    // Sample response body:

    // {
    //   "ARN": "arn:aws:secretsmanager:us-west-2:954491834127:secret:MyTestDatabaseSecret2-F16Tps",
    //   "Name": "MyTestDatabaseSecret2",
    //   "VersionId": "EXAMPLE1-90ab-cdef-fedc-ba987SECRET1"
    // }

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

    _ = try j_resp.stringOf(alloc, "ARN");
    _ = try j_resp.stringOf(alloc, "Name");
    _ = try j_resp.stringOf(alloc, "VersionId");
}