# AWS Terraform 정리 ![Static Badge](https://img.shields.io/badge/Terraform-341e4a?logo=terraform) ![Static Badge](https://img.shields.io/badge/AWS%20CLI-232F3E?logo=amazonwebservices) ## 준비 사항 - [ ] Terraform 설치 [[문서 이동]](https://developer.hashicorp.com/terraform/install) - [ ] AWS CLI 설치 [[문서 이동]](https://docs.aws.amazon.com/ko_kr/cli/latest/userguide/getting-started-install.html) - [ ] aws에서 `Access key`와 `Secret access key` 생성 ## AWS CLI 설정 ```shell aws configure # aws configure 생성 AWS Access Key ID [None]: # AWS Access Key ID 입력 AWS Secret Access Key [None]: # Secret Access Key 입력 Default region name [None]: # 사용할 디폴트 리전 입력 Default output format [None]: json aws configure get region # 현재 AWS CLI와 연결되어 있는 리전 확인 ``` > 해당 설정은 필수 사항은 아니며, AWS CLI를 사용할 경우 또는 파일을 통한 참조를 할 때를 위한 작업 ## tf 파일 작성 Terraform을 통한 리소스 생성을 위해 tf 파일 작성 tf 파일 작성 같은 경우 유지보수의 용이를 위하여 종류 별 분리 |파일 명|내용| |---|---| |variables.tf|access key, secret access key, region 등 변수 정의| |aws.tf|provider 정의| |vpc.tf|vpc 정의| |subnet.tf|subnet 정의| |gateway.tf|gateway 정의| |route.tf|route table 정의| |acl.tf, acl_association.tf|ACL 관련 정의| |ssl_key.tf|SSH 접속을 위한 Key 관련 정의| |server.tf|EC2 정의| > 추가 예정 ## Terraform 실행 1. 디렉토리 초기화 및 프로바이더 플러그인 설치 ```shell terraform init Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "5.78.0"... - Installing hashicorp/aws v5.78.0... - Installed hashicorp/aws v5.78.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. ``` 2. 현 상태와 변경 사항 확인 ```shell terraform plan 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. # 변경 사항이 없어 위와 같이 표기 # 변경이 된 사항이 있을 경우 출력 ``` 3. 리소스 생성 ```shell terraform apply ``` 4. 리소스 삭제 ```shell terraform destroy ``` > 추가 예정