# 1주차 과제 - AWS VPC(Subnet, IGW 등)을 코드로 배포한 환경에서 EC2 웹 서버 배포 cat < main.tf # provider 설정 provider "aws" { region = "ap-northeast-2" } # vpc 생성 resource "aws_vpc" "vpc" { cidr_block = var.vpc_cidr enable_dns_hostnames = true enable_dns_support = true tags = { "Name" = "vpc" } } # public subnet 생성 resource "aws_subnet" "pub1" { vpc_id = aws_vpc.vpc.id cidr_block = var.public_cidr availability_zone = "ap-northeast-2a" map_public_ip_on_launch = true tags = { Name = "pub1" } } # igw 생성 resource "aws_internet_gateway" "igw" { vpc_id = aws_vpc.vpc.id tags = { Name = "igw" } } # public route table 생성 및 igw로의 라우팅 설정 resource "aws_route_table" "pubrt" { vpc_id = aws_vpc.vpc.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.igw.id } tags = { Name = "pubrt" } } # 라우팅 테이블과 서브넷 연결 resource "aws_route_table_association" "prta" { subnet_id = aws_subnet.pub1.id route_table_id = aws_route_table.pubrt.id } EOT cat < var.tf variable "vpc_cidr" { default = "10.0.0.0/16" } variable "public_cidr" { default = "10.0.10.0/24" } data "aws_ami" "ubuntu" { most_recent = true filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] } filter { name = "virtualization-type" values = ["hvm"] } owners = ["099720109477"] # Canonical } variable "server_port" { default = "50000" } EOT terraform init terraform plan terraform apply -auto-approve terraform init terraform plan terraform apply -auto-approve