LoginSignup
0
0

More than 1 year has passed since last update.

Python3 DynamoDBにAWS Lambdaからkintoneのレコードを登録する方法

Last updated at Posted at 2022-04-07

DynamoDBにLambdaからkintoneのレコードを登録する方法のメモです。

環境

  • Cloud9
  • Python3.9
  • pykintoneライブラリ

環境構築

Cloud9にPython3.9をインストールする。

・pyenvのインストール
・Python3.9のインストール

Boto3のインストール

・pip install boto3

pykintoneのインストール

・pip install pykintone

EBSボリュームサイズ変更

サンプルコード

import os
import botocore
import boto3
import pykintone

class FetchError(Exception):
    def __init__(self, error, message="kintone record fetch error:"):
        self.error = error
        self.message = message
        super().__init__(self.message)
    def __str__(self):
        return f'{self.message} -> {self.error}'

def lambda_handler(event, context):

    try:
        domain = os.environ['SUBDOMAIN']
        loginuser = os.environ['LOGINUSER']
        password = os.environ['PASSWORD']
        appid = os.environ['APPID']
        backuptable = os.environ['BACKUPTABLE']

        dynamodb = boto3.resource('dynamodb')
        table = dynamodb.Table(backuptable)

        app = pykintone.login(domain, loginuser, password).app(appid)
        response = app.select("order by $id asc limit 500")
        if response.ok:
            records = response.records
            for record in records:
                print(record["$id"]["value"])
                table.put_item(
                    Item={
                        'id': int(record["$id"]["value"]),
                        'record': record
                    }
                )
            return {
                "statusCode": 200,
                "body": {
                    "message": records,
                },
            }
        else:
            print(response.error)
            raise FetchError(response.error)
    except botocore.exceptions.ClientError as error:
        print(error)
        raise error

参考

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