0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【AWS】API Gateway → Lambda → DynamoDB ハンズオン

Posted at

概要

・API Gateway、Lambda、DynamoDBの構築

・API Gateway → Lambda → DynamoDB間でHTTP通信のテスト(テーブル作成)

構成図

image.png

ハンズオン参考資料

作業手順

  1. IAMポリシーの作成(Lambda用)
  2. IAMロールの作成(Lambda用)
  3. Lambda関数作成
  4. API Gatewayの作成(REST)
  5. DynamoDBの作成
  6. API Gatewayからテーブル作成のテスト実行

IAMポリシーの作成(Lambda用)

・ハンズオン資料の「許可ポリシーを作成する」を参照
 下記のカスタムポリシーを使用(DynamoDB及びCloudWatchLogへの許可権限)

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1428341300017",
      "Action": [
        "dynamodb:DeleteItem",
        "dynamodb:GetItem",
        "dynamodb:PutItem",
        "dynamodb:Query",
        "dynamodb:Scan",
        "dynamodb:UpdateItem"
      ],
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Sid": "",
      "Resource": "*",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Effect": "Allow"
    }
  ]
}

作成したIAMポリシー
image.png

IAMロールの作成(Lambda用)

・ハンズオン資料の「実行ロールを作成する」を参照
作成したIAMロール
image.png

Lambda関数作成

・ハンズオン資料の「関数を作成する」を参照
作成したLambda関数
image.png

Pythonコードには、こちらを張り付ける。

import boto3

# Define the DynamoDB table that Lambda will connect to
table_name = "lambda-apigateway"

# Create the DynamoDB resource
dynamo = boto3.resource('dynamodb').Table(table_name)

# Define some functions to perform the CRUD operations
def create(payload):
    return dynamo.put_item(Item=payload['Item'])

def read(payload):
    return dynamo.get_item(Key=payload['Key'])

def update(payload):
    return dynamo.update_item(**{k: payload[k] for k in ['Key', 'UpdateExpression', 
    'ExpressionAttributeNames', 'ExpressionAttributeValues'] if k in payload})

def delete(payload):
    return dynamo.delete_item(Key=payload['Key'])

def echo(payload):
    return payload

operations = {
    'create': create,
    'read': read,
    'update': update,
    'delete': delete,
    'echo': echo,
}

def lambda_handler(event, context):
    '''Provide an event that contains the following keys:
      - operation: one of the operations in the operations dict below
      - payload: a JSON object containing parameters to pass to the 
        operation being performed
    '''
    
    operation = event['operation']
    payload = event['payload']
    
    if operation in operations:
        return operations[operation](payload)
        
    else:
        raise ValueError(f'Unrecognized operation "{operation}"')

API Gatewayの作成(REST)

・ハンズオン資料の「API Gateway を使用して REST API を作成する」を参照
作成したAPI Gateway(API名:DynamoDBOperations、リソース名:DynamoDBManager、メソッド:HTTP POST)

image.png

DynamoDBの作成

・ハンズオン資料の「DynamoDB テーブルを作成する」を参照
作成したDynamoDBテーブル
image.png

API Gatewayからテーブル作成のテスト実行

・ハンズオン資料の「API Gateway、Lambda、および DynamoDB の統合をテストする」を参照
API Gatewayの画面からテストコードを入力(create)
image.png

テスト実行、ステータスコード200(正常)を確認
image.png

DynamoDB画面にて、テーブル項目が作成されているか確認
image.png

API Gatewayの画面からテストコードを入力(update)
image.png

テスト実行、ステータスコード200(正常)を確認
image.png

DynamoDB画面にて、テーブル項目が更新されているか確認
image.png

最後に

API Gateway → Lambda → DynamoDB ハンズオンを実施しましたが、手を動かすことで、理解度を深めることが出来ました。(0→1にすることが重要!)
今回のハンズオンの参考として、CloudTech内のレッスン動画を複数回見て勉強しました。とても素晴らしい教材なので皆さんも是非ご活用ください。
下記にリンクを貼っておきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?