Using Amazon DynamoDB tables to store global data - Amazon GameSparks

Amazon GameSparks is currently in preview. Changes might be made to this service and to this documentation. We don’t recommend using this service for production workloads.

Using Amazon DynamoDB tables to store global data

Amazon DynamoDB tables are useful for storing global game data such as level maps, catalogs, and so on. This topic uses sample operations to show you how to work with DynamoDB tables to store your game data.

Create a table

Create a DynamoDB table named Weapon-Table with a partition key named Name.

  1. At the top of the page, choose Services.

  2. Search for or choose DynamoDB under Recently visited.

  3. In the navigation pane choose Tables.

  4. Choose Create table.

  5. For Table name enter Weapon-Table.

  6. For Partition key enter Name.

  7. Choose Create table.

  8. On the Tables page, wait for the message that the table was created.

Add sample items to the table

Add two entries to the table to represent two different types of weapons.

  1. Select the checkbox next to Weapon-Table, choose Actions, and then choose Explore items.

  2. In Items > Weapon-Table, create the following item.

    Name (String)

    Type (String)

    Damage (Number)

    Magic (Boolean)





  3. On the Explore items page, select the checkbox next to Broadsword, choose Actions, and then choose Duplicate item.

  4. Update the item information to create the following item.

    Name (String)

    Type (String)

    Damage (Number)

    Magic (Boolean)

    Magic mace




Get the table ARN

To give Amazon GameSparks access to the table, get its Amazon Resource Name (ARN).

  1. In the DynamoDB, from the Tables page, choose Weapon-Table.

  2. In the General information panel, expand Additional info.

  3. Copy the table’s ARN and save it for the next section.

An Amazon Resource Name (ARN) is a string that uniquely identifies a resource such as a game, a stage, a Lambda function, or a DynamoDB table. For more information, see Amazon Resource Names (ARNs) in the AWS General Reference.

Allow your Dev stage to access the table

Before your GameSparks game can access your DynamoDB table, you must provide access using AWS Identity and Access Management (IAM) policies. Here you’ll create two policies: one to read from the table and one to write to the table.

  1. Go to the IAM Create policy page, and then choose the JSON tab.

  2. Replace the existing JSON policy with the following policy.

    { "Version": "2012-10-17", "Statement": [ { "Action": [ "dynamodb:GetItem", "dynamodb:Query" ], "Resource": "arn:aws:dynamodb:us-east-1:012345678912:table/Weapon-Table", "Effect": "Allow" } ] }

    For Resource replace the value with the function ARN that you copied in the previous section.

  3. Choose Next: Tags, and then Next: Review.

  4. On the Review policy page, do the following:

    1. For Name, enter ReadWeaponTable.

    2. Choose Create policy.

  5. Repeat the previous process to create the following policy.

    { "Version": "2012-10-17", "Statement": [ { "Action": [ "dynamodb:DeleteItem", "dynamodb:PutItem", "dynamodb:UpdateItem" ], "Resource": "arn:aws:dynamodb:us-east-1:012345678912:table/Weapon-Table", "Effect": "Allow" } ] }

    For Resource replace the value with the function ARN that you copied in the previous section.

  6. Choose Next: Tags, and then Next: Review.

  7. On the Review policy page, do the following:

    1. For Name, enter WriteWeaponTable.

    2. Choose Create policy.

  8. In the GameSparks console, go to your game.

  9. In the navigation pane, choose Dev.

  10. In the Dev stage configuration panel, choose View in IAM console.

    The IAM console displays the IAM role for your Dev stage.

  11. On the Permissions tab, choose Add permissions, Attach policies.

  12. Under Other permissions policies, search for the ReadWeaponTable policy.

  13. In the search results, select the policy, and then choose Attach policies.

  14. Repeat the previous two steps for the WriteWeaponTable policy.

Use messages to manage your items

Next you’ll create cloud code messages that manage a catalog of weapons. You’ll use DynamoDB().PutItem to add to the catalog, and DynamoDB().UpdateItem to correct a weapon, and DynamoDB().GetItem() to retrieve a weapon.

Create an event

Create an event, enable client permission, and create an event handler with the code below.

  1. From the GameSparks navigation pane, select Cloud code.

  2. Create a new event named Weapons.

  3. Select Enable client permission.

  4. Add an event handler named WeaponsHandler, and then add the following sample code.

GameSparks().Logging().Debug("Inside Weapons handler"); // add a magic blade weapon GameSparks().DynamoDB().PutItem("Weapon-Table", { "Name": "Snickersnee", "Type": "Blade", "Damage": 21, "Magic": true }); let blade = GameSparks().DynamoDB().GetItem("Weapon-Table", { "Name": "Snickersnee" }); GameSparks().Logging().Debug("New sword: " + JSON.stringify(blade)); // add a club with some problems (you'll fix them below) GameSparks().DynamoDB().PutItem("Weapon-Table", { "Name": "Club", "Type": "Blade", "Damage": 6, "Magic": false, "NoNeed": "Delete me" }) let club = GameSparks().DynamoDB().GetItem("Weapon-Table", { "Name": "Club" }); GameSparks().Logging().Debug("New club (needs fixing): " + JSON.stringify(club)); // fix the club: remove NoNeed field, change Type to "Blunt" // "Type" is a DynamoDB reserved keyword, so you define a DynamoDB substitution token #T // :val is a DynamoDB attribute value GameSparks().DynamoDB().UpdateItem("Weapon-Table", { "Name": "Club" }, "SET #T = :val REMOVE NoNeed", { "#T":"Type" }, { ":val": "Blunt" }); club = GameSparks().DynamoDB().GetItem("Weapon-Table", { "Name": "Club" }); GameSparks().Logging().Debug("club fixed: " + JSON.stringify(club));
  1. Choose Save.

Deploy and test your messages

Deploy your messages:

  1. In the GameSparks navigation pane, choose Dev.

  2. Choose Deploy as new snapshot, enter First snapshot for the description, and then choose Save.

To test your event:

  1. In the Messages pane, choose the event.

  2. Choose Test.

    The Test harness appears.

  3. Choose the event.

  4. Choose Populate example.

  5. Choose Send message.

Learn more

To learn more about DynamoDB permissions, see Amazon DynamoDB: Allows access to a specific table.