

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Step Functions 執行 DynamoDB CRUD 操作
<a name="connect-ddb"></a>

您可以將 Step Functions 與 DynamoDB 整合，以在 DynamoDB 資料表上執行 CRUD 操作。此頁面列出支援的 DynamoDB APIs並提供從 DynamoDB 擷取項目的範例`Task`狀態。

若要了解如何在 Step Functions 中整合 AWS服務，請參閱 [整合 服務](integrate-services.md)和 [在 Step Functions 中將參數傳遞至服務 API](connect-parameters.md)。

**最佳化 DynamoDB 整合的主要功能**  
[請求回應](connect-to-resource.md#connect-default) 整合模式沒有特定的最佳化。
[使用任務字符等待回呼](connect-to-resource.md#connect-wait-token) 不支援 整合模式。
只有 [https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html)、[https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html)、 [https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html)和 [https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html) API 動作可透過最佳化整合來使用。其他 API 動作，例如 [https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html)可使用 DynamoDBAWS SDK 整合。

以下是從 DynamoDB 擷取訊息的範例`Task`狀態。

```
"Read next Message from DynamoDB": {
    "Type": "Task",
    "Resource": "arn:aws:states:::dynamodb:getItem",
    "Arguments": {
        "TableName": "DYNAMO_DB_TABLE_NAME",
        "Key": {
            "MessageId": {"S": "{% $List[0] %}"}
        }
    }
```

若要在工作範例中查看此狀態，請參閱[使用 Lambda、 DynamoDB和 傳輸資料記錄 Amazon SQS](sample-project-transfer-data-sqs.md)入門範本。

**例外狀況字首差異**  
當標準 DynamoDB 連線發生錯誤時，例外狀況字首將會是 `DynamoDb`（混合大小寫）。  
為了最佳化整合，例外狀況字首將為 `DynamoDB`（大寫 `DB`)。

**輸入或結果資料的配額**  
在服務之間傳送或接收資料時，任務的最大輸入或結果為 256 KiB 的資料，做為 UTF-8 編碼字串。請參閱 [狀態機器執行的相關配額](service-quotas.md#service-limits-state-machine-executions)。

## 最佳化 DynamoDB APIs
<a name="connect-dynamodb-api"></a>
+ [https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html)
+ [https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html)
+ [https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html)
+ [https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html)

**中的參數Step Functions以 PascalCase 表示**  
即使原生服務 API 位於 camelCase 中，例如 API 動作 `startSyncExecution`，您也可以在 PascalCase 中指定參數，例如：`StateMachineArn`。

## 呼叫 DynamoDB 的 IAM 政策
<a name="dynamo-iam"></a>

下列範例範本顯示 如何根據狀態機器定義中的資源AWS Step Functions產生 IAM 政策。如需詳細資訊，請參閱[Step Functions 如何為整合服務產生 IAM 政策](service-integration-iam-templates.md)及[探索 Step Functions 中的服務整合模式](connect-to-resource.md)。

*靜態資源*

****  

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

*動態資源*

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:PutItem",
                "dynamodb:UpdateItem",
                "dynamodb:DeleteItem"
            ],
            "Resource": "*"
        }
    ]
}
```

如需所有 DynamoDB API 動作 IAM 政策的詳細資訊，請參閱《Amazon [ DynamoDB 開發人員指南》中的搭配 DynamoDB 的 IAM 政策](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/using-identity-based-policies.html)。 * DynamoDB * 此外，如需適用於 DynamoDB 的 PartiQL IAM 政策的相關資訊，請參閱《Amazon [ DynamoDB 開發人員指南》中的適用於 DynamoDB 的 PartiQL 的 IAM 政策](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-iam.html)。 * DynamoDB *