brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Sep 29. 2024

테라폼-2024-09-29

테라폼으로 시작하는 IaC

책을 보며 필요한 부분을 정리해 본다.



<1> 준비 - AWS에 명령서버 1대 만들고 시작하자

<2> 테라폼 기초 알아보기

<3> 테라폼 주석  3가지

<4> 테라폼 블록 5가지



<1> 준비 - AWS에 명령서버 1대 만들고 시작하자


1

명령서버 1대를 콘솔로 만들어도 된다.

Cloudfomation 으로 1대 만들어도 된다.


여기서는 Cloudfomation 으로 1대 만들자.

myeks2-bastion-EC2


myeks

ec2키페어 필요

access/secret-key 필요




2

 terraform -version




<2> 테라폼 기초 알아보기



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




<3> 테라폼 주석  3가지


// 한줄 주석1


#  한줄 주석2


/*

라인

주석

*/





<4> 테라폼 블록 5가지


감사합니다.

매거진의 이전글 43탄-3.테라폼-AWS VPC 만들어 사용하자.
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari