brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Dec 03. 2019

193.(예전버전) 테라폼- AWS - VPC생성

2019 작성

2019 테라폼 예전 버전용 입니다~



<1> 현황,문제점,개선방향

<2> Tool  범위

<3> 설정 순서

<4> API계정으로 VPC 만들기

<5> 테라폼 설치와  VPC 생성

<6> 리소스 삭제

<7>  AWS  4.13 버전 설정법



<1>  현황,문제점,개선방향


1. 현황

신규 서비스가 생기면  2시간 걸려 VPC와 IAM 계정 생성함.

 기본 인프라 구축에 2시간이 소요된다.


2. 문제점

신규 서비스가 여러 건 생기면 기본 인프라 제공 시간이 지연된다.


3. 개선방향

테라폼이나 Cloudformation으로  자동화하여  30분 이내 구축해보자



<2> Tool  범위





<3>  설정 순서



<4>  API계정으로 VPC 만들기


1.  구성

리눅스 서버 (API  계정) -----  AWS (VPC 만들기)



2. 환경 구성


1)  리눅스 서버 만들기


2)  AWS에 로그인하여 AWS API계정과  API 계정에 권한 할당하기

access_key와 secret_key를 확인한다.

계정과 암호이므로 외부에 노출되지 않도록 주의해야 한다.


3) 리눅스 로그인하여 aws configure 설정한다.


[root@ip-10-0-0-32 ~]# aws configure
AWS Access Key ID [****************NYXG]:
AWS Secret Access Key [****************OFjC]:
Default region name [None]: ap-northeast-2
Default output format [None]:

[root@ip-10-0-0-32 ~]# aws s3 ls
2019-11-26 11:24:44 seo-s3.serverchk.com
2019-11-26 12:28:04 seo1-s3.serverchk.com



3.  생성하려는 VPC구성도


목표

Public, Private, DB 서브넷으로 구성된 VPC

AZ이중화

일단은  간단하게 만들어 본다.



4.  만들어야 할 것들


1)  vpc  1개 , subnet 1개

2)  igw

3)  route table




<5>  테라폼 설치와  VPC 생성


1. 최신 버전  0.12 버전

www.terraform.io/downloads.html



1) linux


wget   https://releases.hashicorp.com/terraform/0.12.18/terraform_0.12.18_linux_amd64.zip

unzip terraform_0.12.18_linux_amd64.zip

mv terraform /usr/bin

terraform -v



2) mac


$ brew install terraform

terraform version




2.  provider지정과  테라폼  초기화


[root@ip-10-0-0-32 tera]# pwd
/root/tera


1)      

 vi  aws-provider.tf


provider "aws" {

  access_key = "<ACCESS_KEY>"

  secret_key = "<SECRET_KEY>"

  region     = "ap-northeast-2"

}



2) 테라폼을 사용하기 위해서는 테라폼 초기화 작업이 필요하다.


terraform init




3) 리눅스 서버에  vsftp 설치해서 pem -> pub키로 변환해 key.tf 만들기


 yum install vsftpd*

/usr/sbin/vsftpd

Pem키 올리기

aws에서 ftp 허용 필요


[root@ip-10-0-0-32 tera]# chmod 700 tera1.pem

[root@ip-10-0-0-32 tera]# ssh-keygen -y -f tera1.pem >> tera1.pub



4) key.tf 파일 작성
[root@ip-10-0-0-32 tera]# more key.tf
resource "aws_key_pair" "terraform-key" {
key_name = "tera1"
public_key = "${file("./tera1.pub")}"
}



5)  terraform import로 SSH 키를 테라폼에 등록


[root@ip-10-0-0-32 tera]# terraform import aws_key_pair.terraform-key tera1


6) terraform.tfstate 파일에  public key를 직접 입력

" zzzzzzzzzzfadsasdas"



4. 리소스가 생성 가능한지 계획


terraform plan

오류 시

Error: Could not satisfy plugin requirements

Plugin reinitialization required. Please run “terraform init”.




5.  vpc 만든다


terraform apply

Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.


Enter a value: yes

aws_vpc.test-vpc: Creating...
aws_vpc.test-vpc: Creation complete after 1s [id=vpc-07b2035435d821020]
aws_subnet.test-subnet-c: Creating...
aws_internet_gateway.test-gateway: Creating...
aws_subnet.test-subnet-c: Creation complete after 0s [id=subnet-0d19833d9839335f1]
aws_internet_gateway.test-gateway: Creation complete after 0s [id=igw-06f4a86b059a94454]
aws_route_table.test-route-table: Creating...
aws_route_table.test-route-table: Creation complete after 1s [id=rtb-09d1f3bd8b4358921]
aws_route_table_association.my-subnet-association: Creating...
aws_route_table_association.my-subnet-association: Creation complete after 0s [id=rtbassoc-05a775a823cebc002]



6. 확인


VPC


Subnet



Route



IGW




7. 설정 파일


[root@ip-10-0-0-32 network-config]# ls

gateway.tf routes.tf terraform.tfstate vpc-subnet.tf



[root@ip-10-0-0-32 network-config]# more vpc-subnet.tf


resource "aws_vpc" "test-vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "test-vpc"
}
}


resource "aws_subnet" "test-subnet-c" {
vpc_id = "${aws_vpc.test-vpc.id}"
cidr_block = "10.0.1.0/24"
availability_zone = "ap-northeast-2c"
}


[root@ip-10-0-0-32 network-config]# more gateway.tf
resource "aws_internet_gateway" "test-gateway" {
vpc_id = "${aws_vpc.test-vpc.id}"
tags = {
Name = "test-gateway"
}
}


[root@ip-10-0-0-32 network-config]# more routes.tf
resource "aws_route_table" "test-route-table" {
vpc_id = "${aws_vpc.test-vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.test-gateway.id}"
}
tags = {
Name = "test-route-table"
}
}
resource "aws_route_table_association" "my-subnet-association" {
subnet_id = "${aws_subnet.test-subnet-c.id}"
route_table_id = "${aws_route_table.test-route-table.id}"
}




<6> 리소스 삭제


terraform  destroy

Enter a value: yes




<7>   AWS  4.13 버전 설정법


https://registry.terraform.io/providers/hashicorp/aws/4.13.0




연관 자료

AWS CLI로  VPC 만들기 https://brunch.co.kr/@topasvga/747

AWS CLI로 서버 만들기 https://brunch.co.kr/@topasvga/872

AWS CLI로 서버 생성법  https://brunch.co.kr/@topasvga/345



https://brunch.co.kr/@topasvga/2421


감사합니다.

매거진의 이전글 (몰아보기) 19탄- 테라폼-AWS-2022
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari