LoginSignup
0

More than 1 year has passed since last update.

awsをterraformで構築するには(基本編)

Posted at

プロバイダーを決める

terraform {
  required_version = ">= 1.0.1"
  required_providers {
    aws = ">= 3.0"
  }
}

provider "aws" {
  access_key = var.aws_access_key
  secret_key = var.aws_secret_key
  region     = var.aws_region
}

変数定義

terraform.tfvarsにて

aws_access_key="xxxxxxxx"
aws_secret_key="xxxxxxxx"
aws_region="xxxxxxxx"

variables.tfの中で

variable "aws_access_key" {
  type = string
}

variable "aws_secret_key" {
  type = string
}

variable "aws_region" {
  type    = string
}

resourceの定義

あとは例えばaws_alb.tfみたいなファイルを作って(ファイルは分割しなくても動くが、基本分割した方が綺麗だし、わかりやすい)、リソースの定義をする。詳しくはドキュメントを確認。

resource "aws_lb" "api_lb" {
  name               = "test-alb"
  internal           = false
  load_balancer_type = "application"
  security_groups    = [aws_security_group.test-group.id]
  subnets            = ["${aws_subnet.test_subnet.id}", "${aws_subnet.test_subnet2.id}"]

  enable_deletion_protection = true

  tags = {
    "Environment" = terraform.workspace
    "Application" = "api"
  }
}

一通り記載したら下記を実行

terraform plan

terraform apply

変更あるたびに terraform plan terraform applyを行う

リソースを一旦消したいのであれば

terraform destroy

以上メモように記録

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