Terraform

terraform code (ec2)

KK_Ryong 2023. 8. 31. 12:15

아래 처럼 할 경우 단점이 apply 시 lifecycle 설정을 안해두면 지워졌다 생기므로 

구축 완료 후 lifecycle  ignore_changes = all로 업데이트 방지 해야 함 

----------------------------------------------------------------------------------------------------------------

resource "aws_instance" "projent-stg-bastion" {
    ami                         = "ami-055179a7fc9fb032d"
    availability_zone           = "ap-northeast-2a"
    ebs_optimized               = false
    instance_type               = "t2.micro"
    monitoring                  = false
    key_name                    = "projent-stg"
    count                       = 1
    subnet_id                   = aws_subnet.projent-stg-subnet-public-ap-northeast-2a[count.index].id
    security_groups             = [aws_security_group.projent-stg-bastion-sg.id]
    associate_public_ip_address = false
    source_dest_check           = true
    root_block_device {
        volume_type           = "gp3"
        volume_size           = 8
        delete_on_termination = true
    }
    lifecycle {
        #create_before_destroy = true
        #prevent_destroy = true  #삭제 방지 (오류로 떨어짐)
        ignore_changes = all   #업데이트 방지
    }
    tags = {
        Name = "projent-stg-bastion"
        auto-schedule = "false"
    }
    
}
resource "aws_eip" "bastion" {
  count = 1
  instance = "${aws_instance.projent-stg-bastion[count.index].id}"
  vpc      = true
  tags = {
        Name = "projent-stg-bastion"
    }
}
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_instance" "projent-stg-jenkins" {
    ami                         = "ami-0cb6865d759254be2"
    availability_zone           = "ap-northeast-2c"
    ebs_optimized               = false
    instance_type               = "t3.medium"
    monitoring                  = false
    key_name                    = "projent-stg"
    count                       = 1
    subnet_id                   = aws_subnet.projent-stg-subnet-private-ap-northeast-2a[1].id
    security_groups             = [aws_security_group.projent-stg-jenkins.id]
    associate_public_ip_address = false
    source_dest_check           = true
    root_block_device {
        volume_type           = "gp3"
        volume_size           = 20
        delete_on_termination = true
    }
    lifecycle {
        #create_before_destroy = true
        #prevent_destroy = true  #삭제 방지 (오류로 떨어짐)
        ignore_changes = all   #업데이트 방지
    }
    tags = {
        Name = "projent-stg-jenkins-2c"
        auto-schedule = "false"
    }
}
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_instance" "projent-stg-prometheus" {
    ami                         = "ami-04260b2ecff1bbe5e"
    availability_zone           = "ap-northeast-2c"
    ebs_optimized               = false
    instance_type               = "t2.micro"
    monitoring                  = false
    key_name                    = "projent-stg"
    count                       = 1
    subnet_id                   = aws_subnet.projent-stg-subnet-private-ap-northeast-2a[1].id
    security_groups             = [aws_security_group.projent-stg-prometheus-sg.id]
    associate_public_ip_address = false
    source_dest_check           = true
    root_block_device {
        volume_type           = "gp3"
        volume_size           = 20
        delete_on_termination = true
    }
    lifecycle {
        #create_before_destroy = true
        #prevent_destroy = true  #삭제 방지 (오류로 떨어짐)
        ignore_changes = all   #업데이트 방지
    }
    tags = {
        Name = "projent-stg-prometheus-2c"
        auto-schedule = "false"
    }
}
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_instance" "projent-stg-grafana" {
    ami                         = "ami-0018a7fa5c79cbf47"
    availability_zone           = "ap-northeast-2c"
    ebs_optimized               = false
    instance_type               = "t2.micro"
    monitoring                  = false
    key_name                    = "projent-stg"
    count                       = 1
    subnet_id                   = aws_subnet.projent-stg-subnet-private-ap-northeast-2a[1].id
    security_groups             = [aws_security_group.projent-stg-grafana-sg.id]
    associate_public_ip_address = false
    source_dest_check           = true
    root_block_device {
        volume_type           = "gp3"
        volume_size           = 10
        delete_on_termination = true
    }
    lifecycle {
        #create_before_destroy = true
        #prevent_destroy = true  #삭제 방지 (오류로 떨어짐)
        ignore_changes = all   #업데이트 방지
    }
    tags = {
        Name = "projent-stg-grafana-2c"
        auto-schedule = "false"
    }
}
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_instance" "projent-stg-chatting" {
    ami                         = "ami-055179a7fc9fb032d"
    availability_zone           = "ap-northeast-2c"
    ebs_optimized               = false
    instance_type               = "t2.micro"
    monitoring                  = false
    key_name                    = "projent-stg"
    count                       = 1
    subnet_id                   = aws_subnet.projent-stg-subnet-private-ap-northeast-2a[1].id
    security_groups             = [aws_security_group.projent-stg-chatting-sg.id]
    associate_public_ip_address = false
    source_dest_check           = true
    root_block_device {
        volume_type           = "gp3"
        volume_size           = 10
        delete_on_termination = true
    }
    lifecycle{
        #create_before_destroy = true
        #prevent_destroy = true  #삭제 방지 (오류로 떨어짐)
        ignore_changes = all   #업데이트 방지
    }
    tags = {
        Name = "projent-stg-chatting-2c"
        auto-schedule = "false"
    }
}
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_instance" "projent-stg-thumbor" {
    ami                         = "ami-055179a7fc9fb032d"
    availability_zone           = "ap-northeast-2c"
    ebs_optimized               = false
    instance_type               = "t2.micro"
    monitoring                  = false
    key_name                    = "projent-stg"
    count                       = 1
    subnet_id                   = aws_subnet.projent-stg-subnet-private-ap-northeast-2a[1].id
    security_groups             = [aws_security_group.projent-stg-thumbor-sg.id]
    associate_public_ip_address = false
    source_dest_check           = true
    root_block_device {
        volume_type           = "gp3"
        volume_size           = 20
        delete_on_termination = true
    }
    lifecycle{
        #create_before_destroy = true
        #prevent_destroy = true  #삭제 방지 (오류로 떨어짐)
        ignore_changes = all   #업데이트 방지
    }
    tags = {
        Name = "projent-stg-thumbor-2c"
        auto-schedule = "false"
    }
}
#■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
resource "aws_instance" "projent-stg-elk" {
    ami                         = "ami-055179a7fc9fb032d"
    availability_zone           = "ap-northeast-2c"
    ebs_optimized               = false
    instance_type               = "t3.small"
    monitoring                  = false
    key_name                    = "projent-stg"
    count                       = 1
    subnet_id                   = aws_subnet.projent-stg-subnet-private-ap-northeast-2a[1].id
    security_groups             = [aws_security_group.projent-stg-elk-sg.id]
    associate_public_ip_address = false
    source_dest_check           = true
    root_block_device {
        volume_type           = "gp3"
        volume_size           = 20
        delete_on_termination = true
    }
    lifecycle{
        #create_before_destroy = true
        #prevent_destroy = true  #삭제 방지 (오류로 떨어짐)
        ignore_changes = all   #업데이트 방지
    }
    tags = {
        Name = "projent-stg-elk-2c"
        auto-schedule = "false"
    }
}

'Terraform' 카테고리의 다른 글

terraform code(tg)  (0) 2023.08.31
terraform code (alb)  (0) 2023.08.31
terraform code (network)  (0) 2023.08.31
Terraform AWS 구성 (ecs 기반)  (0) 2023.04.11
[Terraform] Auto Scaling  (0) 2022.03.16