Daily Archives: January 20, 2015

Using Amazon AWS DynamoDb from Powershell

Standard

Serious Cat -   Update Dynamodb in Powershell? You cant be serious

Dynamo Db can be very useful in AWS scripting for persisting variable values.

It seems the most common way of doing this is to use the “aws” cli tool.  For Powershell users there isn’t an equivalent commandlet, so I see alot of examples calling the aws commandline tool from within Powershell.

For Powershell people there must be a better way, and that way is to call the AWS SDK directly!

Here’s an example that updates a simple table with a “Name” (key) and “Value” columns.  I’ve encapsulated this in a pair of functions of utility.

Hope you find this useful.

function UpdateVar
{
Param([string]$secretKey,
[string]$secretAccessKey,
[string]$regionName,
[string]$name,
[string]$value)

Add-Type -Path (${env:ProgramFiles(x86)} + “\AWS SDK for .NET\bin\net45\AWSSDK.dll”)

$regionEndpoint=[Amazon.RegionEndPoint]::GetBySystemName($regionName)
$client=[Amazon.AWSClientFactory]::CreateAmazonDynamoDBClient($secretKey,$secretAccessKey,$regionEndpoint)

$req = New-Object Amazon.DynamoDBv2.Model.UpdateItemRequest
$req.TableName = “mytable”

# Key
$req.Key = New-Object ‘system.collections.generic.dictionary[string,Amazon.DynamoDBv2.Model.AttributeValue]’
$keyattribute = New-Object Amazon.DynamoDBv2.Model.AttributeValue
$keyattribute.S = $name
$req.Key.Add(“Name”, $keyattribute)

# Expression Attribute Names
$req.ExpressionAttributeNames = New-Object ‘system.collections.generic.dictionary[string,string]’
$req.ExpressionAttributeNames.Add(“#V”,”Value”)
# Expression Attribute Values

$valueattribute = New-Object Amazon.DynamoDBv2.Model.AttributeValue
$valueattribute.S = $value
$req.ExpressionAttributeValues = New-Object ‘system.collections.generic.dictionary[string,Amazon.DynamoDBv2.Model.AttributeValue]’
$req.ExpressionAttributeValues.Add(“:value”,$valueattribute)
$req.UpdateExpression = “SET #V = :value”

$client.UpdateItem($req)
return $true
}

function GetVar
{
Param([string]$secretKey,
[string]$secretAccessKey,
[string]$regionName,
[string]$name)

Add-Type -Path (${env:ProgramFiles(x86)} + “\AWS SDK for .NET\bin\net45\AWSSDK.dll”)

$regionEndpoint=[Amazon.RegionEndPoint]::GetBySystemName($regionName)
$client=[Amazon.AWSClientFactory]::CreateAmazonDynamoDBClient($secretKey,$secretAccessKey,$regionEndpoint)

$req = New-Object Amazon.DynamoDBv2.Model.GetItemRequest
$req.TableName = “mytable”
$req.Key = New-Object ‘system.collections.generic.dictionary[string,Amazon.DynamoDBv2.Model.AttributeValue]’

$keyattribute = New-Object Amazon.DynamoDBv2.Model.AttributeValue
$keyattribute.S = $name
$req.Key.Add(“Name”, $keyattribute)

$resp = $client.GetItem($req)

$res = $resp.GetItemResult
return $res.Item[“Value”].S

}

$secretKeyID=”xxxxxxxxxxxxxxxxx”
$secretAccessKeyID=”yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy”
$region=”ap-southeast-2″

UpdateVar $secretKeyID $secretAccessKeyID $region “SomeKey” “somevalue”

$val = GetVar $secretKeyID $secretAccessKeyID $region “SomeKey”

Advertisements