- Terraformã³ã¼ãã¹ã¿ã¤ã«ä¸è¦§
- Terraformã®ã³ã¼ããã©ã¼ããã
- ã³ã¼ãæ¤è¨¼
- ãã¡ã¤ã«å
- ã³ã¡ã³ã
- ãªã½ã¼ã¹å½åè¦å
- ãªã½ã¼ã¹é åº
- å¤æ°(ãã©ã¡ã¼ã¿ã¼)
- Outputs
- Local values
- ãããã¤ãã¼ã¨ã¤ãªã¢ã·ã³ã°
- count, for_each
- .gitignore
åè : https://developer.hashicorp.com/terraform/language/style
Terraformã®è¨è¨ä¸ã®æ±ºå®ã¯çµç¹ã®ãã¼ãºã好ã¿ã«ä¾åãã¾ãããæ¡ç¨ãããã¨ããå§ãããããã¤ãã®ä¸è¬çãªãã¿ã¼ã³ãããã¾ãã ãããã¹ã¿ã¤ã«ã¬ã¤ãã¨å¼ã³ã¾ã ã¹ã¿ã¤ã«ã¬ã¤ããæ¡ç¨ãã¦ã³ã¼ãã£ã³ã°ããã¨ããã©ãã©ã¼ã ã³ã¼ããèªã¿ããããã¹ã±ã¼ã©ãã«ã§ãä¿å®å¯è½ã«ãªãã¾ãã
ãã®è¨äºã§ã¯ãçµç¹ã®ã¹ã¿ã¤ã«ã¬ã¤ããä½æããéã«ããã¹ããã©ã¯ãã£ã¹ã¨ããã¤ãã®èæ ®äºé ã«ã¤ãã¦çæãã¹ãããã¤ãã®èæ ®äºé ã«ã¤ãã¦èª¬æãã¾ãã æåã¯ããã©ã¼ãããããªã½ã¼ã¹çµç¹ãªã©ã®ã³ã¼ãã¹ã¿ã¤ã«ã®æ¨å¥¨äºé ã«ã¤ãã¦èª¬æãã¾ãã 次ã¯ã¡ã¿ã¢ã¼ã°ã¡ã³ãããã¼ã¸ã§ã³åãææãªãã¼ã¿ç®¡çãä»ããã©ã¤ããµã¤ã¯ã«ç®¡çãªã©ã®æä½ã¨ã¯ã¼ã¯ããã¼ã®æ¨å¥¨äºé ã«ã¤ãã¦èª¬æãã¾ãã
Terraformã³ã¼ãã¹ã¿ã¤ã«ä¸è¦§
ä¸è²«ããã¹ã¿ã¤ã«ã§Terraformã³ã¼ããæ¸ããã¨ã§ãèªã¿ãããç¶æãããããªãã¾ãã次ã®ã»ã¯ã·ã§ã³ã§ã¯ãã³ã¼ãã¹ã¿ã¤ã«ã®æ¨å¥¨äºé ã«ã¤ãã¦èª¬æãã¾ãã
- ã³ã¼ããã³ãããããåã«ã
terraform fmt
ã¨terraform validate
æ¤è¨¼ãã¾ãã - TFLintãªã©ã®
linter
ã使ç¨ãã¦ãçµç¹èªèº«ã®ã³ã¼ãã£ã³ã°ãã¹ããã©ã¯ãã£ã¹ãå®æ½ãã¾ãã - åä¸ããã³ãã«ãã©ã¤ã³ã®ã³ã¡ã³ãã«
ï¼
ã使ç¨ã§ãã¾ãã - ãªã½ã¼ã¹åã«åè©ã使ç¨ããååã«ãªã½ã¼ã¹ã¿ã¤ããå«ããªãã§ãã ããã
- å¥ã®ãªã½ã¼ã¹ããåç
§ããéã«ã¯ã
.
ç¹ããã§ãªã½ã¼ã¹ã¿ã¤ããæè¨ãããã¨ãã§ããããã§ãã
- å¥ã®ãªã½ã¼ã¹ããåç
§ããéã«ã¯ã
- ã¢ã³ãã¼ã¹ã³ã¢ã使ç¨ãã¦ãååãåé¢ãã¾ãã ãªã½ã¼ã¹ã®ã¿ã¤ãã¨ååããªã½ã¼ã¹å®ç¾©ã§äºéå¼ç¨ç¬¦ã§å·»ãä»ãã¾ãã
- å¤æ°ã®éå°ä½¿ç¨ã¯é¿ãã¦ãã ããã
- å¿ ãããã©ã«ãã®ãããã¤ãã¼æ§æãå«ãã¾ãã
- countã使ç¨ãã¦ã
for_each
æ§æã®éå°ä½¿ç¨ã¯é¿ãã¦ãã ããã
以ä¸ã¯ãããã®ã¹ã¿ã¤ã«ã¬ã¤ããå®ã£ãS3ã¨IAMã¦ã¼ã¶ã¼ãä½æããTerraformã³ã¼ãã§ãã
# ããã©ã«ãã®AWSãããã¤ãã¼è¨å® provider "aws" { region = "us-west-2" } # S3ãã±ããã®ä½æ resource "aws_s3_bucket" "my_bucket" { bucket = "my-unique-bucket-name" acl = "private" tags = { Name = "MyBucket" Environment = "Dev" } } # IAMã¦ã¼ã¶ã¼ã®ä½æ (countã使ç¨ãã¦è¤æ°ã®ã¦ã¼ã¶ã¼ãä½æ) resource "aws_iam_user" "app_user" { count = 2 # 2ã¤ã®ã¦ã¼ã¶ã¼ãä½æ name = "app_user_${count.index + 1}" path = "/" force_destroy = false tags = { Name = "AppUser${count.index + 1}" Environment = "Dev" } } # IAMã¦ã¼ã¶ã¼ç¨ã®ã¢ã¯ã»ã¹ãã¼ãä½æ (for_eachã使ç¨ãã¦åã¦ã¼ã¶ã¼ã«ã¢ã¯ã»ã¹ãã¼ãä½æ) resource "aws_iam_access_key" "app_user_key" { for_each = aws_iam_user.app_user user = each.value.name } # S3ãã±ããã¸ã®ã¢ã¯ã»ã¹ã許å¯ããããªã·ã¼ã®ä½æ resource "aws_iam_policy" "s3_access_policy" { name = "S3AccessPolicy" path = "/" policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = ["s3:ListBucket"] Effect = "Allow" Resource = [aws_s3_bucket.my_bucket.arn] }, { Action = ["s3:GetObject", "s3:PutObject"] Effect = "Allow" Resource = ["${aws_s3_bucket.my_bucket.arn}/*"] } ] }) } # IAMããªã·ã¼ãIAMã¦ã¼ã¶ã¼ã«ã¢ã¿ãã (for_eachã使ç¨ãã¦åã¦ã¼ã¶ã¼ã«ããªã·ã¼ãã¢ã¿ãã) resource "aws_iam_user_policy_attachment" "app_user_policy_attachment" { for_each = aws_iam_user.app_user user = each.value.name policy_arn = aws_iam_policy.s3_access_policy.arn }
Terraformã®ã³ã¼ããã©ã¼ããã
åãã¹ãã¬ãã«ã«ã¯ã2ã¤ã®ã¹ãã¼ã¹ã®ã¤ã³ãã³ããæ¨å¥¨ãã¾ãã
å¼æ°ã®å¤ã¯ã¿ãã使ç¨ãã¦é«ããæãã¾ãããã
- NG
ami = "abc123" instance_type = "t2.micro"
- GOOD
ami = "abc123" instance_type = "t2.micro"
å¼æ°ã¨ãããã¯ã®ä½ç½®
- å¼æ°ã¨ãããã¯ãåä¸ã®ãããã¯ããã£å é¨ã«ä¸ç·ã«æ¸ãè¾¼ãéã«ã¯ãå¼æ°ãã¾ã¨ãã¦ä¸é¨ã«é ç½®ãããã®ä¸ã«ãã¹ãããããããã¯ãé ç½®ãã¾ãã
- ã¾ããå¼æ°ç¾¤ã¨ãããã¯ã®éã«ã¯1ã¤ã®ç©ºç½è¡ã使ç¨ãã¦ãå¼æ°ããããã¯ããåé¢ãã¾ãã
空ã®è¡ã使ç¨ãã¦ããããã¯å ã®å¼æ°ã®è«çã°ã«ã¼ããåé¢ãã¾ãããã
NG
resource "aws_instance" "example" { ami = "ami-12345678" instance_type = "t2.micro" tags = { Name = "example-instance" } network_interface { device_index = 0 network_interface_id = "eni-12345678" } }
- GOOD
resource "aws_instance" "example" { ami = "ami-12345678" instance_type = "t2.micro" tags = { Name = "example-instance" } network_interface { device_index = 0 network_interface_id = "eni-12345678" } }
ã¡ã¿å¼æ°ã®
- å¼æ°ã¨ãã¡ã¿å¼æ°(countãfor_eachãdepends_on)ãã®ä¸¡æ¹ãå«ããããã¯ã«ã¤ãã¦ã¯ãæåã«ã¡ã¿å¼æ°ãé ç½®ãã1ã¤ã®ç©ºç½è¡ã§ä»ã®å¼æ°ããåé¢ãã¾ãã
resource "aws_instance" "example" { # meta-argument first count = 2 ami = "abc123" instance_type = "t2.micro" network_interface { # ... } # meta-argument block last lifecycle { create_before_destroy = true } }
- ãããã¯ã¿ã¤ããã»ãã³ãã£ã¯ã¹ã«ãã£ã¦å®ç¾©ããã¦ãã¡ããªãå½¢æããå ´åãé¤ããåãã¿ã¤ãã®è¤æ°ã®ãããã¯ãç°ãªãã¿ã¤ãã®ä»ã®ãããã¯ã¨ã°ã«ã¼ãåããªãã§ãã ãããï¼ä¾ï¼root_block_deviceãebs_block_deviceãephemeral_block_device on aws_instanceã®awsãããã¯ããã¤ã¹ã説æãããããã¯ã¿ã¤ãã®ãã¡ããªãå½¢æãããããä¸ç·ã«ã°ã«ã¼ãåãã¦æ··åã§ãã¾ããï¼
erraform fmt
ã³ãã³ãã¯ãTerraformæ§æãä¸è¨ã®æ¨å¥¨äºé
ãã©ã¼ãããç·¨éãã¾ãã
ããã©ã«ãã§ã¯ãterraform fmt
ã³ãã³ãã¯ããããå®è¡ãããã£ã¬ã¯ããªå
ã®Terraformã³ã¼ãã®ã¿ãå¤æ´ãã¾ããããã¹ã¦ã®ãµããã£ã¬ã¯ããªã®ã³ã¼ããå¤æ´ããããã®-recursive
ãã©ã°ãå«ãããã¨ãã§ãã¾ãã
Terraformã³ã¼ããç·¨éãããã¨æ°ãã¼ã¸ã§ã³ã«ã³ãããããåã«ãterraform fmt
ãå®è¡ãããã¨ããå§ããã¾ãã
git pre-commit hooksãªã©ã®ã¡ã«ããºã ã使ç¨ãã¦ãã³ã¼ããã³ããããããã³ã«ãã®ã³ãã³ããèªåçã«å®è¡ã§ãã¾ãã
VSCodeã使ç¨ããå ´åã¯ãterraform vscodeæ¡å¼µæ©è½ã使ç¨ãã¦ãæ§æã®å¼·èª¿è¡¨ç¤ºã¨æ¤è¨¼ãèªåã³ã¼ãã®æ¸å¼è¨å®ãªã©ã®æ©è½ãæå¹ã«ãã¾ãã éçºç°å¢ã¾ãã¯ããã¹ãã¨ãã£ã¿ã¼ãè¨èªãµã¼ãã¼ãããã³ã«ããµãã¼ããã¦ããå ´åãTerraform Language Serverã使ç¨ãã¦VSCodeæ¡å¼µæ©è½ã®ã»ã¨ãã©å ¨ã¦ã使ç¨ã§ãã¾ãã
ã³ã¼ãæ¤è¨¼
terraform validate
ã¯ãã½ã¼ã¹ã³ã¼ããæ§æçã«æå¹ã§ããå
é¨çã«ä¸è²«ãã¦ãããã¨ãæ¤è¨¼ããã³ãã³ããã§ãã¯ã§ãã
ãã ããã®æ¤è¨¼ã³ãã³ãã¯ãå¼æ°å¤ãç¹å®ã®ãããã¤ãã¼ã«å¯¾ãã¦æå¹ãã©ããã¾ã§ã¯ç¢ºèªãã¾ããã
terraform validate
ã³ãã³ãã¯ãèªåçãã¤é »ç¹ã«å®è¡ã§ãã¾ãã
ããã¹ãã¨ãã£ã¿ã¼ãè¨å®ãã¦ããã®ã³ãã³ãããã¹ãã»ã¼ããã§ãã¯ã¨ãã¦å®è¡ããããgitãªãã¸ããªã®äºåã³ãããããã¯ã¨ãã¦å®ç¾©ããããCI/CDãã¤ãã©ã¤ã³ã®ã¹ãããã¨ãã¦å®è¡ãããã§ãã¾ãã
詳細ã«ã¤ãã¦ã¯ãTerraformã®æ¤è¨¼ããã¥ã¡ã³ããåç §ãã¦ãã ããã
ãã¡ã¤ã«å
Terraformã§ã¯æ¬¡ã®ãã¡ã¤ã«ã®å½åè¦åããå§ããã¾ãã
- ããã¯ã¨ã³ãæ§æãå«ã
backend.tf
ãã¡ã¤ã«ã(æ§æå ã®è¤æ°ã®TerraFormãããã¯ãå®ç¾©ãã¦ãããã¯ã¨ã³ãæ§æãTerraformããã³ãããã¤ãã¼ãã¼ã¸ã§ã³æ§æããåé¢ã§ãã¾ãã) - ãã¹ã¦ã®ãªã½ã¼ã¹ã¨ãã¼ã¿ã½ã¼ã¹ãããã¯ãå«ã
main.tf
ãã¡ã¤ã«ã - ãã¹ã¦ã®åºåãããã¯ãã¢ã«ãã¡ãããé ã«å«ã
outputs.tf
ãã¡ã¤ã«ã - ãã¹ã¦ã®ãããã¤ãã¼ãããã¯ã¨æ§æãå«ã
providers.tf
ãã¡ã¤ã«ã - ã¢ã«ãã¡ãããé ã«ãã¹ã¦ã®å¤æ°ãããã¯ãå«ãå¤æ°ã
variables.tf
ãã¡ã¤ã«ã - ãã¼ã«ã«å¤ãå«ã
locals.tf
ãã¡ã¤ã«ã - æ§æã®ãªã¼ãã¼ã©ã¤ãå®ç¾©ãå«ã
override.tf
ãã¡ã¤ã«ããããã®ãªã¼ãã¼ã©ã¤ãã«ãããã³ã¼ãã®æ¨è«ããã®ãé£ãããªãããé度ãªä½¿ç¨ã¯ãæ§ããã ããã
ã³ã¼ããã¼ã¹ãæé·ããã«ã¤ãã¦ãTerraformã³ã¼ãã®ç®¡çããããã®ãã¡ã¤ã«ã ãã«å¶éãããã¨ã¯ãç¶æãå°é£ã«ãªãå¯è½æ§ãããã¾ãã ã³ã¼ãããµã¤ãºã®ããã«ããã²ã¼ããã«ããå ´åã¯ãè«çã°ã«ã¼ããã¨ã«å¥ã®ãã¡ã¤ã«ã§ãªã½ã¼ã¹ã¨ãã¼ã¿ã½ã¼ã¹ãæ´çãããã¨ããå§ããã¾ãã
ãã¨ãã°ãWebã¢ããªã±ã¼ã·ã§ã³ã«ãããã¯ã¼ãã³ã°ãã¹ãã¬ã¼ã¸ãããã³ã³ã³ãã¥ã¼ãã£ã³ã°ãªã½ã¼ã¹ãå¿ è¦ãªå ´åã次ã®ãã¡ã¤ã«ãä½æã§ãã¾ãã
- VPCããµããããããã¼ããã©ã³ãµã¼ãããã³ãã®ä»ã®ãã¹ã¦ã®ãããã¯ã¼ãã³ã°ãªã½ã¼ã¹ãå«ã
network.tf
ãã¡ã¤ã«ã - ãªãã¸ã§ã¯ãã¹ãã¬ã¼ã¸ã¨é¢é£ãã権éæ§æãå«ã
storage.tf
ãã¡ã¤ã«ã - ã³ã³ãã¥ã¼ãã£ã³ã°ã¤ã³ã¹ã¿ã³ã¹ãå«ã
compute.tf
ãã¡ã¤ã«ã
ã³ã¡ã³ã
åºæ¬ã¯ã³ã¡ã³ãã§ã¯ãªããç°¡åã«ç解ã§ããããã«ã³ã¼ããæ¸ãã¦ãã ãããå¿ è¦ãªå ´åã«ã®ã¿ãã³ã¡ã³ãã使ç¨ãã¦ãä»ã®ã¡ã³ããã¼ã®è¤éãã軽æ¸ãã¾ãããã
åä¸è¡ã³ã¡ã³ãã¨è¤æ°æ¥ã³ã¡ã³ãã®ä¸¡æ¹ã«ï¼
ã使ç¨ãã¾ãã
//
ããã³ /* */
ã³ã¡ã³ãæ§æã¯æ
£ç¨çã§ã¯ããã¾ããããTerraformã¯ã以åã®ãã¼ã¸ã§ã³ã®HCLã¨å¾æ¹äºææ§ãç¶æãããã¨ããµãã¼ããã¦ãã¾ãã
# Each tunnel is responsible for encrypting and decrypting traffic exiting # and leaving its associated gateway.resource "google_compute_vpn_tunnel" "tunnel1" { ## ...
ãªã½ã¼ã¹å½åè¦å
Terraformæ§æå ã®ãã¹ã¦ã®ãªã½ã¼ã¹ã«ã¯ãä¸æã®ååãå¿ è¦ã§ãã ä¸è²«æ§ã¨èªã¿ãããã®ããã«ã説æçãªåè©ã使ç¨ããã¢ã³ãã¼ã¹ã³ã¢ã®ããåèªãåé¢ãã¾ãã ãã ãããªã½ã¼ã¹ã¢ãã¬ã¹ã¯ãã§ã«å«ã¾ãã¦ããããããªã½ã¼ã¹èå¥åã«ãªã½ã¼ã¹ã¿ã¤ããå«ããªãã§ãã ããã ãªã½ã¼ã¹ã®ç¨®é¡ã¨ååãäºéå¼ç¨ç¬¦ã§å ã¿ã¾ãã
- NG:
resource aws_instance webAPI-aws-instance {...}
- Good:
resource "aws_instance" "web_api" {...}
ãªã½ã¼ã¹é åº
ã³ã¼ãå ã®ãªã½ã¼ã¹ã¨ãã¼ã¿ã½ã¼ã¹ã®é åºã¯ãTerraformãããããæ§ç¯ããæ¹æ³ã«å½±é¿ããªããããèªã¿ãããããããã«ãªã½ã¼ã¹ãæ´çãã¦ãã ããã Terraformã¯ããªã½ã¼ã¹éã®ä¾åé¢ä¿ã«åºã¥ãã¦ä½æé åºã決å®ãã¾ãã
ãªã½ã¼ã¹ã®é çªã®ãã¹ããã©ã¯ãã£ã¹ã¯ãã³ã¼ãã®ãµã¤ãºã¨è¤éãã«å¤§ããä¾åãã¾ããããã¼ã¿ã½ã¼ã¹ãåç §ãããªã½ã¼ã¹ã¨ä¸ç·ã«ãã¼ã¿ã½ã¼ã¹ãå®ç¾©ãããã¨ããå§ããã¾ãã èªã¿ãããã®ããã«ãTerraformã³ã¼ãã¯ãããèªä½ã«åºã¥ãã¦æ§ç¯ãããå¿ è¦ãããã¾ãããããåç §ãããªã½ã¼ã¹ã®åã«ãã¼ã¿ã½ã¼ã¹ãå®ç¾©ããå¿ è¦ãããã¾ãã
次ã®ä¾ã§ã¯ã2ã¤ã®ãã¼ã¿ã½ã¼ã¹ãaws_ami
ã¨aws_availability_zones
ã«ä¾åããaws_instance
ãå®ç¾©ãã¦ãã¾ãã
èªã¿ãããã¨é£ç¶æ§ã®ããã«ãaws_instance
ãªã½ã¼ã¹ã®åã«ãã¼ã¿ã½ã¼ã¹ãå®ç¾©ãã¾ãã
data "aws_ami" "web" { ##... } data "aws_availability_zones" "available" { ##... } resource "aws_instance" "web" { ami = data.aws_ami.web.id availability_zone = data.aws_availability_zones.available.names[0] ##... }
å¤æ°(ãã©ã¡ã¼ã¿ã¼)
å¤æ°ã¯ã¢ã¸ã¥ã¼ã«ãããæè»ã«ãã¾ãããå¤æ°ãéå°ã«ä½¿ç¨ããã¨ãã³ã¼ããç解ãã«ããå ´åãããã¾ãã ãªã½ã¼ã¹è¨å®ã®å¤æ°ãå ¬éãããã©ããã決å®ããã¨ãã¯ããã®ãã©ã¡ã¼ã¿ã¼ãå±éå ã§å¤æ´ããããã©ãããèæ ®ããã¹ãã§ãã
- ãã¹ã¦ã®å¤æ°ã«ã¯ã¿ã¤ãã¨èª¬æãå®ç¾©ãã¾ãã
- å¤æ°ããªãã·ã§ã³ã®å ´åã妥å½ãªããã©ã«ããå®ç¾©ãã¾ãã
- ãã¹ã¯ã¼ãããã©ã¤ãã¼ããã¼ãªã©ã®æ©å¯å¤æ°ã®å ´åã
sensitive
ãTRUEã«è¨å®ãã¾ãããã Terraformã¯ããã®å¤ããã¬ã¼ã³ããã¹ãã«ä¿åãã¦ãããã¨ã念é ã«ããã¦ããã¦ãã ããã ãã ããterraform plan
ã¾ãã¯terraform apply
ãå®è¡ãã¦ãsensitive
ãè¨å®ããå¤ã¯è¡¨ç¤ºãããªããªãã¾ãã å¤æ°ãããã¯ã®è¨è¿°ã®é çªã¯æ¬¡ã®ããã«ãããã¨ããå§ããã¾ãã
Type
- Description
- Default (optional)
- Sensitive (optional)
- Validation blocks
Outputs
Outputãé©åã«è¨å®ãããã¨ã«ãã£ã¦ãã¦ã¼ã¶ã¼ã¯ã³ãã³ãã©ã¤ã³ã§ã¤ã³ãã©ã¹ãã©ã¯ãã£ã«é¢ãããã¼ã¿ã表示ãããã¨ãã§ããä»ã®ãã©ãã©ã¼ã æ§æã§ç°¡åã«åç §ã§ãã¾ãã
Outputãã©ã¡ã¼ã¿ã¼ã«ã¯ã次ã®é çªã§ã®ä½¿ç¨ãå§ããã¾ãã
- Description
- Value
- Sensitive (optional)
ãã¹ã¦ã®å¤æ°ã¨Outputã«ã¯ä¸æã®ååãå¿ è¦ã§ãã Outputåã«ã¯ä¸è²«æ§ã¨èªã¿ãããã®ããã«ã説æçãªåè©ã使ç¨ããã¢ã³ãã¼ã¹ã³ã¢ã§å¥ã ã®åèªã使ç¨ãããã¨ããå§ããã¾ãã
variable "db_disk_size" { type = number description = "Disk size for the API database" default = 100 } variable "db_password" { type = string description = "Database password" sensitive = true } output "web_public_ip" { description = "Public IP of the web instance" value = aws_instance.web.public_ip }
Local values
Terraformã§ã¯ãã¼ã«ã«ã®å¤ã«ãããå¼ã¾ãã¯å¤ãè¤æ°ååç §ã§ãã¾ãã ãã¼ã«ã«å¤ãéå°ä½¿ç¨ãã³ã¼ãã®è¤éåã«ã¤ãªããããããã¼ã«ã«ã®ä¾¡å¤ãæ§ããã«ä½¿ç¨ãã¾ãã
ãã¨ãã°ããã¼ã«ã«å¤ã使ç¨ãã¦ããªã¼ã¸ã§ã³ã¨ç°å¢ï¼devãtestãªã©ï¼ã®ãµãã£ãã¯ã¹ãä½æããè¤æ°ã®ãªã½ã¼ã¹ã«è¿½å ã§ãã¾ãã
locals { name_suffix = "${var.region}-${var.environment}" } resource "aws_instance" "web" { ami = data.aws_ami.ubuntu.id instance_type = "t3.micro" tags = { Name = "web-${local.name_suffix}" } }
2ã¤ã®å ´æã®ããããã§ãã¼ã«ã«å¤ãå®ç¾©ãããã¨ããå§ããã¾ãã
- è¤æ°ã®ãã¡ã¤ã«ã§ãã¼ã«ã«å¤ãåç
§ããå ´åã¯ã
locals.tf
ã¨ããååã®ãã¡ã¤ã«ã§å®ç¾©ãã¾ãã - ãã¼ã«ã«å¤ãä¸ã¤ã®ãã¡ã¤ã«ã«åºæã®å ´åã¯ããã®ãã¡ã¤ã«ã®ä¸é¨ã«å®ç¾©ãã¾ãã
ãããã¤ãã¼ã¨ã¤ãªã¢ã·ã³ã°
ãããã¤ãã¼ã¨ã¤ãªã¢ã·ã³ã°ã§ã¯ãåãTerraformãããã¤ãã¼ã®è¤æ°ã®ãããã¤ãã¼ãããã¯ãå®ç¾©ã§ãã¾ãã ä¾ãã°ãawsã®è¤æ°ãªã¼ã¸ã§ã³ã§ãµã¼ãã¹ããããã¤ãããæããããã¯awsãªã½ã¼ã¹ãç¹å®ã®ãªã¼ã¸ã§ã³ã«ãã対å¿ãã¦ããªãæã¯ãããã¤ãã¼ã¨ã¤ãªã¢ã·ã³ã°ãæ´»ç¨ã§ãã¾ãã
providers.tf
provider "aws" { region = "us-east-1" } provider "aws" { alias = "west" region = "us-west-2" }
ä¸è¨ã®providers.tf
ãã¡ã¤ã«ã§ãããã¤ãã¼ãè¤æ°é¸æãã¦ãã¾ãã
main.tf
resource "aws_instance" "example" { provider = aws.west # ... } module "aws_vpc" { source = "./aws_vpc" providers = { aws = aws.west } }
ã©ã®ãããã¤ãã¼ã使ç¨ãããã¯åãªã½ã¼ã¹ã®provider
å¼æ°ã§æå®ã§ãã¾ãã
count, for_each
for_eachããã³ã«ã¦ã³ãã¡ã¿ã¢ã¼ã°ã¡ã³ãã使ç¨ããã¨ãå®è¡æã®æ¡ä»¶ã«å¿ãã¦ãåä¸ã®ãªã½ã¼ã¹ãããã¯ããè¤æ°ã®ãªã½ã¼ã¹ãä½æã§ãã¾ãããããã®ã¡ã¿ã¢ã¼ã°ã¡ã³ãã使ç¨ãã¦ãã³ã¼ããæè»ã«ããéè¤ãããªã½ã¼ã¹ãããã¯ãåæ¸ã§ãã¾ãããªã½ã¼ã¹ãã»ã¼åä¸ã®å ´åã¯ãã«ã¦ã³ãã使ç¨ãã¦ãã ãããããã¤ãã®å¼æ°ã«ã¯ãæ´æ°ããæ´¾çã§ããªãæ確ãªå¤ãå¿ è¦ãªå ´åã¯ãfor_eachã使ç¨ãã¦ãã ããã
for_each meta-argumentã¯ãããã¾ãã¯ã»ããå¤ãåãå ¥ããTerraformã¯æä¾ããå¤ã®åè¦ç´ ã®ãã®ãªã½ã¼ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãä½æãã¾ãã次ã®ä¾ã§ã¯ãterraformã¯ãweb_instanceså¤æ°ã§å®ç¾©ããã¦ããåæååã®aws_instanceãä½æãã¾ãï¼ "ui"ã "api"ã "db"ã "metrics"ããã®ä¾ã§ã¯ãåã¤ã³ã¹ã¿ã³ã¹ã«åã¤ã³ã¹ã¿ã³ã¹ã«ä¸æã®ååãä»ãã¦ãã ãããweb_private_ipsåºåã¯expressionã使ç¨ãã¦ã¤ã³ã¹ã¿ã³ã¹åã¨ãã®ãã©ã¤ãã¼ãIPã¢ãã¬ã¹ã®ããããä½æããweb_ui_public_ipåºåã¯ãã¼ãUIããç´æ¥ä½¿ç¨ãã¦ã¤ã³ã¹ã¿ã³ã¹ãã¢ãã¬ã¹æå®ãã¾ãã
variable "web_instances" { type = list(string) description = "A list of instances for the web application" default = [ "ui", "api", "db", "metrics" ]}resource "aws_instance" "web" { for_each = toset(var.web_instances) ami = data.aws_ami.webapp.id instance_type = "t3.micro" tags = { Name = "web_${each.key}" }}output "web_private_ips" { description = "Private IPs of the web instances" value = { for k, v in aws_instance.web : k => v.private_ip }}output "web_ui_public_ip" { description = "Public IP of the web UI instance" value = aws_instance.web["ui"].public_ip}
ä¸è¨ã®ä¾ã§ã¯ã次ã®åºåãä½æããã¾ãã
web_private_ips = { "api" = "172.31.25.29" "db" = "172.31.18.33" "metrics" = "172.31.26.112" "ui" = "172.31.20.142"}web_ui_public_ip = "18.216.208.182"
ãã®ä»ã®ä¾ã«ã¤ãã¦ã¯ãfor_eachã¡ã¿ã¢ã¼ã°ã¡ã³ãããã¥ã¡ã³ããåç §ãã¦ãã ããã
Count Meta-Argumentã使ç¨ããã¨ãåä¸ã®ãªã½ã¼ã¹ãããã¯ãããªã½ã¼ã¹ã®è¤æ°ã®ã¤ã³ã¹ã¿ã³ã¹ãä½æã§ãã¾ããä¾ã«ã¤ãã¦ã¯ãCount Meta-Argumentããã¥ã¡ã³ããåç §ãã¦ãã ããã
æ¡ä»¶ä»ããªã½ã¼ã¹ãä½æããä¸è¬çãªæ £è¡ã¯ãæ¡ä»¶ä»ãå¼ã§ã«ã¦ã³ãã¡ã¿ã¢ã¼ãã³ã°ã使ç¨ãããã¨ã§ãã次ã®ä¾ã§ã¯ãvar.enable_metricsãçã§ããå ´åã«ã®ã¿ãterraformãAWS_INSTANCEãä½æãã¾ãã
variable "enable_metrics" { description = "True if the metrics server should be deployed" type = bool default = true}resource "aws_instance" "web" { count = var.enable_metrics ? 1 : 0 ami = data.aws_ami.webapp.id instance_type = "t3.micro" ##...}
ã¡ã¿ã¢ã¼ã°ã¡ã³ãã¯ã³ã¼ããç°¡ç´ åãã¾ãããè¤éãã追å ãããããé©åº¦ã«ä½¿ç¨ãã¦ãã ãããã¡ã¿ã¢ã¼ãã¥ã¡ã³ãã®å¹æãããã«æããã§ãªãå ´åã¯ãæ確åã®ããã«ã³ã¡ã³ãã使ç¨ãã¦ãã ããã
ãããã®ã¡ã¿ã¢ã¼ã°ã¡ã³ãã®è©³ç´°ã«ã¤ãã¦ã¯ãfor_eachããã³ã«ã¦ã³ãããã¥ã¡ã³ããåç §ãã¦ãã ããã
.gitignore
ãªãã¸ããªã®.gitignore
ãã¡ã¤ã«ãå®ç¾©ãã¦ãstateãã¡ã¤ã«ãªã©ã®ããã¸ã§ã¯ããã¼ã¸ã§ã³ã³ã³ããã¼ã«ã«å
¬éãã¹ãã§ã¯ãªããã¡ã¤ã«ãé¤å¤ãã¾ãããã
- 以ä¸ã®ã³ã¼ãã¯çµ¶å¯¾ã«ã³ãããããªãã§ãã ããã
terraform.tfstate
ãå«ãterraform.tf.*
ã¹ãã¼ããã¡ã¤ã«ã.terraform.tfstate.lock.info
ãã¡ã¤ã«ã- Terraformã¯ã
terraform apply
ã³ãã³ããå®è¡ããã¨ããã®ãã¡ã¤ã«ãèªåçã«ä½æããã³åé¤ããããã¯ã管çãã¾ãã
- Terraformã¯ã
.terraform
ãã£ã¬ã¯ããªãTerraformã¯ãã®ãã£ã¬ã¯ããªå ã«ãããã¤ãã¼ã¨åã¢ã¸ã¥ã¼ã«ããã¦ã³ãã¼ããã¾ãã- æ©å¯æ
å ±ãå«ã
.tfvars
ãã¡ã¤ã«ã
- 常ã«ã³ãããï¼
- ãã¹ã¦ã®Terraformã³ã¼ããã¡ã¤ã«
.terraform.lock.hcl
: ä¾åé¢ä¿ããã¯ãã¡ã¤ã«- ã³ã¼ããå ¥åå¤æ°ãããã³åºåã説æããreadme.md
以ä¸ã¯çæ³çãª.gitignore
ãã¡ã¤ã«ã®ä¾ã§ãã
# Local .terraform directories **/.terraform/* # .tfstate files *.tfstate *.tfstate.* # Crash log files crash.log crash.*.log # Exclude all .tfvars files, which are likely to contain sensitive data, such as # password, private keys, and other secrets. These should not be part of version # control as they are data points which are potentially sensitive and subject # to change depending on the environment. *.tfvars *.tfvars.json # Ignore override files as they are usually used to override resources locally and so # are not checked in override.tf override.tf.json *_override.tf *_override.tf.json # Ignore transient lock info files created by terraform apply .terraform.tfstate.lock.info # Include override files you do wish to add to version control using negated pattern # !example_override.tf # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan # example: *tfplan* # Ignore CLI configuration files .terraformrc terraform.rc
åè : https://github.com/github/gitignore/blob/main/Terraform.gitignore
page:https://minegishirei.hatenablog.com/entry/2024/06/11/202050