programming-study
programming-study copied to clipboard
[devOps] Learn DevOps: Infrastructure Automation With Terraform
https://www.udemy.com/learn-devops-infrastructure-automation-with-terraform/learn/v4/overview
Terraform
인프라를 만들고 바꾸고 버전 관리하는 도구
소개
- HCL
-
.tf
or.tf.json
- Infra Structure = Provider └─ AWS, DigitalOcean, Google Cloud, Microsoft Azure, ...
특징
- Infrastructure as Code
- Execution Plans
- Resource Graph
- Change Automation
설치
https://www.terraform.io/downloads.html
Demo Sample
https://github.com/wardviaene/terraform-course
환경 셋팅(+Vagrant)
https://github.com/heowc/vagrant-sample/tree/master/ubuntu1604_terraform
설정
-
.tf
: 각각의 설정 -
.tfvars
: 상수 값 초기화
Terraform로 AWS 코딩하기
-
AWS 계정 생성
- IAM 추가(AdministratorAccess)
-
terraform init
-
region 확인(https://docs.aws.amazon.com/ko_kr/general/latest/gr/rande.html)
-
설정 작성(provider 지정)
-
terraform plan
-
terraform apply
EC2 instance
- resoure:
aws_instance
- ami 지정
- 인스턴스 타입 지정
S3 Bucket
- resoure:
aws_s3_bucket
RDS MySql
- resource:
aws_db_instance
-
engine
,username
,password
, ... -
skip_final_snapshot
: 마지막 스냅샷 skip 여부
VPC
- Virtual Private Network
- default VPC 존재
- VPC 안에 application, database를 구성
main vpc
┌ subnet - [public], [private]
gateway ┼ subnet - [public], [private]
└ subnet - [public], [private]
-
public subnet
은gateway
를 통해 접근할 수 있어야 한다. (service, application, etc) -
private subnet
은 외부로 부터 접근할 수 없어야 하며,nat-gateway
를 통해서만 접근 할 수 있어야 한다. (database, caching service, etc)
remote-state
-
terraform remote
-
backend
- S3에 version controll 가능
security group 설정
-
aws_security_group
- CIDR, port, protocol 등 설정
※ CIDR 사이더(Classless Inter-Domain Routing, CIDR)는 클래스 없는 도메인 간 라우팅 기법으로 1993년 도입되기 시작한, 최신의 IP 주소 할당 방법이다.
aws-cli 맛보기
설치
- https://docs.aws.amazon.com/ko_kr/cli/latest/userguide/installing.html
-
aws
ssh 접속
- https://docs.aws.amazon.com/ko_kr/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html
- ssh 접근:
ssh -i {pem file} {user}@{public ip}
- file 업로드:
scp -i {pem file} {user}@{public ip}
data sources
- Terraform 구성의 다른 곳에서 사용하기 위해 데이터를 가져 오거나 계산할 수 있다.
- Terraform 구성을 Terraform 외부에서 정의 된 정보 또는 다른 별도의 Terraform 구성으로 정의 된 정보를 기반으로 구축 할 수 있다.
-
data
NGINX 맛보기(ubuntu 기준)
설치
$ sudo apt-get update -y
$ sudo apt-get install -y nginx
실행
$ sudo systemctl start nginx.service
or
$ sudo service nginx start
설정 파일 갱신
$ sudo nginx -s reload
프록시 설정(ex. conf.d/ngx_proxy.conf
생성)
api application에 proxy 용도라면,
nginx.conf
에 include된 site-available를 주석하는 것이 좋다?
server {
location / {
proxy_pass http://localhost:8080;
}
}
참고 https://nginx.org/en/docs/beginners_guide.html
명령어 정리
명령어 | 설명 |
---|---|
apply | provider에 반영 |
destroy | 테라폼으로 provider에 반영된 내용 제거 |
fmt | 설정 파일 포맷과 스타일을 정리 |
get | 모듈 다운로드 및 업데이트 |
graph | Graph Viz 문법의 내용 표기 |
import | state import |
output | output 내용 표기 |
plan | 작성한 내용을 표기 |
push | hashicorp의 엔터프라이즈 툴인 atlas에서 push |
refresh | remote state 갱신 |
remote | remote storge 설정(ex. S3, consul) |
show | plan 내용 보기 |
state | 강화된 state 관리 command |
taint | apply에서 지정한 내용을 없애고 다음 apply에 재생성 |
validate | 테라폼 문법 검증 |
untaint | taint 취소 |