๐Ÿ“‚ IaC/Terraform

Terraform 101 Study - 2์ฃผ์ฐจ(2) ๋„์ „ ๊ณผ์ œ

dhyuck 2023. 7. 14. 23:58
๋ฐ˜์‘ํ˜•
  • CloudNeta์˜ ๊ฐ€์‹œ๋‹ค๋‹˜์ด ์ง„ํ–‰ํ•˜์‹œ๋Š” Terraform 101 Study์— ์ฐธ๊ฐ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
  • ์Šคํ„ฐ๋”” ๊ต์žฌ โ†’ ํ…Œ๋ผํผ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” IaC

[2์ฃผ์ฐจ ๋„์ „๊ณผ์ œ] 1. ๋ฐ์ดํ„ฐ ์†Œ์Šค ์‹ค์Šต


  • ๋ฆฌ์ „ ๋‚ด์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐ€์šฉ์˜์—ญ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
data "aws_availability_zones" "available" {
  state = "available"
}

output "available_az_names" {
  value = data.aws_availability_zones.available.names
}

[2์ฃผ์ฐจ ๋„์ „๊ณผ์ œ] 2. ๋ฆฌ์†Œ์Šค ์œ ํ˜•๊ณผ ์ด๋ฆ„์˜ ์ฐจ์ด


  • vpc์™€ subnet์„ ๋ฐฐํฌํ•˜๋ฉด์„œ ๋ฆฌ์†Œ์Šค ์œ ํ˜•๊ณผ ์ด๋ฆ„์˜ ์ฐจ์ด๋ฅผ ์ดํ•ด
  • ๋ฆฌ์†Œ์Šค ๋ธ”๋Ÿญ์€ โ€œ<ํ”„๋กœ๋ฐ”์ด๋”>_<๋ฆฌ์†Œ์Šค ์œ ํ˜•>โ€ โ€œ<์ด๋ฆ„>โ€์œผ๋กœ ์„ ์–ธ
  • <ํ”„๋กœ๋ฐ”์ด๋”>_<๋ฆฌ์†Œ์Šค ์œ ํ˜•>.<์ด๋ฆ„>.<์†์„ฑ>์œผ๋กœ ์ฐธ์กฐ
provider "aws" {
  region = "ap-northeast-2"
}

resource "aws_vpc" "hyuckang_vpc" {
  cidr_block           = "10.10.0.0/16"
  enable_dns_support   = true
  enable_dns_hostnames = true

  tags = {
    Name = "t101-hyuckang-vpc"
  }
}

resource "aws_subnet" "hyuckang_subnet_1" {
  vpc_id     = aws_vpc.hyuckang_vpc.id
  cidr_block = "10.10.1.0/24"

  availability_zone = "ap-northeast-2a"

  tags = {
    Name = "t101-hyuckang_subnet_1"
  }
}

resource "aws_subnet" "hyuckang_subnet_2" {
  vpc_id     = aws_vpc.hyuckang_vpc.id
  cidr_block = "10.10.2.0/24"

  availability_zone = "ap-northeast-2c"

  tags = {
    Name = "t101-hyuckang_subnet-2"
  }
}

output "aws_vpc_id" {
  value = aws_vpc.hyuckang_vpc.id
}

[2์ฃผ์ฐจ ๋„์ „๊ณผ์ œ] 3. ์ž…๋ ฅ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ


  • ์ž…๋ ฅ ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ EC2์— Name ํƒœ๊ทธ ์ ์šฉ
variable "name_tag" {
  default = "hyuckang"
}

resource "aws_instance" "variable_test" {
  instance_type = "t3.micro"
  ami           = "ami-0c9c942bd7bf113a2"
  tags = {
    Name = var.name_tag
  }
}

output "variable_test_instance_arn" {
  value = aws_instance.variable_test.arn
}

[2์ฃผ์ฐจ ๋„์ „๊ณผ์ œ] 4. local์„ ํ™œ์šฉํ•œ ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ


  • ํ”„๋กœ๋ฐ”์ด๋”์— ๊ตฌ์„ฑ๋œ current ๋ฆฌ์ „์„ local์—์„œ ์ฐธ์กฐํ•˜์—ฌ region ํƒœ๊ทธ ์ ์šฉ
# provider์— ๊ตฌ์„ฑ๋œ region์„ ํ™•
locals {
  regions = {
    "ap-northeast-1" = "tokyo"
    "ap-northeast-2" = "seoul"
    "ap-northeast-3" = "osaka"
  }
}

data "aws_region" "current" {}

resource "aws_instance" "locals_test" {
  instance_type = "t3.micro"
  ami           = "ami-0c9c942bd7bf113a2"

  tags = {
    "Name"   = "locals_test"
    "region" = local.regions[data.aws_region.current.name]
  }
}

[2์ฃผ์ฐจ ๋„์ „๊ณผ์ œ] 5. ๋ฐ˜๋ณต๋ฌธ์„ ํ™œ์šฉํ•œ ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ


  • dynamic์„ ํ™œ์šฉํ•˜์—ฌ sg ๋‚ด์—์„œ sg rule ๋ฐ˜๋ณต ์ƒ์„ฑ
variable "ingress_rules" {
  default = [
    { protocol = "tcp", port = 80, cidr_blocks = ["0.0.0.0/0"], description = "http" },
    { protocol = "tcp", port = 443, cidr_blocks = ["0.0.0.0/0"], description = "https" },
    { protocol = "tcp", port = 22, cidr_blocks = ["0.0.0.0/0"], description = "ssh" }
  ]
}

resource "aws_security_group" "dynamic_test" {
  name        = "dynamic_test"
  description = "A security group for terraform dynamic test"

  dynamic "ingress" {
    for_each = var.ingress_rules
    content {
      protocol    = ingress.value.protocol
      from_port   = ingress.value.port
      to_port     = ingress.value.port
      cidr_blocks = ingress.value.cidr_blocks
      description = ingress.value.description
    }
  }
}

๋ฐ˜์‘ํ˜•