- 4-3. workspace 실습2024년 07월 06일
- yeongki0944
- 작성자
- 2024.07.06.:45
1. tf 코드 - main.tf
provider "aws" { region = "us-east-2" } resource "aws_vpc" "example" { cidr_block = "10.0.0.0/16" tags = { Name = "week4-vpc-${terraform.workspace}-example" } } resource "aws_subnet" "example" { vpc_id = aws_vpc.example.id cidr_block = "10.0.1.0/24" tags = { Name = "week4-subnet-${terraform.workspace}-example" } } resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI instance_type = "t2.micro" subnet_id = aws_subnet.example.id tags = { Name = "week4-ec2-${terraform.workspace}-example" } } variable "instance_type" { description = "Type of the instance" type = string default = "t2.micro" } output "instance_id" { value = aws_instance.example.id } output "instance_public_ip" { value = aws_instance.example.public_ip }
2. terraform init
terraform init
3. terraform workspace
terraform workspace new dev terraform workspace new prod
local backend workspace 4. dev 워크스페이스에 배포
terraform workspace select dev terraform workspace show terraform apply -auto-approve
terraform.tfstate.d > dev > terraform.tfstate 파일 생성 5. prod 워크스페이스에 배포
terraform workspace select prod terraform workspace show terraform apply -auto-approve
terraform.tfstate.d > prod > terraform.tfstate 파일 생성 6. terraform cli에서 workspace 명시하는 방법
terraform destroy, apply 명령어 옵션에서 직접 워크스페이스를 지정하는 옵션은 없습니다.
cli에서 워크스페이스를 명시하기 위해선, 아래와 같은 && 연산을 통해 확실하게 특정 워크스페이스를 지정하여 apply, destroy가 가능합니다.
terraform workspace select dev && terraform destroy -auto-approve
terraform workspace select prod && terraform destroy -auto-approve
다음글이전글이전 글이 없습니다.댓글
스킨 업데이트 안내
현재 이용하고 계신 스킨의 버전보다 더 높은 최신 버전이 감지 되었습니다. 최신버전 스킨 파일을 다운로드 받을 수 있는 페이지로 이동하시겠습니까?
("아니오" 를 선택할 시 30일 동안 최신 버전이 감지되어도 모달 창이 표시되지 않습니다.)