Method: Aws::DynamoDB::Client#create_table
- Defined in:
- lib/aws-sdk-dynamodb/client.rb
#create_table(params = {}) ⇒ Types::CreateTableOutput
The ‘CreateTable` operation adds a new table to your account. In an Amazon Web Services 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 optionally define secondary indexes on the new table, as part of the ‘CreateTable` operation. If you want to create multiple tables with secondary indexes on them, you must create the tables sequentially. Only one table with secondary indexes can be in the `CREATING` state at any given time.
You can use the ‘DescribeTable` action to check the table status.
Examples:
Example: To create a table
Example: To create a table
# This example creates a table named Music.
resp = client.create_table({
attribute_definitions: [
{
attribute_name: "Artist",
attribute_type: "S",
},
{
attribute_name: "SongTitle",
attribute_type: "S",
},
],
key_schema: [
{
attribute_name: "Artist",
key_type: "HASH",
},
{
attribute_name: "SongTitle",
key_type: "RANGE",
},
],
provisioned_throughput: {
read_capacity_units: 5,
write_capacity_units: 5,
},
table_name: "Music",
})
resp.to_h outputs the following:
{
table_description: {
attribute_definitions: [
{
attribute_name: "Artist",
attribute_type: "S",
},
{
attribute_name: "SongTitle",
attribute_type: "S",
},
],
creation_date_time: Time.parse("1421866952.062"),
item_count: 0,
key_schema: [
{
attribute_name: "Artist",
key_type: "HASH",
},
{
attribute_name: "SongTitle",
key_type: "RANGE",
},
],
provisioned_throughput: {
read_capacity_units: 5,
write_capacity_units: 5,
},
table_name: "Music",
table_size_bytes: 0,
table_status: "CREATING",
},
}
Request syntax with placeholder values
Request syntax with placeholder values
resp = client.create_table({
attribute_definitions: [ # required
{
attribute_name: "KeySchemaAttributeName", # required
attribute_type: "S", # required, accepts S, N, B
},
],
table_name: "TableArn", # required
key_schema: [ # required
{
attribute_name: "KeySchemaAttributeName", # required
key_type: "HASH", # required, accepts HASH, RANGE
},
],
local_secondary_indexes: [
{
index_name: "IndexName", # required
key_schema: [ # required
{
attribute_name: "KeySchemaAttributeName", # required
key_type: "HASH", # required, accepts HASH, RANGE
},
],
projection: { # required
projection_type: "ALL", # accepts ALL, KEYS_ONLY, INCLUDE
non_key_attributes: ["NonKeyAttributeName"],
},
},
],
global_secondary_indexes: [
{
index_name: "IndexName", # required
key_schema: [ # required
{
attribute_name: "KeySchemaAttributeName", # required
key_type: "HASH", # required, accepts HASH, RANGE
},
],
projection: { # required
projection_type: "ALL", # accepts ALL, KEYS_ONLY, INCLUDE
non_key_attributes: ["NonKeyAttributeName"],
},
provisioned_throughput: {
read_capacity_units: 1, # required
write_capacity_units: 1, # required
},
on_demand_throughput: {
max_read_request_units: 1,
max_write_request_units: 1,
},
warm_throughput: {
read_units_per_second: 1,
write_units_per_second: 1,
},
},
],
billing_mode: "PROVISIONED", # accepts PROVISIONED, PAY_PER_REQUEST
provisioned_throughput: {
read_capacity_units: 1, # required
write_capacity_units: 1, # required
},
stream_specification: {
stream_enabled: false, # required
stream_view_type: "NEW_IMAGE", # accepts NEW_IMAGE, OLD_IMAGE, NEW_AND_OLD_IMAGES, KEYS_ONLY
},
sse_specification: {
enabled: false,
sse_type: "AES256", # accepts AES256, KMS
kms_master_key_id: "KMSMasterKeyId",
},
tags: [
{
key: "TagKeyString", # required
value: "TagValueString", # required
},
],
table_class: "STANDARD", # accepts STANDARD, STANDARD_INFREQUENT_ACCESS
deletion_protection_enabled: false,
warm_throughput: {
read_units_per_second: 1,
write_units_per_second: 1,
},
resource_policy: "ResourcePolicy",
on_demand_throughput: {
max_read_request_units: 1,
max_write_request_units: 1,
},
})
Response structure
Response structure
resp.table_description.attribute_definitions #=> Array
resp.table_description.attribute_definitions[0].attribute_name #=> String
resp.table_description.attribute_definitions[0].attribute_type #=> String, one of "S", "N", "B"
resp.table_description.table_name #=> String
resp.table_description.key_schema #=> Array
resp.table_description.key_schema[0].attribute_name #=> String
resp.table_description.key_schema[0].key_type #=> String, one of "HASH", "RANGE"
resp.table_description.table_status #=> String, one of "CREATING", "UPDATING", "DELETING", "ACTIVE", "INACCESSIBLE_ENCRYPTION_CREDENTIALS", "ARCHIVING", "ARCHIVED", "REPLICATION_NOT_AUTHORIZED"
resp.table_description.creation_date_time #=> Time
resp.table_description.provisioned_throughput.last_increase_date_time #=> Time
resp.table_description.provisioned_throughput.last_decrease_date_time #=> Time
resp.table_description.provisioned_throughput.number_of_decreases_today #=> Integer
resp.table_description.provisioned_throughput.read_capacity_units #=> Integer
resp.table_description.provisioned_throughput.write_capacity_units #=> Integer
resp.table_description.table_size_bytes #=> Integer
resp.table_description.item_count #=> Integer
resp.table_description.table_arn #=> String
resp.table_description.table_id #=> String
resp.table_description.billing_mode_summary.billing_mode #=> String, one of "PROVISIONED", "PAY_PER_REQUEST"
resp.table_description.billing_mode_summary.last_update_to_pay_per_request_date_time #=> Time
resp.table_description.local_secondary_indexes #=> Array
resp.table_description.local_secondary_indexes[0].index_name #=> String
resp.table_description.local_secondary_indexes[0].key_schema #=> Array
resp.table_description.local_secondary_indexes[0].key_schema[0].attribute_name #=> String
resp.table_description.local_secondary_indexes[0].key_schema[0].key_type #=> String, one of "HASH", "RANGE"
resp.table_description.local_secondary_indexes[0].projection.projection_type #=> String, one of "ALL", "KEYS_ONLY", "INCLUDE"
resp.table_description.local_secondary_indexes[0].projection.non_key_attributes #=> Array
resp.table_description.local_secondary_indexes[0].projection.non_key_attributes[0] #=> String
resp.table_description.local_secondary_indexes[0].index_size_bytes #=> Integer
resp.table_description.local_secondary_indexes[0].item_count #=> Integer
resp.table_description.local_secondary_indexes[0].index_arn #=> String
resp.table_description.global_secondary_indexes #=> Array
resp.table_description.global_secondary_indexes[0].index_name #=> String
resp.table_description.global_secondary_indexes[0].key_schema #=> Array
resp.table_description.global_secondary_indexes[0].key_schema[0].attribute_name #=> String
resp.table_description.global_secondary_indexes[0].key_schema[0].key_type #=> String, one of "HASH", "RANGE"
resp.table_description.global_secondary_indexes[0].projection.projection_type #=> String, one of "ALL", "KEYS_ONLY", "INCLUDE"
resp.table_description.global_secondary_indexes[0].projection.non_key_attributes #=> Array
resp.table_description.global_secondary_indexes[0].projection.non_key_attributes[0] #=> String
resp.table_description.global_secondary_indexes[0].index_status #=> String, one of "CREATING", "UPDATING", "DELETING", "ACTIVE"
resp.table_description.global_secondary_indexes[0].backfilling #=> Boolean
resp.table_description.global_secondary_indexes[0].provisioned_throughput.last_increase_date_time #=> Time
resp.table_description.global_secondary_indexes[0].provisioned_throughput.last_decrease_date_time #=> Time
resp.table_description.global_secondary_indexes[0].provisioned_throughput.number_of_decreases_today #=> Integer
resp.table_description.global_secondary_indexes[0].provisioned_throughput.read_capacity_units #=> Integer
resp.table_description.global_secondary_indexes[0].provisioned_throughput.write_capacity_units #=> Integer
resp.table_description.global_secondary_indexes[0].index_size_bytes #=> Integer
resp.table_description.global_secondary_indexes[0].item_count #=> Integer
resp.table_description.global_secondary_indexes[0].index_arn #=> String
resp.table_description.global_secondary_indexes[0].on_demand_throughput.max_read_request_units #=> Integer
resp.table_description.global_secondary_indexes[0].on_demand_throughput.max_write_request_units #=> Integer
resp.table_description.global_secondary_indexes[0].warm_throughput.read_units_per_second #=> Integer
resp.table_description.global_secondary_indexes[0].warm_throughput.write_units_per_second #=> Integer
resp.table_description.global_secondary_indexes[0].warm_throughput.status #=> String, one of "CREATING", "UPDATING", "DELETING", "ACTIVE"
resp.table_description.stream_specification.stream_enabled #=> Boolean
resp.table_description.stream_specification.stream_view_type #=> String, one of "NEW_IMAGE", "OLD_IMAGE", "NEW_AND_OLD_IMAGES", "KEYS_ONLY"
resp.table_description.latest_stream_label #=> String
resp.table_description.latest_stream_arn #=> String
resp.table_description.global_table_version #=> String
resp.table_description.replicas #=> Array
resp.table_description.replicas[0].region_name #=> String
resp.table_description.replicas[0].replica_status #=> String, one of "CREATING", "CREATION_FAILED", "UPDATING", "DELETING", "ACTIVE", "REGION_DISABLED", "INACCESSIBLE_ENCRYPTION_CREDENTIALS", "ARCHIVING", "ARCHIVED", "REPLICATION_NOT_AUTHORIZED"
resp.table_description.replicas[0].replica_status_description #=> String
resp.table_description.replicas[0].replica_status_percent_progress #=> String
resp.table_description.replicas[0].kms_master_key_id #=> String
resp.table_description.replicas[0].provisioned_throughput_override.read_capacity_units #=> Integer
resp.table_description.replicas[0].on_demand_throughput_override.max_read_request_units #=> Integer
resp.table_description.replicas[0].warm_throughput.read_units_per_second #=> Integer
resp.table_description.replicas[0].warm_throughput.write_units_per_second #=> Integer
resp.table_description.replicas[0].warm_throughput.status #=> String, one of "CREATING", "UPDATING", "DELETING", "ACTIVE", "INACCESSIBLE_ENCRYPTION_CREDENTIALS", "ARCHIVING", "ARCHIVED", "REPLICATION_NOT_AUTHORIZED"
resp.table_description.replicas[0].global_secondary_indexes #=> Array
resp.table_description.replicas[0].global_secondary_indexes[0].index_name #=> String
resp.table_description.replicas[0].global_secondary_indexes[0].provisioned_throughput_override.read_capacity_units #=> Integer
resp.table_description.replicas[0].global_secondary_indexes[0].on_demand_throughput_override.max_read_request_units #=> Integer
resp.table_description.replicas[0].global_secondary_indexes[0].warm_throughput.read_units_per_second #=> Integer
resp.table_description.replicas[0].global_secondary_indexes[0].warm_throughput.write_units_per_second #=> Integer
resp.table_description.replicas[0].global_secondary_indexes[0].warm_throughput.status #=> String, one of "CREATING", "UPDATING", "DELETING", "ACTIVE"
resp.table_description.replicas[0].replica_inaccessible_date_time #=> Time
resp.table_description.replicas[0].replica_table_class_summary.table_class #=> String, one of "STANDARD", "STANDARD_INFREQUENT_ACCESS"
resp.table_description.replicas[0].replica_table_class_summary.last_update_date_time #=> Time
resp.table_description.global_table_witnesses #=> Array
resp.table_description.global_table_witnesses[0].region_name #=> String
resp.table_description.global_table_witnesses[0].witness_status #=> String, one of "CREATING", "DELETING", "ACTIVE"
resp.table_description.restore_summary.source_backup_arn #=> String
resp.table_description.restore_summary.source_table_arn #=> String
resp.table_description.restore_summary.restore_date_time #=> Time
resp.table_description.restore_summary.restore_in_progress #=> Boolean
resp.table_description.sse_description.status #=> String, one of "ENABLING", "ENABLED", "DISABLING", "DISABLED", "UPDATING"
resp.table_description.sse_description.sse_type #=> String, one of "AES256", "KMS"
resp.table_description.sse_description.kms_master_key_arn #=> String
resp.table_description.sse_description.inaccessible_encryption_date_time #=> Time
resp.table_description.archival_summary.archival_date_time #=> Time
resp.table_description.archival_summary.archival_reason #=> String
resp.table_description.archival_summary.archival_backup_arn #=> String
resp.table_description.table_class_summary.table_class #=> String, one of "STANDARD", "STANDARD_INFREQUENT_ACCESS"
resp.table_description.table_class_summary.last_update_date_time #=> Time
resp.table_description.deletion_protection_enabled #=> Boolean
resp.table_description.on_demand_throughput.max_read_request_units #=> Integer
resp.table_description.on_demand_throughput.max_write_request_units #=> Integer
resp.table_description.warm_throughput.read_units_per_second #=> Integer
resp.table_description.warm_throughput.write_units_per_second #=> Integer
resp.table_description.warm_throughput.status #=> String, one of "CREATING", "UPDATING", "DELETING", "ACTIVE", "INACCESSIBLE_ENCRYPTION_CREDENTIALS", "ARCHIVING", "ARCHIVED", "REPLICATION_NOT_AUTHORIZED"
resp.table_description.multi_region_consistency #=> String, one of "EVENTUAL", "STRONG"
Parameters:
-
params
(Hash)
(defaults to: {})
—
({})
Options Hash (params):
-
:attribute_definitions
(required, Array<Types::AttributeDefinition>)
—
An array of attributes that describe the key schema for the table and indexes.
-
:table_name
(required, String)
—
The name of the table to create. You can also provide the Amazon Resource Name (ARN) of the table in this parameter.
-
:key_schema
(required, Array<Types::KeySchemaElement>)
—
Specifies the attributes that make up the primary key for a table or an index. The attributes in ‘KeySchema` must also be defined in the `AttributeDefinitions` array. For more information, see [Data Model] in the *Amazon DynamoDB Developer Guide*.
Each ‘KeySchemaElement` in the array is composed of:
-
‘AttributeName` - The name of this key attribute.
-
‘KeyType` - The role that the key attribute will assume:
-
‘HASH` - partition key
-
‘RANGE` - sort key
-
<note markdown=“1”> The partition key of an item is also known as its *hash attribute*. The term “hash attribute” derives from the DynamoDB usage of an internal hash function to evenly distribute data items across partitions, based on their partition key values.
The sort key of an item is also known as its *range attribute*. Theterm “range attribute” derives from the way DynamoDB stores items with the same partition key physically close together, in sorted order by the sort key value.
</note>For a simple primary key (partition key), you must provide exactly one element with a ‘KeyType` of `HASH`.
For a composite primary key (partition key and sort key), you must provide exactly two elements, in this order: The first element must have a ‘KeyType` of `HASH`, and the second element must have a `KeyType` of `RANGE`.
For more information, see [Working with Tables] in the *Amazon DynamoDB Developer Guide*.
[1]: docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataModel.html [2]: docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithTables.html#WorkingWithTables.primary.key
-
-
:local_secondary_indexes
(Array<Types::LocalSecondaryIndex>)
—
One or more local secondary indexes (the maximum is 5) to be created on the table. Each index is scoped to a given partition key value. There is a 10 GB size limit per partition key value; otherwise, the size of a local secondary index is unconstrained.
Each local secondary index in the array includes the following:
-
‘IndexName` - The name of the local secondary index. Must be unique only for this table.
-
‘KeySchema` - Specifies the key schema for the local secondary index. The key schema must begin with the same partition key as the table.
-
‘Projection` - Specifies attributes that are copied (projected) from the table into the index. These are in addition to the primary key attributes and index key attributes, which are automatically projected. Each attribute specification is composed of:
-
‘ProjectionType` - One of the following:
-
‘KEYS_ONLY` - Only the index and primary keys are projected into the index.
-
‘INCLUDE` - Only the specified table attributes are projected into the index. The list of projected attributes is in `NonKeyAttributes`.
-
‘ALL` - All of the table attributes are projected into the index.
-
-
‘NonKeyAttributes` - A list of one or more non-key attribute names that are projected into the secondary index. The total count of attributes provided in `NonKeyAttributes`, summed across all of the secondary indexes, must not exceed 100. If you project the same attribute into two different indexes, this counts as two distinct attributes when determining the total. This limit only applies when you specify the ProjectionType of `INCLUDE`. You still can specify the ProjectionType of `ALL` to project all attributes from the source table, even if the table has more than 100 attributes.
-
-
-
:global_secondary_indexes
(Array<Types::GlobalSecondaryIndex>)
—
One or more global secondary indexes (the maximum is 20) to be created on the table. Each global secondary index in the array includes the following:
-
‘IndexName` - The name of the global secondary index. Must be unique only for this table.
-
‘KeySchema` - Specifies the key schema for the global secondary index.
-
‘Projection` - Specifies attributes that are copied (projected) from the table into the index. These are in addition to the primary key attributes and index key attributes, which are automatically projected. Each attribute specification is composed of:
-
‘ProjectionType` - One of the following:
-
‘KEYS_ONLY` - Only the index and primary keys are projected into the index.
-
‘INCLUDE` - Only the specified table attributes are projected into the index. The list of projected attributes is in `NonKeyAttributes`.
-
‘ALL` - All of the table attributes are projected into the index.
-
-
‘NonKeyAttributes` - A list of one or more non-key attribute names that are projected into the secondary index. The total count of attributes provided in `NonKeyAttributes`, summed across all of the secondary indexes, must not exceed 100. If you project the same attribute into two different indexes, this counts as two distinct attributes when determining the total. This limit only applies when you specify the ProjectionType of `INCLUDE`. You still can specify the ProjectionType of `ALL` to project all attributes from the source table, even if the table has more than 100 attributes.
-
-
‘ProvisionedThroughput` - The provisioned throughput settings for the global secondary index, consisting of read and write capacity units.
-
-
:billing_mode
(String)
—
Controls how you are charged for read and write throughput and how you manage capacity. This setting can be changed later.
-
‘PAY_PER_REQUEST` - We recommend using `PAY_PER_REQUEST` for most DynamoDB workloads. `PAY_PER_REQUEST` sets the billing mode to [On-demand capacity mode].
-
‘PROVISIONED` - We recommend using `PROVISIONED` for steady workloads with predictable growth where capacity requirements can be reliably forecasted. `PROVISIONED` sets the billing mode to [Provisioned capacity mode].
[1]: docs.aws.amazon.com/amazondynamodb/latest/developerguide/on-demand-capacity-mode.html [2]: docs.aws.amazon.com/amazondynamodb/latest/developerguide/provisioned-capacity-mode.html
-
-
:provisioned_throughput
(Types::ProvisionedThroughput)
—
Represents the provisioned throughput settings for a specified table or index. The settings can be modified using the ‘UpdateTable` operation.
If you set BillingMode as ‘PROVISIONED`, you must specify this property. If you set BillingMode as `PAY_PER_REQUEST`, you cannot specify this property.
For current minimum and maximum provisioned throughput values, see
- Service, Account, and Table Quotas][1
-
in the *Amazon DynamoDB
Developer Guide*.
[1]: docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html
-
:stream_specification
(Types::StreamSpecification)
—
The settings for DynamoDB Streams on the table. These settings consist of:
-
‘StreamEnabled` - Indicates whether DynamoDB Streams is to be enabled (true) or disabled (false).
-
‘StreamViewType` - When an item in the table is modified, `StreamViewType` determines what information is written to the table’s stream. Valid values for ‘StreamViewType` are:
-
‘KEYS_ONLY` - Only the key attributes of the modified item are written to the stream.
-
‘NEW_IMAGE` - The entire item, as it appears after it was modified, is written to the stream.
-
‘OLD_IMAGE` - The entire item, as it appeared before it was modified, is written to the stream.
-
‘NEW_AND_OLD_IMAGES` - Both the new and the old item images of the item are written to the stream.
-
-
-
:sse_specification
(Types::SSESpecification)
—
Represents the settings used to enable server-side encryption.
-
:tags
(Array<Types::Tag>)
—
A list of key-value pairs to label the table. For more information, see [Tagging for DynamoDB].
[1]: docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tagging.html
-
:table_class
(String)
—
The table class of the new table. Valid values are ‘STANDARD` and `STANDARD_INFREQUENT_ACCESS`.
-
:deletion_protection_enabled
(Boolean)
—
Indicates whether deletion protection is to be enabled (true) or disabled (false) on the table.
-
:warm_throughput
(Types::WarmThroughput)
—
Represents the warm throughput (in read units per second and write units per second) for creating a table.
-
:resource_policy
(String)
—
An Amazon Web Services resource-based policy document in JSON format that will be attached to the table.
When you attach a resource-based policy while creating a table, the policy application is *strongly consistent*.
The maximum size supported for a resource-based policy document is 20 KB. DynamoDB counts whitespaces when calculating the size of a policy against this limit. For a full list of all considerations that apply for resource-based policies, see [Resource-based policy considerations].
<note markdown=“1”> You need to specify the ‘CreateTable` and `PutResourcePolicy` IAM actions for authorizing a user to create a table with a resource-based policy.
</note>[1]: docs.aws.amazon.com/amazondynamodb/latest/developerguide/rbac-considerations.html
-
:on_demand_throughput
(Types::OnDemandThroughput)
—
Sets the maximum number of read and write units for the specified table in on-demand capacity mode. If you use this parameter, you must specify ‘MaxReadRequestUnits`, `MaxWriteRequestUnits`, or both.
Returns:
-
(Types::CreateTableOutput)
—
Returns a response object which responds to the following methods:
-
#table_description => Types::TableDescription
-
See Also:
1892 1893 1894 1895 |
# File 'lib/aws-sdk-dynamodb/client.rb', line 1892 def create_table(params = {}, options = {}) req = build_request(:create_table, params) req.send_request(options) end |