Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Last active July 27, 2020 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/402c07e12dc7cedaa98f2b1c70b004d6 to your computer and use it in GitHub Desktop.
Save mamemomonga/402c07e12dc7cedaa98f2b1c70b004d6 to your computer and use it in GitHub Desktop.
terraformスニペット集
# main.tf
# ------------------------
# 設定
# ------------------------
locals {
# ドメイン
domain = "hoge.example.org"
# AWSプロファイル
aws_profile = "my-aws-profile"
}
# ------------------------
# backend: terraform cloud
# ------------------------
terraform {
backend remote {
hostname = "app.terraform.io"
organization = "組織名"
workspaces {
name = "ワークスペース名"
}
}
}
# ------------------------
# backend: local
# ------------------------
terraform {
backend "local" {
path = "terraform.tfstate"
}
}
# ------------------------
# EC2キーペア
# ------------------------
resource aws_key_pair d {
key_name = "キーペア名"
public_key = "ssh-rsa (RSA鍵以外不可)..."
}
# ------------------------
# AWS 東京(デフォルト)
# ------------------------
provider aws {
version = "~> 2.69"
region = "ap-northeast-1"
profile = local.aws_profile
}
# ------------------------
# AWS バージニア北部
# ------------------------
provider aws {
version = "~> 2.69"
alias = "usea1"
region = "us-east-1"
profile = local.aws_profile
}
# ------------------------
# デフォルトVPC
# ------------------------
data aws_vpc d {
default = true
}
locals {
vpc_id = data.aws_vpc.d.id
}
# ------------------------
# サブネットID
# ------------------------
data aws_subnet_ids m {
vpc_id = local.vpc_id
}
data aws_subnet m {
for_each = data.aws_subnet_ids.m.ids
id = each.value
}
# local.az2subnet_id["ap-northeast-1a"] という感じで取得できる
# ap-northeast-1a, ap-northeast-1c, ap-northeast-1d
locals {
az2subnet_id = { for i in data.aws_subnet.m : i.availability_zone => i.id }
}
# ------------------------
# 東京リージョンのIP範囲
# ------------------------
data aws_ip_ranges apne1 {
regions = ["ap-northeast-1"]
services = ["ec2"]
}
# ----------------------------
# 設定ずみのRoute53情報をドメイン名から取得
# ----------------------------
data aws_route53_zone pub {
name = local.domain
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment