[Terraform] AppSync GraphQL API + DynamoDB で CRUD する

2026-07-13
AppSync を DynamoDB に繋げて CRUD しました。
Terraform のサンプルコードを載せます。
構成
arch.png
GraphQL スキーマ
スキーマはこんな感じです。
Create/Read/Update/Deleteします。
</>
schema {
  query: Query
  mutation: Mutation
}

type Query {
  notes: [Note!]!
  note(id: ID!): Note
}

type Mutation {
  createNote(input: NoteInput!): Note
  updateNote(id: ID!, input: NoteInput!): Note
  deleteNote(id: ID!): Boolean!
}

input NoteInput {
  title: String!
  message: String!
}

type Note {
  id: ID!
  title: String!
  message: String!
}
コード
</>
1
メモ
AppSync GraphQL API を立てます。authentication_type は API キーにしました。コンソールで API キーを発行しリクエストに付与すると API にアクセスできます
ロギング設定です。とりあえず全部ログに出力します
GraphQL のスキーマです。CRUDします
・notes
・note
・createNote
・updateNote
・deleteNote
createNote のリゾルバーです
DyanmoDB の設定です
これを apply します
</>
$ terraform plan | grep will
Terraform will perform the following actions:
  # aws_appsync_datasource.dynamodb will be created
  # aws_appsync_graphql_api.main will be created
  # aws_appsync_resolver.create_note will be created
  # aws_appsync_resolver.delete_note will be created
  # aws_appsync_resolver.note will be created
  # aws_appsync_resolver.notes will be created
  # aws_appsync_resolver.update_note will be created
  # aws_dynamodb_table.main will be created
  # aws_iam_role.appsync_datasource_dynamodb will be created
  # aws_iam_role.appsync_logging will be created
  # aws_iam_role_policy.appsync_datasource_dynamodb_dynamodb will be created
  # aws_iam_role_policy.appsync_logging_logs will be created

$ terraform apply
動作確認
APIキーの発行
では実際にGraphQLのリクエストを投げていきますが、
その前にまずAPIキーを発行します。
create-apikey.png
GraphQLリクエスト
では実際にリクエストを投げていきます。
まずはデータの作成です。
</>
mutation {
  createNote(input: {
    title: "タイトル",
    message: "テキスト"
  }) {
    id
  }
}
graphql-create.png
つづいて更新です。
graphql-update.png
一覧
graphql-list.png
詳細
graphql-get.png
削除
graphql-delete.png