Amazon DynamoDB Integration Setup

This section provides a step-by-step guide for getting started with Amazon DynamoDB using the AWS Mobile SDK for iOS.

Get the SDK#

To use Amazon DynamoDB in your mobile app, first set up the AWS Mobile SDK for iOS:

  1. Download the iOS SDK and include it in your iOS project, as described in Set Up the SDK for iOS.

  2. Install the SDK

    Add the AWS SDK for iOS to your project and import the APIs you need, by following the steps in Set Up the SDK for iOS.

  3. Configure credentials

    To use Amazon Cognito to create AWS identities and credentials that give your users access to your app's AWS resources, follow the steps in Amazon Cognito for iOS.

For more information on setting up the Amazon Cognito client, see Using Federated Identities in the Amazon Cognito Developer Guide.

Create an Amazon DynamoDB Table and Index#

This tutorial is based on a simple bookstore app. The app tracks the books that are available in the bookstore using an Amazon DynamoDB table.

To create the Books table:

  1. Sign in to the Amazon DynamoDB Console.
  2. Choose Create Table.
  3. Type Books as the name of the table.
  4. Enter ISBN in the Partition key field of the Primary key with String as their type.
  5. Clear the Use default settings checkbox and choose + Add Index.
  6. In the Add Index dialog type Author with String as the type.
  7. Check the Add sort key checkbox and enter Title as the sort key value, with String as its type.
  8. Leave the other values at their defaults. Choose Add index to add the Author-Title-index index.
  9. Set the read capacity to 10 and the write capacity to 5.
  10. Choose Create.Amazon DynamoDB will create your database.
  11. Refresh the console and choose your Books table from the list of tables.
  12. Open the Overview tab and copy or note the Amazon Resource Name (ARN). You need this for the next procedure.

Set Permissions#

To use Amazon DynamoDB in your mobile app, you must set the correct permissions. The following IAM policy allows the user to perform the actions shown in this tutorial on two resources (a table and an index) identified by an ARN.

{
    "Statement": [{
        "Effect": "Allow",
        "Action": [
            "dynamodb:DeleteItem",
            "dynamodb:GetItem",
            "dynamodb:PutItem",
            "dynamodb:Scan",
            "dynamodb:Query",
            "dynamodb:UpdateItem",
            "dynamodb:BatchWriteItem"
        ],
        "Resource": [
            "arn:aws:dynamodb:us-west-2:123456789012:table/Books",
            "arn:aws:dynamodb:us-west-2:123456789012:table/Books/index/*"
        ]
    }]
}

Apply this policy to the unauthenticated role assigned to your Amazon Cognito identity pool, replacing the Resource values with the correct ARN for the Amazon DynamoDB table:

  1. Sign in to the IAM console.
  2. Choose Roles and then choose the "Unauth" role that Amazon Cognito created for you.
  3. Choose Attach Role Policy.
  4. Choose Custom Policy and then Choose Select.
  5. Type a name for your policy and paste in the policy document shown above, replacing the Resource values with the ARNs for your table and index. (You can retrieve the table ARN from the Details tab of the database; then append /index/* to obtain the value for the index ARN.
  6. Choose Apply Policy.

To learn more about IAM policies, see Using IAM. To learn more about creating fine-grained access policies for Amazon DynamoDB, see DynamoDB on Mobile – Part 5: Fine-Grained Access Control.