Search⌘ K
AI Features

Demo: Storing and Retrieving Information

Explore how to store and retrieve information in DynamoDB by creating records with Golang code and searching data via keys. Learn to configure credentials, execute commands using AWS CLI, and handle querying for specific and filtered records to manage your DynamoDB tables effectively.

We'll cover the following...

Create a record

Let’s create a record by using the following Golang piece of code. It is important to have an access-key and a secret-key to do it.

package main

import (
	"fmt"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"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"
)

const (
	Region = "{{Region}}"
	AccessKey = "{{AccessKey}}"
	SecretKey = "{{SecretKey}}"
	Token = ""
	TableName = "demo-interaction"
)

func main() {
	svc := CreateSession()
	CreateTableOnDemand(TableName,svc)
	CreateRecord(TableName,svc)
}

// CreateSession creates a session to invoke AWS APIs
func CreateSession() dynamodbiface.DynamoDBAPI{
	sess, err := session.NewSession(&aws.Config{
		Region:      aws.String(Region),
		// This is for practice purposes, NEVER hardcode credentials, AWS provides different mechanisms to authenticate SDK
		Credentials: credentials.NewStaticCredentials(AccessKey, SecretKey, Token),
	})

	if err != nil{
		fmt.Println(err)
	}

	var svc dynamodbiface.DynamoDBAPI
	svc = dynamodb.New(sess)

	return svc
}

func CreateTableOnDemand(tableName string, svc dynamodbiface.DynamoDBAPI) {
	fmt.Println("CreateTableOnDemand starts")

	// It means OnDemand
	billingMode := "PAY_PER_REQUEST"

	params := &dynamodb.CreateTableInput{
		TableName: aws.String(tableName),

		// Definition of attributes that the table is going to have
		AttributeDefinitions: []*dynamodb.AttributeDefinition{
			{
				AttributeName: aws.String("partitionkey"),
				AttributeType: aws.String("S"),
			},
			{
				AttributeName: aws.String("sortkey"),
				AttributeType: aws.String("S"),
			},
		},

		// Definition of keys of the table, HASH is for primary key and RANGE is for sort key
		KeySchema: []*dynamodb.KeySchemaElement{
			{
				AttributeName: aws.String("partitionkey"),
				KeyType:       aws.String("HASH"),
			},
			{
				AttributeName: aws.String("sortkey"),
				KeyType:       aws.String("RANGE"),
			},
		},
		BillingMode: &billingMode,
	}

	output, err := svc.CreateTable(params)
	if err != nil {
		fmt.Println(err)
	}

	description := dynamodb.DescribeTableInput{
		TableName: aws.String(tableName),
	}
	svc.WaitUntilTableExists(&description)

	fmt.Printf("CREATING:\n Table ARN: %s \n, Table name: %s \n",*output.TableDescription.TableArn, *output.TableDescription.TableName)
}

// CreateRecord allows to create a record in the table
func CreateRecord(tableName string, svc dynamodbiface.DynamoDBAPI){
	input := &dynamodb.PutItemInput{
		TableName: aws.String(tableName),
		Item: map[string]*dynamodb.AttributeValue{
			"partitionkey":{
				S: aws.String("test"),
			},
			"sortkey":{
				S: aws.String("2022"),
			},
			"active":{
				BOOL: aws.Bool(false),
			},
		},
	}

	_,err := svc.PutItem(input)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("Result created successfully")
}
Record creation Golang

Search for records

The ...