Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Add-VSDynamoDBTableAttributeDefinition? #27

Closed
RegEM opened this issue Mar 29, 2018 · 7 comments
Closed

Add-VSDynamoDBTableAttributeDefinition? #27

RegEM opened this issue Mar 29, 2018 · 7 comments
Assignees
Labels

Comments

@RegEM
Copy link

RegEM commented Mar 29, 2018

Hi Nate,
I'm hoping you have a minute to help me with creating some attributes for a dynamodb table? I am struggling with how to do it.
I want these properties for my table:
AttributeDefinitions: - AttributeName: key AttributeType: S - AttributeName: eventTime AttributeType: S KeySchema: - AttributeName: key KeyType: HASH - AttributeName: eventTime KeyType: RANGE ProvisionedThroughput: ReadCapacityUnits: 1 WriteCapacityUnits: 1

I thought I could do this, but it doesn't seem to work when I vaporize

        -AttributeDefinitions ( Add-VSDynamoDBTableAttributeDefinition `
                                   -AttributeName key,eventTime -AttributeType S,S ) `
        -KeySchema ( Add-VSDynamoDBTableKeySchema `
                        -AttributeName key,eventTime -KeyType HASH,RANGE ) `
        -ProvisionedThroughput 1,1 

Cheers, Richard

@scrthq
Copy link
Member

scrthq commented Mar 29, 2018

Hey Richard,

The way to think about it is that a DynamoDB Table AttributeDefinition is a single object with two properties; AttributeName and AttributeType. In PowerShell, you would need to create 2 AttributeDefinition objects for this to match, which means a call to Add-VSDynamoDBTableAttributeDefinition for each attribute. Same concept for the KeySchemas. ProvisionedThroughput is also breaking in your call as you have it separated with a comma, which would pass an array of integers in your case instead of using positional parameters.

Here are a couple ways you can accomplish this to create the DynamoDBTable you're looking for:

Option 1 (cleaner view): Store AttributeDefinitions and KeySchemas in variables

$dyDbAttDefs = @()
$dyDbKeySchemas = @()
$dyDbAttDefs += @("key","eventTime") | ForEach-Object {Add-VSDynamoDBTableAttributeDefinition -AttributeName $_ -AttributeType S}
$dyDbKeySchemas += Add-VSDynamoDBTableKeySchema -AttributeName key -KeyType HASH
$dyDbKeySchemas += Add-VSDynamoDBTableKeySchema -AttributeName eventTime -KeyType RANGE
$newVSDynamoDBTableSplat = @{
    ProvisionedThroughput = (Add-VSDynamoDBTableProvisionedThroughput -ReadCapacityUnits 1 -WriteCapacityUnits 1)
    KeySchema             = $dyDbKeySchemas
    AttributeDefinitions  = $dyDbAttDefs
    LogicalId             = "MyDynamoDB"
}
New-VSDynamoDBTable @newVSDynamoDBTableSplat

Option 2: One-liner

New-VSDynamoDBTable -LogicalId "MyDynamoDB" -AttributeDefinitions (Add-VSDynamoDBTableAttributeDefinition -AttributeName key -AttributeType S),(Add-VSDynamoDBTableAttributeDefinition -AttributeName eventTime -AttributeType S) -KeySchema (Add-VSDynamoDBTableKeySchema -AttributeName key -KeyType HASH),(Add-VSDynamoDBTableKeySchema -AttributeName eventTime -KeyType RANGE) -ProvisionedThroughput (Add-VSDynamoDBTableProvisionedThroughput -ReadCapacityUnits 1 -WriteCapacityUnits 1)

Another thing to keep in mind is that any attributes where CloudFormation itself is expecting a specific type (i.e. Dynamo Table AttributeDefinition) will have a VaporShell function as Add-VS* to create that specific object type. The types are hardcoded in VaporShell, so if you see an error such as the following, know that you're likely passing in a literal value when the functions are expecting a VaporShell object of some sort:

New-VSDynamoDBTable : This parameter only accepts the following types: Vaporshell.Resource.DynamoDB.Table.KeySchema. The current types of the value are: System.String, System.Object.
At line:1 char:1
+ New-VSDynamoDBTable -LogicalId "MyDynamoDB" -AttributeDefinitions (Ad ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-VSDynamoDBTable], Exception
    + FullyQualifiedErrorId : New-VSDynamoDBTable

@scrthq scrthq self-assigned this Mar 29, 2018
@RegEM
Copy link
Author

RegEM commented Mar 29, 2018

Never mind Nate. I think my issues are partially related to having extra spaces in my editor, and then my script failing between parameters.
To resolve the issue, I stored the attributes in their own variables then entered like this:
-AttributeDefinitions ($attr1, $attr2)
but then it was failing on the extra spaces... ie it would not get to the next parameter.
Cheers. Terrific software.

@RegEM RegEM closed this as completed Mar 29, 2018
@RegEM
Copy link
Author

RegEM commented Mar 29, 2018

Thanks Nate. I missed your reply. Will read it now 👍

@RegEM
Copy link
Author

RegEM commented Mar 29, 2018

Thanks again. I was trying to accomplish the one liner method. I appreciate your taking the time to show me the splatting method too.

@scrthq
Copy link
Member

scrthq commented Mar 29, 2018

I'm glad you're liking VaporShell!! Happy to help whenever it's needed!

Not sure if you have pre-existing CloudFormation templates spun up prior to using VaporShell, but I'm currently working on adding functionality to reverse-engineer an existing CloudFormation template into a VaporShell script that would create it. Should hopefully have it released with the next version pretty soon!

@RegEM
Copy link
Author

RegEM commented Mar 29, 2018

currently working on adding functionality to reverse-engineer an existing CloudFormation template into a VaporShell script that would create it

That sounds great.

I am trying to bring in my templates as I go. I only have a few. I'm finding it easiest to look at the original serverless yml files, and then try to replicate into my vaporshell script. At the same time, learning git, and reorganizing my development environment, so plenty on board.

I also was looking recently at sam local (and docker), but I think I will just concentrate on scripts to build & teardown on aws as needed.

@scrthq
Copy link
Member

scrthq commented Mar 29, 2018

Gotcha, that makes sense!

If you'd like to follow along with the progress of that add'l functionality, I've created Issue #28 to track that. Feel free to subscribe to updates there to follow along if you'd like!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants