수업 61일차 테라폼 명령어 , 인스턴스 , 웹 서버 만들기 , 변수지정

유동우·2022년 12월 18일
1

■ 테라폼을 위한 준비

1. AWS CLI가 설치된 운영체제 - Client 실행
2. 테라폼 설치

aws sts get-caller-identity : 사용자 정보 확인하는법


echo $SHELL
ls -al		// .zshrc인지 .bashrc인지 확인
vim ~/.zshrc
맨아래 alias tf='terraform' 추가
source ~/.zshrc
tf --version
terraform -install-autocomplete

================================

cd 01-terraform 
rm -rf *.*
ls	//비어있어야함
mkdir local
mkdir one-server
mkdir one-webserver

● 공급자를 local로 사용
cd local
vim main.tf
Local 지정.pdf 복사붙혀넣기
tree
tf init


cd ..
cd one-server
vim main.tf

provider "aws" {
   region = "ap-northeast-2"
}


tf init

cd ../local
vim main.tf
tf fmt		//포매팅(띄어쓰기 맞춰주는)

vim main.tf
tf validate		//문법검사




main.tf
- 공급자	=> init => plan => apply
- 리소스 정의

■ Terraform 명령어

명령 설명

terraform -help terraform 사용법
terraform init 테라폼을 수행하기 위한 공급자의 플러그인들을 초기 설정하는 명령어
terraform fmt terraform 표준 스타일로 파일 구성 포매팅
terraform validate 파일의 구성이 유효한지 문법 검사Terraform 설치/설정 2 명령 설명
terraform plan 테라폼을 통해 실제로 생성되고 변경되는 내역을 보여준다. 더하기(+) : 생성 빼기(-) : 제거 물결(~) : 변경
terraform apply terraform apply-auto-approve
실제 작업을 진행, 변경 내역을 반영하려면 yes 입력 yes를 자동실행
terraform output 출력 파일
terraform state list 주어진 주소와일치하는 상태 파일의 모든 리소스 확인
terraform graph Graph를 통해 종속성 및 생성할 리소스에 대한 관계를 시각적으로 볼 수 있는 기능
terraform destroy terraform destroy -auto-approve 인프라 삭제




cd.. // 01-terraform에
vim .gitignore
1 .terraform
2 *.tfstate
3 *.tfstate.backup
git init

■ 인스턴스 만들기

cd ../oneserver	
 vim main.tf
  tf fmt
  vim main.tf
  tf init  	  
  tf plan
  vim main.tf
  tf apply -auto-approve
  cat terraform.tfstate
  vim main.tf
  
  1 terraform {
  2   # 테라폼 버전 지정
  3   required_version = ">=1.0.0, <2.0.0"
  4
  5   # 공급자 버전 지정
  6   required_providers {
  7     aws = {
  8       source  = "hashicorp/aws"
  9       version = "~> 4.0"
  10     }
  11   }
  12 }
  13
  14 provider "aws" {
  15   region = "ap-northeast-2"
  16 }
  17
  18 resource "aws_instance" "example" {
  19   ami           = "ami-0ab04b3ccbadfae1f"
  20   instance_type = "t2.micro"
  21
  22   tags = {
  23     Name = "aws08-example"
  24   }
  25 }

==========================

■ 웹 서버 만들기

cd ../one-webserver
cp ../one-server/main.tf .
vim main.tf
22 user_data = <<-EOF
23 #!/bin/bash
24 echo "Hello, World" > index.html
25 nohup busybox httpd -f -p 8080 &
26 EOF
추가

tf init
tf apply -auto-approve

■ 퍼블릭주소 넣는법

33 # Public IP Output
34 output "public-ip" {
35 value = aws_instance.example.public_ip
36 description = "The public IP of the Instance"
37 }

tf apply
yes

■ 8080포트

34 # 보안그룹 설정 8080포트 Open
35 resource "aws_security_group" "instance" {
36 name = "aws08-terraform-example-instance"
37
38 ingress {
39 from_port = 8080
40 to_port = 8080
41 protocol = "tcp"
42 cidr_blocks = ["0.0.0.0/0"]
43
44 }
45 }

21 vpc_security_group_ids = [aws_security_group.instance.id] //리소스에 넣어줘야됨

■ 변수지정

cd 01-terraform
mkdir syntax
vim main.tf

1 provider "local" {
2 }
3
4 variable "number" {
5 description = "An example of a variable in Terraform"
6 type = number
7 default = 42
8 }
9
10 output "number_example" {
11 value = var.number
12 }
13

:e variable.tf

vim output.tf

1 output "number_example" {
2 value = var.number
3 }

:w
:e main.tf

output,variable지우고 저장

profile
클라우드 엔지니어가 되고싶은 클린이

0개의 댓글