βοΈ 1. Resources(μμ±) : AWS μΈνλΌμ μ€μ§μ μΈ μΉμ μ λλ€. EC2 μΈμ€ν΄μ€, S3 λ²ν·, ELBλ±κ³Ό κ°μ ν΄λΌμ°λ ν¬λ©μ΄μ μ μ΄μ©ν΄ AWS μΉ μ½μμμ μ€ννλ κ²μΌλ‘ κ±°μ λͺ¨λ 리μμ€ μ νμ μμ±ν μ μμ΅λλ€. νμ§λ§ μ κ· λλ μ΅μ²¨λ¨μ AWS 리μμ€λ μ¦μ μ 곡λμ§ μλ κ²½μ°κ° μ’ μ’ μμ΅λλ€. 리μμ€μλ κΈ°λ³Έ λ°νκ°μ΄ μμ΅λλ€. Refλ₯Ό μ΄μ©ν΄ μ΄ λ°νκ°μ μ»μ΄μ¬ μ μκ³ ν νλ¦Ώμ λ€λ₯Έ μμΉμ μ¬μ©ν μ μμ΅λλ€. μλ₯Ό λ€μ΄ AWS::EC2::VPC 리μμ€ μ νμ κΈ°λ³Έ λ°νκ°μ κ°κ³ μκ³ μ΄ κ°μ VPCμ ID μ λλ€.
βοΈ 2. Parameters(μ λ ₯) : λͺ λ Ήμ€ λꡬμ μ λ ₯νλ 맀κ°λ³μμ λμΌνκ² μ€νμ λ§λ€κ±°λ μ λ°μ΄νΈν λ μ μνλ μ λ ₯κ°μ λλ€. νλΌλ―Έν°λ ν νλ¦Ώμ λ³κ²½ μμ΄λ μ€νμ 컀μ€ν°λ§μ΄μ¦ν μ μκ² ν΄μ€λλ€. AMI ID, VPC ID, Subnet IDλ±κ³Ό κ°μ 맀κ°λ³μλ₯Ό μ¬μ©ν μ μμ΅λλ€.
βοΈ 3. Output(μΆλ ₯) : μ€νμ΄ μλ£λ νμ κ²°κ³Όλ¬Όμ μΆλ ₯νλ €κ³ ν λ μ μ©ν©λλ€. μλ₯Ό λ€μ΄ ELBμ νΌλΈλ¦ URLμ΄λ EC2μ νΌλΈλ¦ IPλ₯Ό μΆλ ₯ν μ μμ΅λλ€.
βοΈ 4. Mapping(μ§μ ) : 리μ μ νΉνλ ν νλ¦Ώμμ μ΄λ ν μμλ₯Ό μ°Έμ‘°ν λ νμν©λλ€. μλ₯Ό λ€μ΄ ν νλ¦Ώμ EC2 AMI IDμ λν 맀νμ μ§μ νλ κ²μ λλ€. AMI IDκ° λ¦¬μ μ νΉνλ 리μμ€μ΄κΈ° λλ¬Έμ μ ν¨ν AMI IDλ₯Ό 리μ λ³λ‘ μ§μ νλ €κ³ ν λ μ¬μ©ν©λλ€.
# vi new-vpc.yaml
AWSTemplateFormatVersion: 2010-09-09
Resources:
VPC: ## λ
Όλ¦¬μ μ΄λ¦. λ΄κ° μ νκΈ° λλ¦.
Type: AWS::EC2::VPC
Properties:
CidrBlock: 192.168.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
InstanceTenancy: default
Tags:
- Key: Name
Value: NEW-VPC
SubnetA:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-2a
VpcId: !Ref VPC
CidrBlock: 192.168.0.0/20
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: NEW-PUBLIC-SUBNET-2A
SubnetB:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-2b
VpcId: !Ref VPC
CidrBlock: 192.168.16.0/20
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: NEW-PUBLIC-SUBNET-2B
SubnetC:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-2c
VpcId: !Ref VPC
CidrBlock: 192.168.32.0/20
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: NEW-PUBLIC-SUBNET-2C
SubnetD:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-2d
VpcId: !Ref VPC
CidrBlock: 192.168.48.0/20
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: NEW-PUBLIC-SUBNET-2D
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: NEW-IGW
VPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
RouteTableA:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: NEW-PUBLIC-RTB
InternetRoute:
Type: AWS::EC2::Route
DependsOn: InternetGateway
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
RouteTableId: !Ref RouteTableA
SubnetARouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref RouteTableA
SubnetId: !Ref SubnetA
SubnetBRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref RouteTableA
SubnetId: !Ref SubnetB
SubnetCRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref RouteTableA
SubnetId: !Ref SubnetC
SubnetDRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref RouteTableA
SubnetId: !Ref SubnetD
βοΈEnableDnsSupport;
βοΈ λ¦¬μ - μμΈμμ μ§ν
βοΈ μ€νμ΅μ κ΅¬μ± μλ΅ - μ€ν μμ± λ²νΌ ν΄λ¦
VPC ID, μλΈλ· ID μμμ μμ±λ κ²μΌλ‘ μ λ ₯ν΄μ£ΌκΈ°
# vi new-ec2.yaml
AWSTemplateFormatVersion: 2010-09-09
Mappings:
RegionMap:
ap-northeast-2:
AMIID: ami-0fd0765afb77bcca7
ap-northeast-1:
AMIID: ami-0b7546e839d7ace12
Parameters:
InstanceTypeParameter:
Type: String
Default: t2.micro
Description: Enter instance size. Default is t2.micro
VPC:
Type: String
Default: vpc-01a276b266db7833b
Description: VPC ID.
Subnet:
Type: String
Default: subnet-01132b9dddcf71d3d
Description: Subnet ID.
AMI:
Type: String
Default: AMIID
Description: The Linux AMI to use.
Key:
Type: String
Default: new-key
Description: The key used to access the instance.
Resources:
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: "NEW-SG-WEB"
GroupDescription: "SSH and web traffic in, all traffic out."
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 123.142.252.25/32
SecurityGroupEgress:
- IpProtocol: -1
CidrIp: 0.0.0.0/0
Linux:
Type: 'AWS::EC2::Instance'
Properties:
SubnetId: !Ref Subnet
# ImageId: !Ref AMI
ImageId: !FindInMap [ RegionMap, !Ref "AWS::Region", !Ref AMI ]
InstanceType:
Ref: InstanceTypeParameter
KeyName: !Ref Key
SecurityGroupIds:
- Ref: InstanceSecurityGroup
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeSize: 8
- DeviceName: /dev/xvdb
Ebs:
VolumeSize: 8
Tags:
- Key: Name
Value: NEW-EC2
UserData:
Fn::Base64: |
#cloud-boothook
#!/bin/bash
yum install -y httpd
systemctl enable --now httpd
echo "Hello World!" > /var/www/html/index.html
Outputs:
PublicIp:
Description: PublicIp Output
Value: {"Fn::GetAtt": ["Linux","PublicIp"]}
βοΈ λ¦¬μ - μμΈμμ μ§ν
βοΈ μ€νμ΅μ κ΅¬μ± μλ΅ - μ€ν μμ± λ²νΌ ν΄λ¦
βοΈ μμ μμ ; ec2 - vpc
βοΈ μ€νμ μμ νλ©΄ λ΄λΆ 리μμ€λ€λ νκΊΌλ²μ μμ λ¨.
βοΈ s3λ²ν·μ λ°λ‘ μμ ν΄μ£Όμ΄μΌ ν¨.
μ€λ¬΄ νΉν pdf νμΌ 7.p~ νμΈ
βοΈμ€μΌμ€νΈλ μ΄μ
λꡬ
μΏ λ²λ€ν°μ€λ 곧 ν΄λ¬μ€ν°,
AZURE AKS, EKS, GCP GKE => μΏ λ²λ€ν°μ€
ꡬμ±κ΄λ¦¬ ; ansible(on-prem)
μλ² ν
νλ¦Ώ ; λ컀
μ€μΌμ€νΈλ μ΄μ
; μΏ λ²λ€ν°μ€
νλ‘λΉμ ; ν
λΌνΌ
->μμλΈκ³Ό ν
λΌνΌμ νΌλΈλ¦ ν΄λΌμ°λ μλμ μ μ΄λ€λ©΄μ κ·Έ μ°¨μ΄κ° μ μ μ€μ΄λ¦. νμ§λ§ ν
λΌνΌμ΄ ec2μμ±νκΈ°κ° λ μ¬μ.
ν
λΌνΌμ ν΄μμ½νμ¬μμ GoμΈμ΄λ‘ κ°λ°ν μ€νμμ€λꡬμ
λλ€. μ΄μ체μ λ§λ€ λ°μ΄λ리 νμΌμ΄ μ‘΄μ¬νλλ° Go μ½λλ νλμ λ°μ΄λ리 νμΌλ‘ μ»΄νμΌλλ©° Terraformμ΄λΌλ λͺ
λ Ήμ΄λ‘ μ€νν μ μμ΅λλ€. μ΄ Terraform λͺ
λ Ήμ΄λ₯Ό μ¬μ©νμ¬ λ
ΈνΈλΆ, λ°μ€ν¬ν, λΉλ μλ² λλ λ€λ₯Έ μ»΄ν¨ν°μμλ μΈνλΌλ₯Ό λ°°ν¬ν μ μμΌλ©° μ΄λ₯Ό μν΄ μΆκ° μΈνλΌ(λ§μ€ν°, μμ΄μ νΈ)λ₯Ό μμ±ν νμκ° μμ΅λλ€. μ¦ Terraform λͺ
λ Ήμ΄κ° AWS, Azure, GCP, Openstack λ±μ Providerλ₯Ό λμ ν΄ APIλ₯Ό νΈμΆνμ¬ λ¦¬μμ€λ₯Ό μμ±ν©λλ€.
ν
λΌνΌμ μμ±νλ €λ μΈνλΌ μ λ³΄κ° λ΄κ²¨ μλ ν
μ€νΈλ‘ μ΄λ£¨μ΄μ§ ν
λΌνΌ κ΅¬μ± νμΌμ μμ±νμ¬ APIλ₯Ό νΈμΆν©λλ€. μ΄λ¬ν κ΅¬μ± κ°λ€μ΄ 'μ½λν μΈνλΌ'λ₯Ό λ§λλ λ°λ‘ κ·Έ 'μ½λ'μ
λλ€. νμ λκ΅°κ°κ° μΈνλΌλ₯Ό μμ νκ³ μ ν λ, μλ²μ μ§μ μ μνμ¬ μμ
νκ±°λ μμμ
μΌλ‘ μμ νλ λμ ν
λΌνΌμ μ¬μ©νμ¬ κ΅¬μ± νμΌμ μμ ν μ μμ΅λλ€.
# vi main.tf
provider "aws" { # awsλ₯Ό 곡κΈμλ‘ μ¬μ©νμ¬
region = "ap-northeast-2" # μμΈ λ¦¬μ μ μΈνλΌλ₯Ό λ°°ν¬νλ€λ μλ―Έ
}
resource "aws_instance" "example" {
ami = "ami-0fd0765afb77bcca7"
instance_type = "t2.micro"
}
resource "_" "" { # PROVIDERλ aws κ°μ 곡κΈμμ μ΄λ¦μ΄κ³ TYPEμ instance κ°μ΄ ν΄λΉ 곡κΈμμμ μμ±ν 리μμ€ μ νμ
λλ€. NAMEμ ν
λΌνΌ μ½λμμ μ΄ λ¦¬μμ€λ₯Ό μ°Έμ‘°νκΈ° μν΄ μ¬μ©ν μ μλ exampleκ³Ό κ°μ 'μλ³μ'μ
λλ€. CONFIGλ νΉμ 리μμ€μ λν νλ μ΄μμ μΈμ(argument)λ‘ κ΅¬μ±λ©λλ€.
[CONFIG ...]
}
βοΈ *.tf μ€ν¬λ¦½νΈ μμ±
βοΈ terraform init : terraform λͺ
λ Ήμ΄μλ ν
λΌνΌμ κΈ°λ³Έ κΈ°λ₯μ΄ ν¬ν¨λμ΄ μμ§λ§ λͺ¨λ 곡κΈμ(AWS, Azure, GCP λ±)μ λν μ½λκ° ν¬ν¨λμ΄ μμ§ μμ΅λλ€. κ·Έλ κ² λλ¬Έμ terraform init λͺ
λ Ήμ΄λ₯Ό μ€ννμ¬ ν
λΌνΌμ μ½λλ₯Ό μ€μΊνλλ‘ μ§μνκ³ μ΄λ 곡κΈμμΈμ§ νμΈνκ³ , νμν μ½λλ₯Ό λ€μ΄λ‘λνλλ‘ ν΄μΌ ν©λλ€. κΈ°λ³Έμ μΌλ‘ 곡κΈμ μ½λλ ν
λΌνΌμ .terraform ν΄λμ λ€μ΄λ‘λλ©λλ€.
βοΈ terraform plan : ν λΌνΌμ΄ κ΅¬μ± νμΌμ μ¬μ©νμ¬ μμ μ μννκΈ° μ μ μ½λμ μ¨μ μ±μ κ²μ¬ν μ μμ΅λλ€. plan λͺ λ Ήμ΄λ 리λ μ€μμ μ°μ΄λ diff λͺ λ Ήμ κ²°κ΄κ°κ³Ό μ μ¬ν©λλ€. + κ° μλ νλͺ©μ μΆκ°λκ³ , - κ° μλ νλͺ©μ μμ λλ€λ λ»μ λλ€. ~ κ° μλ νλͺ©μ μμ λ©λλ€.
βοΈ terraform apply : ν λΌνΌμ κ΅¬μ± νμΌμ μ€ννλ €λ©΄ terraform apply λͺ λ Ήμ΄λ₯Ό μ€νν©λλ€.
# wget https://releases.hashicorp.com/terraform/1.2.3/terraform_1.2.3_linux_amd64.zip
# unzip terraform_1.2.3_linux_amd64.zip
# mv terraform /usr/local/bin/
# terraform -version
# mkdir aws && cd $_
# vi main.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_instance" "example" {
ami = "ami-0fd0765afb77bcca7"
instance_type = "t2.micro"
}
# terraform init
# terraform plan
# terraform apply
# vi main.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_instance" "example" {
ami = "ami-0fd0765afb77bcca7"
instance_type = "t2.micro"
tags = {
Name = "terraform-example"
}
}
# terraform init
# terraform plan
# terraform apply
# terraform destroy
# vi main.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_instance" "example" {
ami = "ami-0fd0765afb77bcca7"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.instance.id]
key_name = "new-key"
user_data = <<-EOF
#! /bin/bash
yum install -y httpd
systemctl enable --now httpd
echo "Hello, Terraform" > /var/www/html/index.html
EOF
tags = {
Name = "terraform-example"
}
}
resource "aws_security_group" "instance" {
name = var.security_group_name
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["123.142.252.25/32"]
}
ingress {
from_port = -1
to_port = -1
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "terraform-sg"
}
}
variable "security_group_name" {
description = "The name of the security group"
type = string
default = "terraform-example-instance"
}
output "public_ip" {
value = aws_instance.example.public_ip
description = "The public IP of the Instance"
}
output "public_dns" {
value = aws_instance.example.public_dns
description = "The Public dns of the Instance"
}
output "private_ip" {
value = aws_instance.example.private_ip
description = "The Private_ip of the Instance"
}
# terraform init
# terraform plan
# terraform apply
# terraform output public_ip
# terraform destroy
πβοΈβοΈπ’βοΈπ