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

SDKからTerraformへの移行2

0
Posted at

やること

  • DynamoDB
  • S3
  • Lambda
  • API Gateway

彼らからTerraformに移行させる
まずファイルをディレクトリに当てる。

- ~/infra/terraform/main.tf # 実際の AWS リソースを置く場所
- ~/infra/terraform/outputs.tf # provider定義
- ~/infra/terraform/providers.tf # 出力定義
- ~/infra/terraform/variables.tf # 変数定義

参考(https://qiita.com/jyas-protein/items/e4c8ead263b8f3ca785f)

main.tfから

resource "aws_dynamodb_table" "session" {
  name         = "IppunIchieTable"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "userId"
  range_key    = "sessionId"

  attribute {
    name = "userId"
    type = "S"
  }

  attribute {
    name = "sessionId"
    type = "S"
  }

  ttl {
    attribute_name = "ttl"
    enabled        = true
  }

  point_in_time_recovery {
    enabled = true
  }

  tags = {
    Name        = "IppunIchieTable"
    Project     = "ippun-ichie"
    Environment = "prod"
  }
}

resource "aws_s3_bucket" "slides_raw" {
  bucket = "ippun-ichie-slides-raw-${data.aws_caller_identity.current.account_id}-${var.aws_region}"

  tags = {
    Name        = "ippun-ichie-slides-raw"
    Project     = "ippun-ichie"
    Environment = "prod"
  }
}

resource "aws_s3_bucket_public_access_block" "slides_raw" {
  bucket = aws_s3_bucket.slides_raw.id

  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

resource "aws_s3_bucket_server_side_encryption_configuration" "slides_raw" {
  bucket = aws_s3_bucket.slides_raw.id

  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm = "AES256"
    }
  }
}

data "aws_caller_identity" "current" {}

variable.tfも書く

変数名 用途 補足
aws_region デプロイ先リージョン デフォルトは東京(ap-northeast-1
project_name プロジェクト名 タグ付けやリソース命名に使う想定
environment 環境名 prod(本番)。他に dev / staging 用の tfvars を切り替える運用が想定される
jwt_secret JWT署名用の秘密鍵 sensitive = true かつ default なし=必須入力(環境変数から渡す)。ログにも値を表示しない
apple_client_id Apple Sign-inのクライアントID 認証機能で使用
lambda_zip_path Lambdaのデプロイパッケージのパス AWS Lambdaは「関数のコード」をどうにかしてAWSにアップロードする必要があるゆえにZIPファイル

outputs.tfも書く。コレはさっきと真逆のことでterraformを実行した時に外へみせる/渡す値
ok

Terraformファイル構成今

ファイル 役割 主な内容
variables.tf 入力変数の定義 aws_regionproject_nameenvironmentjwt_secret など
providers.tf Terraform / AWS Provider の設定 AWSリージョン(region = var.aws_region)、共通タグなど
main.tf AWSリソースの定義 DynamoDB、S3、S3の公開アクセスブロック・暗号化、AWSアカウント情報など
outputs.tf Terraform実行後に出力する値の定義 DynamoDB名、S3バケット名、AWSアカウントIDなど

ファイル同士の関係

variables.tf
     │
     │ 入力値
     ▼
providers.tf
     │
     │ AWS Provider・共通設定
     ▼
main.tf
     │
     │ AWSリソースを作成
     ▼
outputs.tf
     │
     │ 作成したリソースの情報を出力
     ▼
   Output

ok

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?