Hi Channel!
I don’t know if this is the right place as my question is related to AWS dynamodb with the V2 enhanced client (of course in Spring Boot + Kotlin). I’m following
this blog post. I keep getting this exception from the dynamodb SDK when I try to write to dynamo -
java.lang.IllegalArgumentException: Attempt to execute an operation that requires a primary index without defining any primary key attributes in the table metadata.
. I define partition key in the data class. But it keeps complaining that I don’t have it!. Here’s the entire code:
package com.redacted.backend.monolith.app.controllers
import com.redacted.backend.monolith.config.dynamodb.createDynamoDbClient
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.graphql.data.method.annotation.Argument
import org.springframework.graphql.data.method.annotation.MutationMapping
import org.springframework.stereotype.Controller
import org.springframework.stereotype.Repository
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient
import software.amazon.awssdk.enhanced.dynamodb.TableSchema
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey
@Controller
class DogController(private val repo: DogRepository) {
@MutationMapping
fun createDog(@Argument dog: Dog): String {
return repo.create(dog)
}
}
@DynamoDbBean
data class Dog(
@get:DynamoDbPartitionKey
val dogId: String? = null,
@get:DynamoDbSortKey
val rating: Int = 0,
val name: String? = null
)
@Configuration
class DynamoClient(
@Value("aws.dynamodb.access-key") private val dynamoDbAccessKey: String,
@Value("aws.dynamodb.secret-key") private val dynamoDbSecretKey: String,
@Value("aws.dynamodb.region") private val dynamoDbRegion: String
) {
@Bean
fun dynamoDbClient(): DynamoDbEnhancedClient = createDynamoDbClient(
accessKey = dynamoDbAccessKey,
secretKey = dynamoDbSecretKey,
region = dynamoDbRegion
)
}
@Repository
class DogRepository(private var client: DynamoDbEnhancedClient) {
private val table = client.table("dog_table", TableSchema.fromBean(Dog::class.java))
fun create(dog: Dog): String {
table.putItem(dog)
println("Written the dog to db successfully!")
return "Written"
}
}
Here’s the whole error: Any help would be very much appreciated!
Error message is in the 🧵