More Related Content Similar to Terraform을 이용한 Infrastructure as Code 실전 구성하기 (20) More from JeongHun Byeon (20) Terraform을 이용한 Infrastructure as Code 실전 구성하기5. 유지보수 문제 서버를 내릴 수 없다
장애가 나지 않으면 굳이 설정이나
구성을 바꾸지 않는다
이상한 설정이 있어도 개선하지 않는다
누가 변경을 해도 알기가 어렵다
어떤 리소스가 있는지 알 수 없다
23. resource "aws_instance" "api" {
ami = "ami-d39a02b5" # ubuntu 16.04
instance_type = "t2.micro"
subnet_id = "subnet-xxxxxxxx"
security_groups = ["sg-xxxxxxxx"]
key_name = "your-key-pair"
}
HCL(HashiCorp Configuration Language)
24. resource "aws_instance" "api" {
ami = "ami-d39a02b5" # ubuntu 16.04
instance_type = "t2.micro"
subnet_id = "subnet-xxxxxxxx"
security_groups = ["sg-xxxxxxxx"]
key_name = "your-key-pair"
}
HCL(HashiCorp Configuration Language)
예약어
리소스 종류
리소스 이름
속성 명 속성 값
26. $ terraform plan
------------------------------------------------------
Terraform will perform the following actions:
+ aws_instance.api
id: <computed>
ami: "ami-d39a02b5"
associate_public_ip_address: <computed>
availability_zone: <computed>
instance_type: “t2.nano"
security_groups.#: “1”
security_groups.541019735: "sg-247b2042"
source_dest_check: "true"
Plan: 1 to add, 0 to change, 0 to destroy.
terraform plan
28. $ terraform apply
aws_instance.api: Creating...
ami: "" => "ami-d39a02b5"
associate_public_ip_address: "" => "<computed>"
availability_zone: "" => "<computed>"
instance_type: "" => "t2.nano"
security_groups.#: "" => "1"
security_groups.541019735: "" => "sg-247b2042"
source_dest_check: "" => ”true”
aws_instance.api: Still creating... (10s elapsed)
aws_instance.api: Creation complete after 23s (ID: i-0xxx)
Apply complete!
Resources: 1 added, 0 changed, 0 destroyed.
terraform apply
29. {
"version": 3,
"terraform_version": "0.11.2",
"modules": [{
"resources": {
"aws_instance.api": {
"type": "aws_instance",
"depends_on": [],
"primary": {
"id": "i-0cf4c4b56bad7ddb1",
"attributes": {
"ami": "ami-d39a02b5",
...
},
"meta": { "schema_version": "1" },
},
"provider": "provider.aws"
}
}
}]
}
terraform.tfstate
34. $ terraform import aws_instance.web i-123456
aws_instance.web: Importing from ID "i-123456"...
aws_instance.web: Import complete!
Imported aws_instance (ID: i-123456)
aws_instance.web: Refreshing state... (ID: i-123456)
Import successful!
The resources that were imported are shown above.
These resources are now in your Terraform state
and will henceforth be managed by Terraform
terraform import
35. “Terraform import의 현재 구현체는 상태로만
리소스를 가져올 수 있습니다. 이는 설정을 만들지
않습니다. Terraform 차기 버전에서는 설정도
생성할 것입니다.
https://www.terraform.io/docs/import/index.html
38. $ terraform import aws_instance.web i-123456
Error: resource address "aws_instance.web" does not
exist in the configuration.
Before importing this resource, please create
its configuration in the root module.
For example:
resource "aws_instance" “web" {
# (resource arguments)
}
terraform import
46. Remote State terraform.tfstate 원격 저장소에서 관리한다
Consul, AWS S3, Google Cloud Storage
(GCS), etcd, Terraform Enterprise 등의
백엔드 지원
상태 파일에는 민감한 정보가 있을 수 있으므로
암호화를 해야 한다
백엔드에 따라 Lock을 사용할 수도 있다
47. terraform {
backend "s3" {
bucket = "terraform-state"
key = "ec2/terraform.tfstate"
region = "ap-northeast-1"
encrypt = true
kms_key_id = "alias/terraform_state"
dynamodb_table = "terraform-lock"
}
}
S3 remote state backend
48. S3 Backend S3 버킷의 버저닝 기능을 사용한다
KMS로 암호화한다
DynamoDB를 Lock 테이블로 사용한다
S3 버킷과 DynamoDB, KMS 는 미리
만들어 두어야 한다
56. data "terraform_remote_state" "vpc" {
backend = "s3"
config {
bucket = "terraform-state"
key = "vpc/terraform.tfstate"
region = "ap-northeast-1"
dynamodb_table = "terraform-lock"
}
}
terraform_remote_state
58. resource "aws_instance" "api" {
ami = "ami-d39a02b5"
instance_type = "t2.nano"
security_groups = [
"${data.terraform_remote_state.vpc.public_id}"
]
}
리소스 상호 참조
60. data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = [
"ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"
]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
data “aws_ami”
67. ├── main.tf # 리소스 설정
├── outputs.tf # 출력값
└── variables.tf # 입력값
Module 구조
68. # 필수
variable "name" {
description = "ALB 이름"
}
# 옵션
variable "subnet_ids" {
description = "ALB에 적용할 서브넷 ID 리스트"
type = "list"
default = []
}
variables.tf
69. resource "aws_alb" "main" {
name = "${var.name}"
internal = false
subnets = ["${var.subnet_ids}"]
security_groups = ["sg-1234567"]
}
main.tf
70. output "name" {
value = "${aws_alb.main.name}"
}
# ALB ID
output "id" {
value = "${aws_alb.main.id}"
}
# ALB ARN.
output "arn" {
value = "${aws_alb.main.arn}"
}
output.tf