• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

terraform

date: 2022-02-18 excerpt: terraformの基本的な使い方

tag: terraformgcp


terraformの基本的な使い方

概要

  • Terraform is an open-source infrastructure as code software tool

インストール

nix

$ nix-env -iA nixpkgs.terraform

ubuntu, debian

$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
$ sudo apt-get update && sudo apt-get install terraform

ローカルでdockerコンテナを立ち上げる

    1. ディレクトリを作成し、provider.tf, main.tfというファイルを作成する

provider.tf

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.13"
    }
  }
}

provider "docker" {
  host = "unix:///var/run/docker.sock"
}

main.tf

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "nginx-container"
  ports {
    internal = 80
    external = 8080
  }
}
    1. 初期化
$ terraform init
    1. 反映
$ terraform apply
    1. 動作確認
$ curl http://localhost:8080
<!DOCTYPE html>
<html>
...
    1. 削除
$ terraform destroy

参考

  • AWS入門 - Terraform 使ってみる


terraformgcp Share Tweet