CLOUD/IaC

[Terraform/AWS] Module 이용해서 EC2 생성 & 네트워크 설정

alsruds 2023. 3. 30. 17:57

 미리보기  

1. C drive 밑에 terraform 폴더 생성 > 밑에 module 폴더 생성 (VS code 에서 불러오기)
2. main.tf 생성 & provider 입력
3. vpc 폴더 생성 & 내부에 main.tf 생성
4. vpc - main.tf 에 만들어놨던 vpc&subnet&internet gateway&routing table code 넣기 (provider 제외 > main 에 있으니까)
5. main.tf 에 vpc module 불러오기

6. 새로운 모듈 ec2 (모듈 추가할 때마다 terraform init)
7. 위에서 만들었던 main.tf & variables.tf 가져오기 (provider 제외)
8. main.tf 에 ec2 module 불러오기

9. vpc - main.tf - 보안그룹 생성 코드
10. ec2 - main.tf - 보안그룹/서브넷 추가
          사용할 vpc 변수 설정
          1. main.tf 에서 vpc 변수 설정

          or
          2. ec2 - main.tf 에서 vpc 모듈 불러와서 변수 호출 (이걸 사용하겠어용)


 

 

기본 설정

2023.03.30 - [분류 전체보기] - [Terraform] 시작하기

 

[Terraform] 시작하기

♡ 미리보기 ♡ 1. 테라폼 홈페이지에서 다운로드 2. 경로 설정 3. aws configure : key 등록 4. vs code 에서 hasicope terraform 다운로드 ● 윈도우 환경에 설치 https://www.terraform.io/downloads ● cmd 창에서 편리하

alsrudalsrudalsrud.tistory.com

 

코드 참고

2023.03.30 - [분류 전체보기] - [Terraform/AWS] AWS EC2 생성하기

 

[Terraform/AWS] AWS EC2 생성하기

♡ 미리보기 ♡ 1. C drive 밑에 terraform 폴더 생성 > 밑에 ec2 폴더 생성 2. vs code 에서 ec2 폴더 열기 3. server.tf 파일 생성 4. code 입력 5. terminal 창 실행 6. terraform init (설정파일 생성) 7. terraform plan (code

alsrudalsrudalsrud.tistory.com

2023.03.30 - [분류 전체보기] - [Terraform/AWS] AWS 네트워크 생성 (VPC/Subnet/Internet Gateway/Routing Table)

 

[Terraform/AWS] AWS 네트워크 생성 (VPC/Subnet/Internet Gateway/Routing Table)

♡ 미리보기 ♡ 1. terraform 폴더 > vpc 폴더 생성 : vscode에서 불러오기 2. main.tf 파일 생성 3. terraform code 로 vpc 생성 > terraform init > terraform apply > terraform destroy ● 기본 설정 2023.03.30 - [분류 전체보기] -

alsrudalsrudalsrud.tistory.com

 

● VS code

# main.tf
provider "aws" {
  region = "ap-northeast-2"
}
// vpc 모듈 생성
module "module_vpc" {
    source = "./vpc"
}
// ec2 모듈 생성
module "module_ec2" {
    source = "./ec2"
    my_server_ami = "ami-068a0feb96796b48d"
    my_server_type = "t2.micro"
}

# vpc - main.tf
// vpc 생성
resource "aws_vpc" "terraform_vpc" {
    cidr_block = "200.200.0.0/16"
    enable_dns_hostnames = true
    tags = {
        Name = "terraform_vpc"
    }
}

// subnet 생성
resource "aws_subnet" "tvpc_sub1" {
    vpc_id = aws_vpc.terraform_vpc.id   //[resource].[resource name]
    cidr_block = "200.200.10.0/24"
    availability_zone = "ap-northeast-2a"
    map_public_ip_on_launch = true
    tags = {
        Name = "tvpc_sub1"
    }
}
resource "aws_subnet" "tvpc_sub2" {
    vpc_id = aws_vpc.terraform_vpc.id   //[resource].[resource name]
    cidr_block = "200.200.20.0/24"
    availability_zone = "ap-northeast-2b"
    map_public_ip_on_launch = true
    tags = {
        Name = "tvpc_sub2"
    }
}
resource "aws_subnet" "tvpc_sub3" {
    vpc_id = aws_vpc.terraform_vpc.id   //[resource].[resource name]
    cidr_block = "200.200.30.0/24"
    availability_zone = "ap-northeast-2c"
    map_public_ip_on_launch = true
    tags = {
        Name = "tvpc_sub3"
    }
}
resource "aws_subnet" "tvpc_sub4" {
    vpc_id = aws_vpc.terraform_vpc.id   //[resource].[resource name]
    cidr_block = "200.200.40.0/24"
    availability_zone = "ap-northeast-2d"
    map_public_ip_on_launch = true
    tags = {
        Name = "tvpc_sub4"
    }
}

// internet gateway 생성
resource "aws_internet_gateway" "terraform_ig" {
    vpc_id = aws_vpc.terraform_vpc.id
    tags = {
        Name = "terraform_ig"
    }
}

// routing table 생성
resource "aws_default_route_table" "terraform_route" {
    default_route_table_id = aws_vpc.terraform_vpc.default_route_table_id

    route {
        cidr_block = "0.0.0.0/0"
        gateway_id = aws_internet_gateway.terraform_ig.id
    }
    tags = {
        Name = "terraform_route"
    }
}

// 보안그룹 만들기
resource "aws_security_group" "terraform_sg" {
    vpc_id = aws_vpc.terraform_vpc.id
    ingress {
        description = "SSH"
        from_port = 22
        to_port = 22
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
    egress {
        from_port = 0
        to_port = 0
        protocol = "-1"
        cidr_blocks = ["0.0.0.0/0"]
    }
    tags = {
        Name = "terraform_sg"
    }

// 다른 모듈에서 불러올 수 있도록 output 설정
output "terraform_sg_id" {
  value = aws_security_group.terraform_sg.id
}

output "tvpc_sub1_id" {
  value = aws_subnet.tvpc_sub1.id
}
}

# ec2 - main.tf
// vpc 모듈 불러오기 (변수 사용하기 위해서)
module "module_vpc" {
  source = "../vpc"
}

resource "aws_instance" "myec2" {
    ami = var.my_server_ami
    instance_type = var.my_server_type

    vpc_security_group_ids = [ "${module.module_vpc.terraform_sg_id}" ]
    subnet_id = "${module.module_vpc.tvpc_sub1_id}"

    tags = {
        Name = "myec2"
    }
}

# ec2 - variables.tf
variable "my_server_ami" {
  type = string
}
variable "my_server_type" {
  type = string
}

 

● 실행

terraform init
terraform apply

 

 확인

완료~