다음은 주말 CloudNet 테라폼 스터디 내용 참고하여 정리한 부분입니다.
https://gasidaseo.notion.site/gasidaseo/CloudNet-Blog-c9dfa44a27ff431dafdd2edacc8a1863
<1> main.tf 삭제시 = 만들어진 리소스가 삭제된다.
<2> terraform.tfstate 삭제시 = 이미 만들어져 있는데 또 만들려고 한다.
<3> 도전과제 1 , 2
<1> main.tf 삭제시 = 만들어진 리소스가 삭제된다.
1
별도 폴더 만든기
mkdir change
2
# 코드 파일 생성
cat <<EOF > provider.tf
provider "aws" {
region = "ap-northeast-2"
}
EOF
3
# 초기화
terraform init
4
# 코드 파일 생성 : S3
NICK=master-seo
cat <<EOF > main.tf
resource "aws_s3_bucket" "t101study" {
bucket = "$NICK-t101study-bucket"
}
EOF
5
cat main.tf
6
terraform plan
7
# [터미널 2] 모니터링
while true; do aws s3 ls ; echo "------------------------------"; date; sleep 1; done
8
terraform apply -auto-approve
9
작업 결과가 localPC에 .tfstate 파일에 저장
ls -al
cat terraform.tfstate
terraform plan
10
상태가 달라지는 경우 - main.tf 삭제시?
버킷이 날라간다.
terraform.tfstate에 상태는 남아 있으나 main.tf파일이 없으면 버킷이 없어진다. (주의)
# terraform plan
# aws_s3_bucket.t101study will be destroyed
1 to destroy.
terraform apply -auto-approve
<2> terraform.tfstate 삭제시 = 이미 만들어져 있는데 또 만들려고 한다.
기존에 있기 때문에 만들어지지는 않는다.
1
버킷을 다시 생성
cat <<EOF > main.tf
resource "aws_s3_bucket" "t101study" {
bucket = "$NICK-t101study-bucket-2"
}
EOF
cat main.tf
terraform plan
terraform apply -auto-approve
cat terraform.tfstate
2
terraform.tfstate 삭제
[root@ip-172-31-61-209 change]# rm -rf terraform.tfstate
3
플랜하면, 만든다고 한다.
플랜은 상태를 모르므로 , tf파일을 참고해서 만들려고 한다.
그러나, 리소스는 이미 만들어져 있으므로 중복 오류가 생긴다.
[root@ip-172-31-61-209 change]# terraform plan
# aws_s3_bucket.t101study will be created
Plan: 1 to add, 0 to change, 0 to destroy.
4
terraform apply -auto-approve
│ Error: creating Amazon S3 (Simple Storage) Bucket (master-seo-t101study-bucket-2): BucketAlreadyOwnedByYou: Your previous request to create the named bucket succeeded and you already own it.
5
테라폼상태 파일을 보면 리소스가 없다.
그래서, 실제 리소스를 상태파일로 만드는게 필요하다. import이다.
# cat terraform.tfstate
{
"version": 4,
"terraform_version": "1.3.2",
"serial": 2,
"lineage": "015453e3-dfaa-7ecf-8765-63c3ea0905f7",
"outputs": {},
"resources": [],
"check_results": []
}
terraform import aws_s3_bucket.t101study $NICK-t101study-bucket-2
cat terraform.tfstate
6
재적용
동기화 되었으니 변경 되는게 없다.
terraform apply -auto-approve
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and
found no differences, so no changes are needed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
100
삭제
terraform destroy -auto-approve
<3> 도전과제 1 , 2
1
VPC등 네트워크 만들고 생성하기
2
테라폼 가시화 툴 pluralith 를 통해서 EC2 웹 서버 배포
다음
https://brunch.co.kr/@topasvga/2762
https://brunch.co.kr/@topasvga/2421
감사합니다.