책을 보며 필요한 부분을 정리해 본다.
1
명령서버 1대를 콘솔로 만들어도 된다.
Cloudfomation 으로 1대 만들어도 된다.
여기서는 Cloudfomation 으로 1대 만들자.
myeks2-bastion-EC2
myeks
ec2키페어 필요
access/secret-key 필요
2
terraform -version
1
테라폼 코드 필요
예시코드
https://github.com/terraform101
wget https://github.com/terraform101/terraform-basic/archive/refs/heads/main.zip
unzip main.zip
cd terraform-basic-main/
2
# 테라폼 사용
terraform init
terraform plan
terraform apply -auto-approve
terraform destroy -auto-approve
3
init
terraform init 은 테라폼 구성 파일이 있는 작업 디렉토리를 초기화 한다.
루트 모듈
프로바이더,모듈등 지정 버전에 맞춰 루트 모듈을 구성하는 역할을 한다.
4
# terraform init 하지 않고 , terraform plan 을 하면 에러가 난다.
terraform plan
[root@myeks2-bastion-EC2 ~]# terraform plan
╷
│ Error: No configuration files
│
│ Plan requires configuration to be present. Planning without a configuration would mark everything for destruction, which is normally not
│ what is desired. If you would like to destroy everything, run plan with the -destroy option. Otherwise, create a Terraform configuration
│ file (.tf file) and try again.
╵
5
# 코드가 없으면 초기화 안됨.
terraform init
Terraform initialized in an empty directory!
The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files
[root@myeks2-bastion-EC2 terraform-basic-main]# ls
3-10.tf 3-15.tf 3-1.tf 3-23.tf 3-29.tf 3-33.tf 3-37.tf 3-44.tf 3-4.tf 3-55.tf 3-59.tf 3-65.tf 3-76.tf
3-11.tf 3-16.tf 3-20.tf 3-24.tf 3-2.tf 3-34.tf 3-3.tf 3-47.tf 3-50.tf 3-56.tf 3-5.tf 3-66.tf 3-8.tf
3-12.tf 3-17.tf 3-21.tf 3-25.tf 3-30.tf 3-35.tf 3-43.tf 3-48.tf 3-52.tf 3-57.tf 3-61.tf 3-67.tf 3-9.tf
3-14.tf 3-19.tf 3-22.tf 3-26.tf 3-32.tf 3-36.tf 3-44-sub.tf 3-49.tf 3-53.tf 3-58.tf 3-62.tf 3-6.tf README.md
5
mkdir 1
cd 1
cp ../3-1.tf .
[root@myeks2-bastion-EC2 1]# terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# local_file.abc will be created
+ resource "local_file" "abc" {
+ content = "abc!"
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./abc.txt"
+ id = (known after apply)
}
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
local_file.abc: Creating...
local_file.abc: Creation complete after 0s [id=5678fb68a642f3c6c8004c1bdc21e7142087287b]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
[root@myeks2-bastion-EC2 1]# pwd
/root/terraform-basic-main/1
# 만들어진 파일
[root@myeks2-bastion-EC2 1]# ls
3-1.tf abc.txt terraform.tfstate
[root@myeks2-bastion-EC2 1]# more 3-1.tf
resource "local_file" "abc" {
content = "abc!"
filename = "${path.module}/abc.txt"
}
# 만들어진 파일 내용
[root@myeks2-bastion-EC2 1]# more abc.txt
abc!
6
rm -rf abc.txt
terraform apply -auto-approve
// 한줄 주석1
# 한줄 주석2
/*
라인
주석
*/
감사합니다.