Closed7

TerraformでAWS AppSync上にGraphQL APIを構築したい(HTTPデータソース)

qmotasqmotas
qmotasqmotas

雑感

  • 抽象度はCloudFormationと同程度
  • VSCodeの拡張(HashiCorp Terraform)のおかげでTerraformの方が書きやすく感じた
    • CloudFormationに比べたらというだけで書きやすいわけではない
    • Serverless Frameworkの方が圧倒的に書きやすい(が、構築できる環境が限定的)
  • スキーマや関数の実装を外部ファイルに書ける
  • 公式リファレンスがJavaScript Resolverに対応している
    • Serverless Frameworkはプラグインのalpha版でようやく対応なので、Terraformの方が追従は早そう(それはそう)
  • CloudFormationと比べて圧倒的に構築が早い
    • Serverless FrameworkはCloudFormationをラップしてるだけなので遅い
    • Terraformはドリフト検知が行えない
      • Terraform Cloudならできるっぽい
qmotasqmotas

作業ログ

インストール

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli

qmotasqmotas

Command Line Toolsが古いと怒られた

brew install hashicorp/tap/terraform
Running `brew update --auto-update`...
==> Fetching hashicorp/tap/terraform
==> Downloading https://releases.hashicorp.com/terraform/1.3.8/terraform_1.3.8_darwin_amd64.zip
######################################################################## 100.0%
==> Installing terraform from hashicorp/tap
Error: Your Command Line Tools are too outdated.
Update them from Software Update in System Preferences.

If that doesn't show you any updates, run:
  sudo rm -rf /Library/Developer/CommandLineTools
  sudo xcode-select --install

Alternatively, manually download them from:
  https://developer.apple.com/download/all/.
You should download the Command Line Tools for Xcode 13.4.

ので、愚直に案内に従ってupdate

sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install

It takes forever

qmotasqmotas

main.tf

main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region = "ap-northeast-1"
}

resource "aws_appsync_graphql_api" "this" {
  authentication_type = "API_KEY"
  name                = "AppSync Playground(terraform)"
  schema              = file("schema.graphql")
}

resource "aws_appsync_datasource" "catstronauts_rest_api" {
  api_id = aws_appsync_graphql_api.this.id
  name   = "catstronauts_rest_api"
  type   = "HTTP"

  http_config {
    endpoint = "https://odyssey-lift-off-rest-api.herokuapp.com"
  }
}

resource "aws_appsync_function" "get_tracks_for_home" {
  api_id      = aws_appsync_graphql_api.this.id
  data_source = aws_appsync_datasource.catstronauts_rest_api.name
  name        = "getTracksForHome"
  code        = file("functions/getTracksForHome.js")

  runtime {
    name            = "APPSYNC_JS"
    runtime_version = "1.0.0"
  }
}

resource "aws_appsync_function" "get_author" {
  api_id      = aws_appsync_graphql_api.this.id
  data_source = aws_appsync_datasource.catstronauts_rest_api.name
  name        = "getAuthor"
  code        = file("functions/getAuthor.js")

  runtime {
    name            = "APPSYNC_JS"
    runtime_version = "1.0.0"
  }
}

resource "aws_appsync_resolver" "query_tracks_for_home" {
  api_id = aws_appsync_graphql_api.this.id
  type   = "Query"
  field  = "tracksForHome"
  kind   = "PIPELINE"
  code   = file("resolvers/Query.tracksForHome.js")

  runtime {
    name            = "APPSYNC_JS"
    runtime_version = "1.0.0"
  }

  pipeline_config {
    functions = [
      aws_appsync_function.get_tracks_for_home.function_id,
    ]
  }
}

resource "aws_appsync_resolver" "track_author" {
  api_id = aws_appsync_graphql_api.this.id
  type   = "Track"
  field  = "author"
  kind   = "PIPELINE"
  code   = file("resolvers/Track.author.js")

  runtime {
    name            = "APPSYNC_JS"
    runtime_version = "1.0.0"
  }

  pipeline_config {
    functions = [
      aws_appsync_function.get_author.function_id,
    ]
  }
}
qmotasqmotas

実行

terraform init
terraform apply

CloudFormationを使わないので構築がめちゃくちゃ早い

このスクラップは2023/02/15にクローズされました