LoginSignup
9
6

More than 3 years have passed since last update.

Terraformでmoduleを使ってみる。

Last updated at Posted at 2020-02-14

Terraformにはモジュールで処理を使いまわせる機能がありますが
IaC初心者には理解が難しかったので備忘録がてらメモです。

ディレクトリ構造

work/
├── module
│   └── ec2_module.tf
└── test.tf

ec2_module.tf

モジュールの中身

##########################################################
///moduleでリソースを作成するときに指定する変数
##########################################################
variable "ami_id" {}
variable "instance_type" {}
variable "tags_name" {}

##########################################################
///実際にresourceを作成する部分
##########################################################

resource "aws_instance" "web" {
  ami           =  var.ami_id
  instance_type =  var.instance_type

  tags = {
    Name = var.tags_name
  }
}

test.tf

モジュールを呼び出す部分

##########################################################
///local変数
##########################################################
locals {
   instance = "t3.micro"
}
##########################################################
///ec2のmoduleを使用する部分(sourceでmoduleの場所を指定し、入力を渡す)
##########################################################

///moduleを使用する際には、モジュール本体は別ディレクトリに配置する必要がある。

module "ec2_1" {
  source        = "./module"
  ami_id        = data.aws_ami.ubuntu.id ///データソースで定義した部分
  instance_type = local.instance ///local変数で定義した部分
  tags_name     = "ec2_1"  
}

module "ec2_2" {
  source        = "./module"
  ami_id        = data.aws_ami.ubuntu.id
  instance_type = local.instance
  tags_name     = "ec2_2" 
}

##########################################################
///使用するamiをfilterして指定する
##########################################################

///データソースを使用すると、Terraform構成の他の場所で使用するためにデータを取得または計算できます。
data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}

実行してみる

terraformでmoudleを使用するには
terraform init か terraform get が必須です。

[vagrant@terraform work]$ terraform init
Initializing modules...       ←こんな感じでモジュールを呼び出す         
- ec2_1 in module
- ec2_2 in module

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.49.0...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 2.49"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
[vagrant@terraform work]$

あとはいつも通り、planしてapplyしてあげれば作成できます。
あまりコードを書いた経験のないエンジニアだと最初理解に苦しむかもしれませんが(実体験)、
結構簡単なことしかしていないです。

9
6
1

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
9
6