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

More than 1 year has passed since last update.

AWS Amplifyで生成したLambda関数からDynamoDBを参照する方法

1
Posted at

はじめに

AWS Amplifyで生成したLambda関数からDynamoDBを参照するロジックを実装する際、若干ハマったところがあるので忘れないために記事に残しておきます。

TL;DR

  • getAmplifyDataClientConfigメソッドを使おう
  • amplify_outoputs.jsonはフロントエンド向けのリソースだ。バックエンドでは使えないぞ。

実装

handler.ts
import { env } from '$amplify/env/name-of-your-lambda-function';
import { getAmplifyDataClientConfig } from '@aws-amplify/backend/function/runtime';
import { Amplify } from 'aws-amplify';
import { generateClient } from 'aws-amplify/data';
import { Schema } from '../../data/resource';

// clientのセットアップ
const {resourceConfig, libraryOptions} = await getAmplifyDataClientConfig(env);
Amplify.configure(resourceConfig, libraryOptions);
const client = generateClient<Schema>();

// DynamoDBへアクセス
const { errors, data } = await client.models.YourTable.get({ id: "1" });

getAmplifyDataClientConfigからcofigオブジェクトを取得し、それをベースにDynamoDB用のclientを作成します。clientが生成できれば、あとはclient.models.YourTable.*からテーブルへのQuery,Scan等を実行可能です。

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