# DynamoDB 서비스

Amazon Q Developer를 사용해 DynamoDB 서비스를 위한 코드를 생성합니다.

***

## 예시 #1

### 테이블 생성

#### 일반적인 프롬프트

```bash
Create a function to create a dynamodb table with the following parameters:
- Table name
- PartitionKey Name = 'id'
- Sort key Name = 'name'
```

#### Python 프롬프트

VSCode에서 파일을 생성하고 이름을 `dynamodb.py`로 지정하고 다음 코드를 입력합니다.

```bash
"""
Create a function to create a dynamodb table with the following parameters:
- Table name
- PartitionKey Name = 'id'
- Sort key Name = 'name'

"""
```

<details>

<summary>Amazon Q Developer 제안</summary>

```bash
import boto3

def create_table(table_name):
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.create_table(
        TableName=table_name,
        KeySchema=[
            {
                'AttributeName': 'id',
                'KeyType': 'HASH'
            },
            {
                'AttributeName': 'name',
                'KeyType': 'RANGE'
            }
        ],
        AttributeDefinitions=[
            {
                'AttributeName': 'id',
                'AttributeType': 'N'
            },
            {
                'AttributeName': 'name',
                'AttributeType': 'S'
            }
        ],
        ProvisionedThroughput={
            'ReadCapacityUnits': 10,
            'WriteCapacityUnits': 10
        }
    )
    return table

   

```

</details>

***

## 예시 #2

### 레코드 삽입

#### 일반적인 프롬프트

```bash
Given a table_name, id, and a json object with key value pairs
Create a function to insert a record into the DynamoDB table.
The JSON object will have 1 to many key value pairs.
and the json object can be nested and different from the table schema.
create the parameters, attributes names and values from the json object.
if success, return the id.
if failed, return the error.
```

#### Python 프롬프트

```bash
"""
Given a table_name, id, and a json object with key value pairs
Create a function to insert a record into the DynamoDB table.
The JSON object will have 1 to many key value pairs.
and the json object can be nested and different from the table schema.
create the parameters, attributes names and values from the json object.
if success, return the id.
if failed, return the error.
"""
```

<details>

<summary>Amazon Q Developer 제안</summary>

```bash
import boto3
from botocore.exceptions import ClientError

def create_item(table_name, id, item):
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(table_name)
    try:
        response = table.put_item(
            Item={
                'id': id,
                **item
            }
        )
    except ClientError as e:
        return e.response['Error']['Message']
    else:
        return response['Attributes']['id']
    

```

</details>

***

## 예시 #3

### 테이블 업데이트

#### 일반적인 프롬프트

```bash
Given a table_name, id, and a json object with key value pairs
Create a function to update a record into the DynamoDB table.
The JSON object will have many keys and value pairs.
and the json object can be nested and different from the table schema.
use the json object as list of key value pairs to update the table.
if success, return the id.
if failed, return the error.
```

#### Python 프롬프트

```bash
"""
Given a table_name, id, and a json object with key value pairs
Create a function to update a record into the DynamoDB table.
The JSON object will have many keys and value pairs.
and the json object can be nested and different from the table schema.
use the json object as list of key value pairs to update the table.
if success, return the id.
if failed, return the error.
"""
```

<details>

<summary>Amazon Q Developer 제안</summary>

```bash
import boto3
from botocore.exceptions import ClientError

def update_item(table_name, id, item):
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(table_name)
    try:
        response = table.update_item(
            Key={
                'id': id
            },
            UpdateExpression='SET #key = :value',
            ExpressionAttributeValues={
                ':value': item
            },
            ExpressionAttributeNames={
                '#key': 'key'
            }
        )
    except ClientError as e:
        return e.response['Error']['Message']
    else:
        return response['Attributes']['id']
```

</details>

***

## 예시 #4

### Item 삭제

#### 일반적인 프롬프트

```bash
Given a table_name, id, and a json object with key value pairs
Create a function to delete a record into the DynamoDB table.

if success, return the id.
if failed, return the error.
```

#### Python 프롬프트

```bash
"""
Given a table_name, id, and a json object with key value pairs
Create a function to delete a record into the DynamoDB table.

if success, return the id.
if failed, return the error.
"""
```

<details>

<summary>Amazon Q Developer 제안</summary>

```bash
import boto3
from botocore.exceptions import ClientError

def delete_item(table_name, id):
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(table_name)
    try:
        response = table.delete_item(
            Key={
                'id': id
            }
        )
    except ClientError as e:
        return e.response['Error']['Message']
    else:
        return response['Attributes']['id']

   

```

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bangshinchul.gitbook.io/amazon-q/amazon-q-developer/aws/dynamodb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
