LoginSignup
0
1

More than 1 year has passed since last update.

describe 等で取得した情報を Dynamo DB に保存する

Last updated at Posted at 2021-05-18

素人が自分のノートとして書いてますが、改善点あれば是非コメントください

DynamoDBテーブルに入力する値を渡された関数だけをピックアップ。

aws-lambda-python3_8
import json
import boto3

def lambda_handler():

    #DynamoDBテーブルはあらかじめ作ってある前提。本来なら、テーブルが無ければ作るみたいな感じにすべき。
    table_name = 'test'

    #DynamoDBテーブルに行として追加する情報を item として定義
    item = {
            "db-instance-id": 'i-xxxxxxxxxxxx',
            "db-instance-version": '1.23.3'
    }

    #DynamoDBクライアントを定義
    dynamodb_client = boto3.resource('dynamodb')

    #テーブルにItemを入力
    dynamo_table = dynamodb_client.Table(table_name)
    dynamo_table.put_item(Item=item)

    return

"An error occurred (AccessDeniedException) when calling the PutItem operation のエラーが出たときは権限問題。

エラー
{
  "errorMessage": "An error occurred (AccessDeniedException) when calling the PutItem operation: User: arn:aws:sts::xxxxxxxxx:assumed-role/xxxx-listing-role-8pgwts5t/pvdriver-listing is not authorized to perform: dynamodb:PutItem on resource: arn:aws:dynamodb:ap-northeast-1:xxxxxxxxxxxx:table/yyyyyyyy",
  "errorType": "ClientError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 40, in lambda_handler\n    insert(index, name, version)\n",
    "  File \"/var/task/lambda_function.py\", line 16, in insert\n    dynamodb_table.put_item(Item=item)\n",
    "  File \"/var/runtime/boto3/resources/factory.py\", line 520, in do_action\n    response = action(self, *args, **kwargs)\n",
    "  File \"/var/runtime/boto3/resources/action.py\", line 83, in __call__\n    response = getattr(parent.meta.client, operation_name)(*args, **params)\n",
    "  File \"/var/runtime/botocore/client.py\", line 357, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/runtime/botocore/client.py\", line 676, in _make_api_call\n    raise error_class(parsed_response, operation_name)\n"
  ]
}

対象のLambda関数に付与されたIAMロールに AmazonDynamoDBFullAccess を追加する。
Lambda > 関数 > 対象Lambda関数 > 設定タブ > アクセス権限 > 実行ロール > 【対象ロール】> IAM設定画面に遷移 > 【ポリシーをアタッチします】> AmazonDynamoDBFullAccess を選択 > 【ポリシーのアタッチ】
Putが出来ないといけないので、とりあえず FullAccess を選択。

0
1
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
1