GithubHelp home page GithubHelp logo

Terraformについて about self-study HOT 4 OPEN

Yuya-Furusawa avatar Yuya-Furusawa commented on July 28, 2024
Terraformについて

from self-study.

Comments (4)

Yuya-Furusawa avatar Yuya-Furusawa commented on July 28, 2024

Failed to query available provider packagesエラーについて

初歩的なミスだけどスタックしたのでメモ

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

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

こんな感じのことを書くが、その際にhashicorp/awsを引っ張ってくる

しかしリソースにtypoで

resource "awas_instance" "sample" {
  ami = "ami-0ba46945fe4414e8a"
  instance_type = "t2.small"
}

と書いてしまうとawasを引っ張ってこようとして存在しないのでエラーになる

つまりTerraformはリソースのprefixをみてproviderを取ってくる
なのでtypoには注意

参照

from self-study.

Yuya-Furusawa avatar Yuya-Furusawa commented on July 28, 2024

outputについて

Terraformでは、他ファイルのリソースは同じディレクトリ内でしか参照することができない。
そのため、別ディレクトリにあるリソースを参照するにはoutputを用いる

outputでエクスポートする変数を宣言する

# "instance_ip_addr"が名前
output "instance_ip_addr" {
  value = aws_instance.server.private_ip #エクスポートするリソース
}

terraform_remote_stateを使う

data "terraform_remote_state" "vpc" {
  backend = "s3"
  config = {
    region = "ap-northeast-1"
  }
}

③エクスポートされたリソースを使う
data.terraform_remote_state.[②で指定した名前].outputs.[①で指定した名前]で参照することができる

resource "aws_instance" "foo" {
  # ...
  subnet_id = data.terraform_remote_state.vpc.outputs.instance_ip_addr
}

参照

from self-study.

Yuya-Furusawa avatar Yuya-Furusawa commented on July 28, 2024

for_eachについて

Terraformでのループ処理。リソースを複数作成するときとかに使う。
for_eachはMeta-Argumentで、moduleやresourceで使用可能。

resource "azurerm_resource_group" "rg" {
  for_each = {
    a_group = "eastus"
    another_group = "westus2"
  }
  name     = each.key
  location = each.value
}
  • for_eachmapset(string)しか受け取ることができない。listtupleは受け取ることができない。
    • for_each = ["hoge", "fuga"]みたいな形は無理
    • for_each = toset(["hoge", "fuga"])ならOK
  • 同じresource内でeach.keyeach.valueとして参照できる
  • for_eachforと共に使うことが多い
locals {
  images = [
    { name = "foo", image = "alpine" },
    { name = "bar", image = "debian" },
  ]
}

resource "docker_container" "this" {
  for_each = { for i in local.images : i.name => i }
  name     = each.value.name
  image    = each.value.image
}

参照

from self-study.

Yuya-Furusawa avatar Yuya-Furusawa commented on July 28, 2024

depends_onについて

  • terraform applyするとき、どういう順序でリソースが作成されるかはTerraformが自動的に決めてくれる
  • しかし稀にちゃんと順序を手動で決める必要がある
  • depends_onで指定したリソースが作られたあとにそのリソースが作られる
  • ALBとECS Serviceを接続するときに順序をちゃんと指定する必要があった(リスナーを先に作る必要があった)
resource "aws_ecs_service" "webapp-service" {
  name = "sample-service"
  cluster = aws_ecs_cluster.sample-ecs-cluster.id
  task_definition = aws_ecs_task_definition.sample-task.arn
  desired_count = 1
  launch_type = "EC2"

  load_balancer {
    target_group_arn = aws_lb_target_group.http.arn
    container_name = "sample-webapp"
    container_port = "3000"
  }

  depends_on = [
    aws_lb_listener.http #リスナーが作られた後にECS Serivceが作られる
  ]
}

参照

from self-study.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.