Sample code for 30+ languages & platforms
Rust

DynamoDB - CreateTable

See more Amazon DynamoDB Examples

The CreateTable operation adds a new table to your account. In an AWS account, table names must be unique within each Region. That is, you can have two tables with same name if you create the tables in different Regions.

CreateTable is an asynchronous operation. Upon receiving a CreateTable request, DynamoDB immediately returns a response with a TableStatus of CREATING. After the table is created, DynamoDB sets the TableStatus to ACTIVE. You can perform read and write operations only on an ACTIVE table.

You can use the DescribeTable action to check the table status.

Chilkat Rust Downloads

Rust

let rest = chilkat::Rest::new();

// Implements the following CURL command:

// curl -X POST https://dynamodb.us-west-2.amazonaws.com/ \
//   -H "Accept-Encoding: identity" \
//   -H "Content-Type: application/x-amz-json-1.0" \
//   -H "Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=<Headers>, Signature=<Signature>" \
//   -H "X-Amz-Date: <Date>" \
//   -H "X-Amz-Target: DynamoDB_20120810.CreateTable" \
//   -d '{
//     "AttributeDefinitions": [
//         {
//             "AttributeName": "ForumName",
//             "AttributeType": "S"
//         },
//         {
//             "AttributeName": "Subject",
//             "AttributeType": "S"
//         },
//         {
//             "AttributeName": "LastPostDateTime",
//             "AttributeType": "S"
//         }
//     ],
//     "TableName": "Thread",
//     "KeySchema": [
//         {
//             "AttributeName": "ForumName",
//             "KeyType": "HASH"
//         },
//         {
//             "AttributeName": "Subject",
//             "KeyType": "RANGE"
//         }
//     ],
//     "LocalSecondaryIndexes": [
//         {
//             "IndexName": "LastPostIndex",
//             "KeySchema": [
//                 {
//                     "AttributeName": "ForumName",
//                     "KeyType": "HASH"
//                 },
//                 {
//                     "AttributeName": "LastPostDateTime",
//                     "KeyType": "RANGE"
//                 }
//             ],
//             "Projection": {
//                 "ProjectionType": "KEYS_ONLY"
//             }
//         }
//     ],
//     "ProvisionedThroughput": {
//         "ReadCapacityUnits": 5,
//         "WriteCapacityUnits": 5
//     },
//     "Tags": [ 
//       { 
//          "Key": "Owner",
//          "Value": "BlueTeam"
//       }
//    ]
// }'

// Use the following online tool to generate REST code from a CURL command
// Convert a cURL Command to REST Source Code

let auth_aws = chilkat::AuthAws::new();
auth_aws.set_access_key("AWS_ACCESS_KEY");
auth_aws.set_secret_key("AWS_SECRET_KEY");
// Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
auth_aws.set_region("us-west-2");
auth_aws.set_service_name("dynamodb");
// SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
let _ = rest.set_auth_aws(&auth_aws);

// URL: https://dynamodb.us-west-2.amazonaws.com/
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
// Don't forget to change the region domain (us-west-2.amazonaws.com) to your particular region.
if rest.connect("dynamodb.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect).is_err() {
    println!("ConnectFailReason: {}", rest.connect_fail_reason());
    println!("{}", rest.last_error_text());
    return;
}

// Note: The above code does not need to be repeatedly called for each REST request.
// The rest object can be setup once, and then many requests can be sent.  Chilkat will automatically
// reconnect within a FullRequest* method as needed.  It is only the very first connection that is explicitly
// made via the Connect method.

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

// The following JSON is sent in the request body.

// {
//   "AttributeDefinitions": [
//     {
//       "AttributeName": "ForumName",
//       "AttributeType": "S"
//     },
//     {
//       "AttributeName": "Subject",
//       "AttributeType": "S"
//     },
//     {
//       "AttributeName": "LastPostDateTime",
//       "AttributeType": "S"
//     }
//   ],
//   "TableName": "Thread",
//   "KeySchema": [
//     {
//       "AttributeName": "ForumName",
//       "KeyType": "HASH"
//     },
//     {
//       "AttributeName": "Subject",
//       "KeyType": "RANGE"
//     }
//   ],
//   "LocalSecondaryIndexes": [
//     {
//       "IndexName": "LastPostIndex",
//       "KeySchema": [
//         {
//           "AttributeName": "ForumName",
//           "KeyType": "HASH"
//         },
//         {
//           "AttributeName": "LastPostDateTime",
//           "KeyType": "RANGE"
//         }
//       ],
//       "Projection": {
//         "ProjectionType": "KEYS_ONLY"
//       }
//     }
//   ],
//   "ProvisionedThroughput": {
//     "ReadCapacityUnits": 5,
//     "WriteCapacityUnits": 5
//   },
//   "Tags": [
//     {
//       "Key": "Owner",
//       "Value": "BlueTeam"
//     }
//   ]
// }

let json = chilkat::JsonObject::new();
let mut i = 0;
json.set_i(i);
let _ = json.update_string("AttributeDefinitions[i].AttributeName", "ForumName");
let _ = json.update_string("AttributeDefinitions[i].AttributeType", "S");
i = i + 1;
json.set_i(i);
let _ = json.update_string("AttributeDefinitions[i].AttributeName", "Subject");
let _ = json.update_string("AttributeDefinitions[i].AttributeType", "S");
i = i + 1;
json.set_i(i);
let _ = json.update_string("AttributeDefinitions[i].AttributeName", "LastPostDateTime");
let _ = json.update_string("AttributeDefinitions[i].AttributeType", "S");
let _ = json.update_string("TableName", "Thread");
i = 0;
json.set_i(i);
let _ = json.update_string("KeySchema[i].AttributeName", "ForumName");
let _ = json.update_string("KeySchema[i].KeyType", "HASH");
i = i + 1;
json.set_i(i);
let _ = json.update_string("KeySchema[i].AttributeName", "Subject");
let _ = json.update_string("KeySchema[i].KeyType", "RANGE");

i = 0;
json.set_i(i);
let mut j = 0;
json.set_j(j);
let _ = json.update_string("LocalSecondaryIndexes[i].IndexName", "LastPostIndex");
let _ = json.update_string("LocalSecondaryIndexes[i].KeySchema[j].AttributeName", "ForumName");
let _ = json.update_string("LocalSecondaryIndexes[i].KeySchema[j].KeyType", "HASH");
j = j + 1;
json.set_j(j);
let _ = json.update_string("LocalSecondaryIndexes[i].KeySchema[j].AttributeName", "LastPostDateTime");
let _ = json.update_string("LocalSecondaryIndexes[i].KeySchema[j].KeyType", "RANGE");
let _ = json.update_string("LocalSecondaryIndexes[i].Projection.ProjectionType", "KEYS_ONLY");
let _ = json.update_int("ProvisionedThroughput.ReadCapacityUnits", 5);
let _ = json.update_int("ProvisionedThroughput.WriteCapacityUnits", 5);
let _ = json.update_string("Tags[0].Key", "Owner");
let _ = json.update_string("Tags[0].Value", "BlueTeam");

let _ = rest.add_header("Content-Type", "application/x-amz-json-1.0");
let _ = rest.add_header("X-Amz-Target", "DynamoDB_20120810.CreateTable");
let _ = rest.add_header("Accept-Encoding", "identity");

let sb_request_body = chilkat::StringBuilder::new();
let _ = json.emit_sb(&sb_request_body);
let sb_response_body = chilkat::StringBuilder::new();
if rest.full_request_sb("POST", "/", &sb_request_body, &sb_response_body).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

let resp_status_code = rest.response_status_code();
println!("response status code = {}", resp_status_code);

if resp_status_code >= 400 {
    println!("Response Status Code = {}", resp_status_code);
    println!("Response Header:");
    println!("{}", rest.response_header());
    println!("Response Body:");
    println!("{}", sb_response_body.get_as_string().unwrap_or_default());
    return;
}

let json_response = chilkat::JsonObject::new();
let _ = json_response.load_sb(&sb_response_body);

json_response.set_emit_compact(false);
println!("{}", json_response.emit().unwrap_or_default());

// Sample Response:

//  {
//     "TableDescription": {
//         "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/Thread",
//         "AttributeDefinitions": [
//             {
//                 "AttributeName": "ForumName",
//                 "AttributeType": "S"
//             },
//             {
//                 "AttributeName": "LastPostDateTime",
//                 "AttributeType": "S"
//             },
//             {
//                 "AttributeName": "Subject",
//                 "AttributeType": "S"
//             }
//         ],
//         "CreationDateTime": 1.36372808007E9,
//         "ItemCount": 0,
//         "KeySchema": [
//             {
//                 "AttributeName": "ForumName",
//                 "KeyType": "HASH"
//             },
//             {
//                 "AttributeName": "Subject",
//                 "KeyType": "RANGE"
//             }
//         ],
//         "LocalSecondaryIndexes": [
//             {
//                 "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/Thread/index/LastPostIndex",
//                 "IndexName": "LastPostIndex",
//                 "IndexSizeBytes": 0,
//                 "ItemCount": 0,
//                 "KeySchema": [
//                     {
//                         "AttributeName": "ForumName",
//                         "KeyType": "HASH"
//                     },
//                     {
//                         "AttributeName": "LastPostDateTime",
//                         "KeyType": "RANGE"
//                     }
//                 ],
//                 "Projection": {
//                     "ProjectionType": "KEYS_ONLY"
//                 }
//             }
//         ],
//         "ProvisionedThroughput": {
//             "NumberOfDecreasesToday": 0,
//             "ReadCapacityUnits": 5,
//             "WriteCapacityUnits": 5
//         },
//         "TableName": "Thread",
//         "TableSizeBytes": 0,
//         "TableStatus": "CREATING"
//     }
// }

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

let mut count_j: i32 = 0;

let table_description_table_arn = json_response.string_of("TableDescription.TableArn").unwrap_or_default();
let table_description_creation_date_time = json_response.string_of("TableDescription.CreationDateTime").unwrap_or_default();
let table_description_item_count = json_response.int_of("TableDescription.ItemCount");
let table_description_provisioned_throughput_number_of_decreases_today = json_response.int_of("TableDescription.ProvisionedThroughput.NumberOfDecreasesToday");
let table_description_provisioned_throughput_read_capacity_units = json_response.int_of("TableDescription.ProvisionedThroughput.ReadCapacityUnits");
let table_description_provisioned_throughput_write_capacity_units = json_response.int_of("TableDescription.ProvisionedThroughput.WriteCapacityUnits");
let table_description_table_name = json_response.string_of("TableDescription.TableName").unwrap_or_default();
let table_description_table_size_bytes = json_response.int_of("TableDescription.TableSizeBytes");
let table_description_table_status = json_response.string_of("TableDescription.TableStatus").unwrap_or_default();
i = 0;
let mut count_i = json_response.size_of_array("TableDescription.AttributeDefinitions");
while i < count_i {
    json_response.set_i(i);
    let _ = json_response.string_of("TableDescription.AttributeDefinitions[i].AttributeName").unwrap_or_default();
    let _ = json_response.string_of("TableDescription.AttributeDefinitions[i].AttributeType").unwrap_or_default();
    i = i + 1;
}

i = 0;
count_i = json_response.size_of_array("TableDescription.KeySchema");
while i < count_i {
    json_response.set_i(i);
    let _ = json_response.string_of("TableDescription.KeySchema[i].AttributeName").unwrap_or_default();
    let _ = json_response.string_of("TableDescription.KeySchema[i].KeyType").unwrap_or_default();
    i = i + 1;
}

i = 0;
count_i = json_response.size_of_array("TableDescription.LocalSecondaryIndexes");
while i < count_i {
    json_response.set_i(i);
    let _ = json_response.string_of("TableDescription.LocalSecondaryIndexes[i].IndexArn").unwrap_or_default();
    let _ = json_response.string_of("TableDescription.LocalSecondaryIndexes[i].IndexName").unwrap_or_default();
    let _ = json_response.int_of("TableDescription.LocalSecondaryIndexes[i].IndexSizeBytes");
    let _ = json_response.int_of("TableDescription.LocalSecondaryIndexes[i].ItemCount");
    let _ = json_response.string_of("TableDescription.LocalSecondaryIndexes[i].Projection.ProjectionType").unwrap_or_default();
    j = 0;
    count_j = json_response.size_of_array("TableDescription.LocalSecondaryIndexes[i].KeySchema");
    while j < count_j {
        json_response.set_j(j);
        let _ = json_response.string_of("TableDescription.LocalSecondaryIndexes[i].KeySchema[j].AttributeName").unwrap_or_default();
        let _ = json_response.string_of("TableDescription.LocalSecondaryIndexes[i].KeySchema[j].KeyType").unwrap_or_default();
        j = j + 1;
    }

    i = i + 1;
}