> For the complete documentation index, see [llms.txt](https://psedu.gitbook.io/cloudia/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://psedu.gitbook.io/cloudia/task-13-auto-scaling-group.md).

# Task 13 Auto-scaling-group 생성

Info

\-       Terraform VPC + Auto Scaling Group + ALB Example

VPC

\- VPC CIDR 은 10.0.0.0/16

\- Subnet CIDR 은 10.X.0.0/24

\- Subnet 은 본인이 선택한 Region의 Availability Zone 수만큼 생성 (ex. us-east-1 -> 4 Availability Zones -> 4 Subnets)

\- 각 Availability Zone 별로 Public Subnet, Private Subnet 페어로 한개씩 존재하도록 생성

\- Internet Gateway 생성 후 VPC에 Attach

\- Route Table 은 Public 과 Private Route Table 한개씩 총 2개 생성

\- Public Route Table 은 Internet Gateway 로 통신 가능하도록 Route 추가 후 Public Subnet 4개와 연결 (Associatation)

\- Private Route Table 은 Route 추가 없이 Private Subnet 4개와 연결 (Associatation)

\- NAT Gateway 용 EIP 생성

\- Public Subnet에 NAT Gateway 생성

\- Private Route Table에 외부 통신을 위해서 NAT Gateway 로 통신 가능하도록 Route 추가

\- SSH 허용을 위한 Admin 용 Security Group 과 HTTP 웹 접속 허용을 위한 Web Security Group 총 두개의 Security Group 생성

\- Admin Security Group 에는 SSH(20) 포트를 본인 Cloud9 Public IP 허용하는 Rule 생성

\- Web Security Group 에는 HTTP(80) 포트를 모두 허용하는 Rule 생성

\- 각 Resource를 생성하는 코드를 모두 Module 로 제작

ALB

\- ALB 및 Target Group

\- Target Group 은 비워놓은 채 생성

Auto scaling group

\- Launch Template 생성

A.     AMI ID: 기존 Task 9 EC2 챕터에서 생성한 AMI의 ID

B.      Data Volume: 기존 Task 9 EC2 챕터에서 생성한 Data Volume의 스냅샷 ID

C.      EC2 instance에 설정할 keypair 명 설정

D.     Security Group: Web, Admin Security Group 설정

\- Auto Scaling Group 생성

E.      위에서 생성한 Launch Template 설정

F.      위에서 생성한 ALB 설정

G.     Min(1)/Max(4)/Desired Capacity(2) 설정

1\)    auto-scaling-group실습을 진행하기 위해서 12-auto-scaling-group폴더로 경로 이동

\-       <mark style="color:blue;">cd ../12-auto-scaling-group/</mark>

<figure><img src="https://2744132130-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZTJxGWFIVE9QNQ5WJSQ1%2Fuploads%2FVISVHjFAh9TOml8BO7ZS%2Fimage.png?alt=media&amp;token=d33b7cc5-f3ee-4d8a-be12-9e829c60911e" alt=""><figcaption></figcaption></figure>

2\)    업로드 된 폴더 12-auto-scaling-group 하위 파일인 auto\_scaling\_group.tfvars 파일 실행

변수설정

\- auto\_scaling\_group.tfvars 파일 확인 실행 환경에 맞게 변경

\- # auto\_scaling\_group.tfvars 파일\
\
prefix                  =       "user-<mark style="background-color:red;">\*\*</mark>"

region                  =       "ap-northeast-2"

vpc\_cidr                =       "10.<mark style="background-color:red;">\*\*</mark>.0.0/16"

public\_subnets          =       \[

&#x20;   {cidr = "10.<mark style="background-color:red;">\*\*</mark>.1.0/24", availability\_zone = "ap-northeast-2a"},

&#x20;   {cidr = "10.<mark style="background-color:red;">\*\*</mark>.2.0/24", availability\_zone = "ap-northeast-2b"},

&#x20;   {cidr = "10.<mark style="background-color:red;">\*\*</mark>.3.0/24", availability\_zone = "ap-northeast-2c"},

&#x20;   {cidr = "10.<mark style="background-color:red;">\*\*</mark>.4.0/24", availability\_zone = "ap-northeast-2d"},

]

private\_subnets         =       \[

&#x20;   {cidr = "10.<mark style="background-color:red;">\*\*</mark>.11.0/24", availability\_zone = "ap-northeast-2a"},

&#x20;   {cidr = "10.<mark style="background-color:red;">\*\*</mark>.12.0/24", availability\_zone = "ap-northeast-2b"},

&#x20;   {cidr = "10.<mark style="background-color:red;">\*\*</mark>.13.0/24", availability\_zone = "ap-northeast-2c"},

&#x20;   {cidr = "10.<mark style="background-color:red;">\*\*</mark>.14.0/24", availability\_zone = "ap-northeast-2d"},

]\
\
admin\_access\_cidrs           = \[<mark style="background-color:red;">"<\<YOUR\_LOCAL\_IP\_CIDR>>"</mark>]

ami\_id                      =   <mark style="background-color:red;">"<\<YOUR\_AMI\_ID>>"</mark>

data\_vol\_snapshot\_id        =   <mark style="background-color:red;">"<\<DATA\_VOLUME\_SNAPSHOT\_ID>>"</mark>

data\_volume\_size            =   "10"

instance\_type               =   "t3.micro“

keypair\_name                =   <mark style="background-color:red;">"<\<YOUR\_KEYPAIR\_NAME>>"</mark>

port                        =   "80"

protocol                    =   "HTTP"

min\_size                =       1

max\_size                =       4

desired\_capacity        =       2\ <br>

\- Prefix는 알맞게 변경

\- Region은 본인이 사용할 region 코드로 변경

\- Subnet의 Availability Zone 값은 Region에 맞게 변경

\- admin access CIDR : SSH 접속 허용할 IP 값 변경

\- AMI ID: 기존 Task 9 ec2 챕터에서 생성한 AMI의 ID

\- Data Volume Snapshot ID: 기존 Task 9 ec2 챕터에서 생성한 Data Volume의 스냅샷 ID

\- EC2 instance에 설정할 keypair 명 설정

\- ALB에서 Instance 로 서비스될 트래픽의 포트(80)와 프로토콜(HTTP)

&#x20;

3\)    Init 명령으로 Terraform 수행을 위한 provider plugin 초기화 및 다운로드 수행

\- <mark style="color:blue;">terraform init</mark>

4\)    Plan 명령으로 Terraform 수행 전 실행 시뮬레이션 확인

\- <mark style="color:blue;">terraform plan --var-file=auto\_scaling\_group.tfvars</mark>

5\)    Apply 명령으로 Terraform을 통한 Resource 생성 수행

\- <mark style="color:blue;">terraform apply --var-file=auto\_scaling\_group.tfvars</mark>

6\)    실행 내용 확인

\- 선택한 Region에 VPC, Subnet, Internet Gateway, Route Table, NAT Gateway, Security Group, instance 생성 내용 확인.\
Auto Scaling Group 확인.\
생성된 ALB 와 Target Group 확인. Target Group에 소속된 Instance의 Status Healty 확인.\
ALB의 DNS name 으로 웹브라우저 접속 정상 확인.\
웹브라우저 refresh 시, 정상적인 Balancing 접속 확인.

7\)    Resource 삭제

\-       Destroy

\-       Destroy 명령으로 생성된 Auto Scaling Group, ALB 및 VPC 삭제 수행

\-       <mark style="color:blue;">terraform destroy --var-file=auto\_scaling\_group.tfvars</mark>

&#x20;

8\) 폴더 삭제

\-       Cloud9 왼쪽 메뉴에 12-auto-scaling-group폴더 우 클릭 후 Delete

&#x20;
