_This issue was originally opened by @jgryszko as hashicorp/terraform#12229. It was migrated here as part of the provider split. The original body of the issue is below._
Hi,
It looks like multiple attributes are not supported for dynamodb key schema so far.
Looking at the source, only hash_key is included in the key schema.
resource_aws_dynamodb_table.go:
hash_key_name := d.Get("hash_key").(string)
keyschema := []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String(hash_key_name),
KeyType: aws.String("HASH"),
},
}
Are there plans to change this in near future?
Best,
Janusz
Why should we declare schema in terraform? Dynamodb is schemaless, it doesn't matter.
@thiagonache, It's not about general schema. The key schema defines, which attributes are part of the primary key.
A primary key can either be a single-attribute partition key or a composite partition-sort key. A single attribute partition primary key could be, for example, “UserID”. This would allow you to quickly read and write data for an item associated with a given user ID.
See https://aws.amazon.com/dynamodb/faqs/
gotcha... combination of range key + hash key. I haven't notice that....
resource "aws_dynamodb_table" "mytable" {
name = "mytable"
read_capacity = 5
write_capacity = 5
hash_key = "Id"
range_key = "Timestamp"
attribute {
name = "Id"
type = "S"
}
attribute {
name = "Timestamp"
type = "S"
}
tags {
Name = "mytable"
Environment = "dev"
Terraform = "yes"
}
}
does it not work for you?
Hi @jgryszko
Were you able to replicate @thiagonache 's configuration and confirm whether it works?
Would like to close this one since it seems like a non-bug to me.
Thanks! :)
@Ninir I think you can have multiple range keys in a schema, but range_key is for a single value.
while correct, it isn't required to define the scheme, the option should exist and the documentation suggests that it is possible
I still see this issue while using Terraform v0.11.8
Likewise, with the following configuration
resource "aws_dynamodb_table" "dynamodb_table" {
name = "Asset_DB_TF"
hash_key = "Product"
read_capacity = 1
write_capacity = 1
server_side_encryption {
enabled = true
}
attribute = [{
name = "Product"
type = "S"
}, {
name = "Contact"
type = "S"
}, {
name = "Deployed"
type = "S"
}, {
name = "Resource ID"
type = "S"
}, {
name = "Version"
type = "S"
}]
}
I'm getting
aws_dynamodb_table.dynamodb_table: All attributes must be indexed. Unused attributes: ["Contact" "Resource ID" "Deployed" "Version"]
Hey folks,
Circling back to that one, let me offer a few answers:
I think you can have multiple range keys in a schema, but range_key is for a single value.
@visit1985 the primary key is either a hash key _or_ a hash key + sort key. It is the only thing for the _default_ index.
However, if you add another index, you can add another primary key.
@MarkVLK regarding your issue, this is expected: the fields that are not included in the primary key can't be set as non-key schema fields. Look at the example from the Terraform documentation: there are 3 attributes defined, 2 are for the main primary key, the last one is for the global index.
@jgryszko the code I see as of today is including both the hash and range keys.
I will now close this issue. Please comment if you think there still is an issue.
I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.
If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!
Most helpful comment
Likewise, with the following configuration
I'm getting