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?

Terraform覚書き2

Posted at

main.tf

  • 実行するTerraformのバージョンとプロバイダの指定
  • モジュールの設定
# ---------------------------------------------
# Terraform configuration
# ---------------------------------------------
terraform {
  required_version = ">=1.7.4"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.61.0"
    }
  }
}

# ---------------------------------------------
# Provider
# ---------------------------------------------
provider "aws" {
  region = var.region
}
# ---------------------------------------------
# modules
# ---------------------------------------------

# network
module "network" {
  source = "./modules/network"

  name      = var.name
  region    = var.region
  vpc_cidr  = var.vpc_cidr
  pub_cidrs = var.public_subnet_cidrs
  pri_cidrs = var.private_subnet_cidrs
}

# ec2
module "ec2" {
  source = "./modules/ec2"

  app_name   = var.name
  vpc_id     = module.network.vpc_id
  subnet_ids = module.network.pub_subnet_ids
}

# rds
module "rds" {
  source = "./modules/rds"

  app_name                  = var.name
  db_name                   = var.db_name
  db_username               = var.db_username
  vpc_id                    = module.network.vpc_id
  subnet_ids                = module.network.pri_subnet_ids
  subnet_cidr_blocks        = module.network.pri_subnet_cidr_blocks
  source_security_group_ids = [module.ec2.ec2_security_group_id]
}

# s3
module "s3" {
  source = "./modules/s3"

  name        = var.name
  identifiers = [module.iam.policy_arn]
}

# IAMrole
module "iam" {
  source = "./modules/iam"
}

terraform.tfvars

  • Terraformの実行時に読み込ませたい変数の値を定義するためのファイル
  • ファイル名を「terraform.tfvars」としていた場合は、Terraformの実行時に自動的にtfvarsファイルに設定されている変数の値を読み込ませることが出来る
  • 認証情報もproviders.tfに直接記述可能が、セキュリティリスクを考慮し環境変数で定義
#--------------------------------------------------------------
# General
#--------------------------------------------------------------

name   = "lecture13"
region = "ap-northeast-1"

#--------------------------------------------------------------
# keys
#--------------------------------------------------------------

access_key = "***************"
secret_key = "*******************"

#--------------------------------------------------------------
# Network
#--------------------------------------------------------------

vpc_cidr = "10.0.0.0/16"
public_subnet_cidrs = {
  "a" = "10.0.0.0/24",
  "c" = "10.0.1.0/24"
}
private_subnet_cidrs = {
  "a" = "10.0.2.0/24",
  "c" = "10.0.3.0/24"
}

#--------------------------------------------------------------
# RDS
#--------------------------------------------------------------

db_name     = "lecture13db"
db_username = "admin"

valiable.tf

  • 変数情報をmain.tf等から分離するためのテンプレートファイル
  • main.tfに記述しても良いが内容が増えるため分割
# ---------------------------------------------
# Variables
# ---------------------------------------------
variable "region" {}
variable "name" {}
variable "vpc_cidr" {}
variable "public_subnet_cidrs" { type = map(string) }
variable "private_subnet_cidrs" { type = map(string) }
variable "db_name" {}
variable "db_username" {}
variable "access_key" {}
variable "secret_key" {}
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?