본문 바로가기

퍼블릭 클라우드

Terraform으로 AWS VPC 생성하기

반응형

Terraform으로 AWS VPC 생성하기

1. VPC 리소스 정의

vim vpc.tf
resource "aws_vpc" "sangchul_vpc11" {
  cidr_block           = var.vpc_cidr
  enable_dns_support   = true
  enable_dns_hostnames = true
  instance_tenancy     = "default"

  tags = {
    Name        = var.vpc_name
    Environment = var.env
    Owner       = "iac"
    Service     = "network"
    Role        = "vpc"
    CreatedBy   = "Terraform"
  }
}

2. 변수 정의 (권장)

vim variables.tf
variable "vpc_cidr" {
  description = "VPC CIDR Block"
  default     = "10.11.0.0/16"
}

variable "vpc_name" {
  description = "VPC Name"
  default     = "sangchul_vpc11"
}

variable "env" {
  description = "Environment"
  default     = "stg"
}

3. 실행 절차

3.1 초기화

terraform init

3.2 실행 계획 확인 (권장)

terraform plan -out=tfplan

3.3 VPC 생성

terraform apply tfplan

또는 간단 실행

terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

...

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_vpc.sangchul_vpc11: Creating...
aws_vpc.sangchul_vpc11: Still creating... [10s elapsed]
aws_vpc.sangchul_vpc11: Creation complete after 12s [id=vpc]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
728x90

4. 생성 결과 확인

terraform show

또는 AWS CLI

aws ec2 describe-vpcs --filters "Name=tag:Name,Values=sangchul_vpc11"

5. Output 설정 (권장)

vim outputs.tf
output "vpc_id" {
  value = aws_vpc.sangchul_vpc11.id
}

output "vpc_cidr" {
  value = aws_vpc.sangchul_vpc11.cidr_block
}

6. 자동 생성 리소스 설명

VPC 생성 시 AWS에서 다음 리소스가 자동 생성됩니다

  • Default Route Table
  • Default Network ACL
  • Default Security Group

📌 추가 구성 필요:

  • Subnet
  • Internet Gateway (IGW)
  • Route Table Association
  • NAT Gateway (Private Subnet 사용 시)

7. 트러블슈팅

인증 오류

aws sts get-caller-identity

리소스 충돌

terraform refresh

 

728x90
반응형