본문 바로가기

DevOps

AWS DynamoDB 로컬 환경에서 사용 (2)

반응형

AWS DynamoDB 로컬 환경에서 사용 (1) - 로컬 환경 셋팅 참고

이전 포스트에서는 로컬에서 DynamoDB를 사용할 수 있도록 구축하고 AWS CLI를 사용해서 CRUD를 실행했고 이번 포스트에서는 CURL 통신을 활용한 CRUD를 진행 할 것이다.

우선 나의 경우에는 아래와 같은 순서로 진행했다.

  1. Postman으로 cURL 코드를 생성
  2. 생성한 코드를 .sh파일로 실행 (터미널)

그러면 Postman으로 어떻게 했는지 살펴봅시다.

1. Postman 설정


위 캡처에서 확인 할 수 있듯이 핵심은 Headers에서 선언하는 인증 부분과 타겟이다.
X-Amz-Target에 대한 내용은 공식 문서를 확인 해보면 대략 어떻게 입력해야 되는지 확인이 가능하나 Authorization 인증 같은 경우에는 공식 문서를 확인해봐도 애매모호한 부분이 많다... 로컬이라서 그런지 Credential={a}/{b}/{c} 와 같은 형태로 데이터만 넣어주면 진행하는데 문제가 없었다. 실제 DynamoDB를 접근하려면 IAM의 access key로 인증해야되니 이 부분은 로컬환경이라 문제없이 넘어가는 것으로 생각하고 패스하겠다.

Body는 json으로 작성

{
    "AttributeDefinitions": [
        {
            "AttributeName": "id",
            "AttributeType": "S"
        },
        {
            "AttributeName": "mentionId",
            "AttributeType": "N"
        },
        {
            "AttributeName": "createdAt",
            "AttributeType": "S"
        }
    ],
    "TableName": "Comment5",
    "KeySchema": [
        {
            "AttributeName": "id",
            "KeyType": "HASH"
        }
    ],
    "GlobalSecondaryIndexes": [
        {
            "IndexName": "byMentionId",
            "KeySchema": [
                {
                    "AttributeName": "mentionId",
                    "KeyType": "HASH"
                },
                {
                    "AttributeName": "createdAt",
                    "KeyType": "RANGE"
                }
            ],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 1,
                "WriteCapacityUnits": 1
            }
        }
    ],
    "ProvisionedThroughput": {
        "ReadCapacityUnits": 1,
        "WriteCapacityUnits": 1
    }
}

작성을 다하고 우측의 코드를 열어보면 완성된 cURL 코드를 확인 할 수 있다.

2. 쉘 스크립트 작성 및 실행

위에서 생성한 코드를 복사하고 sh파일로 만들어주면 된다. 쉘 스크립트이니 #!/bin/bash를 선언해준다.

  1. Table 생성
#!/bin/bash

curl --location --request POST 'http://localhost:8000' \
--header 'Authorization: AWS4-HMAC-SHA256 Credential=key1/20190526/ap-northeast-2' \
--header 'X-Amz-Target: DynamoDB_20120810.CreateTable' \
--header 'Content-Type: application/json' \
--data-raw '{
    "TableName": "Comment",
    "AttributeDefinitions": [
        {
            "AttributeName": "id",
            "AttributeType": "S"
        },
        {
            "AttributeName": "mentionId",
            "AttributeType": "N"
        },
        {
            "AttributeName": "createdAt",
            "AttributeType": "S"
        }
    ],
    "KeySchema": [
        {
            "AttributeName": "id",
            "KeyType": "HASH"
        }
    ],
    "GlobalSecondaryIndexes": [
        {
            "IndexName": "byMentionId",
            "KeySchema": [
                {
                    "AttributeName": "mentionId",
                    "KeyType": "HASH"
                },
                {
                    "AttributeName": "createdAt",
                    "KeyType": "RANGE"
                }
            ],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 1,
                "WriteCapacityUnits": 1
            }
        }
    ],
    "ProvisionedThroughput": {
        "ReadCapacityUnits": 1,
        "WriteCapacityUnits": 1
    }
}'
  1. Item 생성 & 수정
#!/bin/bash

curl --location --request POST 'http://localhost:8000' \
--header 'Authorization: AWS4-HMAC-SHA256 Credential=key1/20190526/ap-northeast-2' \
--header 'X-Amz-Target: DynamoDB_20120810.PutItem' \
--header 'Content-Type: application/json' \
--data-raw '
    {
        "TableName": "Comment",
        "Item": {
            "id": {"S": "1"},
            "name": {"S": "testName"},
            "mentionId": {"N": "1"},
            "content": {"S": "testContent"},
            "createdAt": {"S": "2022-03-24"}
        }
    }'
  1. Item 조회
#!/bin/bash

curl --location --request POST 'http://localhost:8000' \
--header 'Authorization: AWS4-HMAC-SHA256 Credential=key1/20190526/ap-northeast-2' \
--header 'X-Amz-Target: DynamoDB_20120810.GetItem' \
--header 'Content-Type: application/json' \
--data-raw '
    {
        "TableName": "Comment",
        "Key": {
            "id": {"S": "1"}
        }
    }'
  1. Item 삭제
#!/bin/bash

curl --location --request POST 'http://localhost:8000' \
--header 'Authorization: AWS4-HMAC-SHA256 Credential=key1/20190526/ap-northeast-2' \
--header 'X-Amz-Target: DynamoDB_20120810.DeleteItem' \
--header 'Content-Type: application/json' \
--data-raw '
    {
        "TableName": "Comment",
        "Key": {
            "id": {"S": "1"}
        }
    }'
  1. Table 삭제
#!/bin/bash

curl --location --request POST 'http://localhost:8000' \
--header 'Authorization: AWS4-HMAC-SHA256 Credential=key1/20190526/ap-northeast-2' \
--header 'X-Amz-Target: DynamoDB_20120810.DeleteTable' \
--header 'Content-Type: application/json' \
--data-raw '
    {
        "TableName": "Comment"
    }'

cURL을 통해서 DynamoDB를 접근하는것은 위 내용으로 마무리 한다. 다음은 Spring boot에서 사용하는 부분에 대해서 정리해보겠다.

반응형

'DevOps' 카테고리의 다른 글

게이트웨이 (Gateway)란?  (0) 2022.10.25
MSA 개념 정리  (0) 2022.10.25
AWS 주요 서비스 정리  (0) 2022.10.25
AWS DynamoDB 로컬 환경에서 사용 (1)  (0) 2022.10.25
AWS EC2 CentOS 서버 용량 증설  (0) 2022.10.25