본문 바로가기

퍼블릭 클라우드

[Terraform] 테라폼 ec2 인스턴스 생성

반응형

ec2 인스턴스 생성

(선행 작업)

- key pair 생성

- security group 생성

- ./script/install_web1.sh 스크립트 생성

instance.tf 파일 생성

$ vim instance.tf
resource "aws_instance" "web31" {
    ami                         = "ami-047a51fa27710816e"
    instance_type               = "t2.micro"
    #ebs_optimized               = true
    #monitoring                  = true
    #count                       = 1
    key_name                    = aws_key_pair.aws_key_4wxyz.key_name
    associate_public_ip_address = true
    # subnet_id                   = aws_subnet.sangchul_vpc11-sb3.id
    vpc_security_group_ids      = [
        aws_security_group.sangchul_vpc11-ec2-web1.id,
        aws_security_group.sangchul_vpc11-ec2-office1.id
    ]
    user_data = file("./script/install_web1.sh")
    tags = {
        Name                = "web31"
        Env                 = "stg"
        CreateUser          = "terraform@email.com"
        Owner               = "iac"
        Role                = "web"
        Service             = "aws_instance"
    }
    root_block_device {
        volume_type         = "gp2"
        volume_size         = 30
        tags = {
            Name            = "web31"
            Env             = "stg"
            CreateUser      = "terraform@email.com"
            Owner           = "iac"
            Role            = "web"
            Service         = "root_block_device"
        }
    }
    # ebs_block_device {
    #     device_name             = "/dev/xvda"
    #     volume_size             = 30
    #     volume_type             = "gp2"
    #     delete_on_termination   = false
    #     tags = {
    #         Name                = "web31"
    #         Env                 = "stg"
    #         CreateUser          = "terraform@email.com"
    #         Owner               = "iac"
    #         Role                = "web"
    #         Service             = "ebs_block_device"
    #     }
    # }
}

install_web1.sh 파일 생성

$ vim script/install_web1.sh
#! /bin/bash
sudo yum install -y httpd
sudo systemctl --now enable httpd
echo "<h1>sangchul.kr page</h1>" | sudo tee /var/www/html/index.html

terraform apply 명령 실행

$ terraform apply

 

서비스 확인

$ nc -zvw10 111.111.111.111 80
Connection to 34.227.31.20 port 80 [tcp/http] succeeded!

 

$ curl 111.111.111.111 

<h1>sangchul.kr page</h1>

 

 

728x90
반응형