CreateTable.go - AWS Code Sample

CreateTable.go

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 package main import ( "flag" "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface" ) // MakeTable creates an Amazon DynamoDB table // Inputs: // sess is the current session, which provides configuration for the SDK's service clients // attributeDefinitions describe the table's attributes // keySchema defines the table schema // provisionedThroughput defines the throughput // tableName is the name of the table // Output: // If success, nil // Otherwise, an error from the call to CreateTable func MakeTable(svc dynamodbiface.DynamoDBAPI, attributeDefinitions []*dynamodb.AttributeDefinition, keySchema []*dynamodb.KeySchemaElement, provisionedThroughput *dynamodb.ProvisionedThroughput, tableName *string) error { _, err := svc.CreateTable(&dynamodb.CreateTableInput{ AttributeDefinitions: attributeDefinitions, KeySchema: keySchema, ProvisionedThroughput: provisionedThroughput, TableName: tableName, }) return err } func main() { tableName := flag.String("t", "", "The name of the table") flag.Parse() if *tableName == "" { fmt.Println("You must supply a table name (-t TABLE)") return } sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := dynamodb.New(sess) attributeDefinitions := []*dynamodb.AttributeDefinition{ { AttributeName: aws.String("Year"), AttributeType: aws.String("N"), }, { AttributeName: aws.String("Title"), AttributeType: aws.String("S"), }, } keySchema := []*dynamodb.KeySchemaElement{ { AttributeName: aws.String("Year"), KeyType: aws.String("HASH"), }, { AttributeName: aws.String("Title"), KeyType: aws.String("RANGE"), }, } provisionedThroughput := &dynamodb.ProvisionedThroughput{ ReadCapacityUnits: aws.Int64(10), WriteCapacityUnits: aws.Int64(10), } err := MakeTable(svc, attributeDefinitions, keySchema, provisionedThroughput, tableName) if err != nil { fmt.Println("Got error calling CreateTable:") fmt.Println(err) return } fmt.Println("Created the table", *tableName) }