brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Jan 04. 2020

27. 테라폼- GCP - VPC 만들기

<1> GCP에 VPC를 ISMS-P에 적합한 안전한 네트워크를 구축하고자 한다.

<2> 

<3> 작업 순서

<4> 환경 구성

<5>  검토사항





<1> GCP에 VPC를 ISMS-P에 적합한 안전한 네트워크를 구축하고자 한다.


2시간 걸리는 일을 10분 만에  구축하고자 한다.




<2>

 




<3> 작업 순서



<4> 환경 구성


리눅스 서버로 하는법


1. Centos 7 리눅스 1대 생성

SSH로 접속



2. 테라폼 다운로드

https://www.terraform.io/downloads.html


yum install wget -y

yum install unzip -y


mkdir terra
wget https://releases.hashicorp.com/terraform/0.12.18/terraform_0.12.18_linux_amd64.zip
ls
unzip terraform_0.12.18_linux_amd64.zip

mv terraform /usr/bin/

terraform --help



3.  현재 프로젝트 설정과 확인


현재 프로젝트 확인

gcloud config list project

gcloud config set project just-lore-264018


현재 사용 Account 확인

gcloud auth list


gcloud config set account  topasvga@kakao.com

gcloud config set account 'ACCOUNT'



// 프로젝트 이름과 현재 사용 Account 확인은 Cloud Shell 에서 확인 가능하다.




4.  테라폼 실행을 위한  서비스 계정 생성


권한 설정 생성

API 및 서비스 > 사용자 인증정보




소유자로 생성한다.



terra-access.json


 yum install vsftpd*


/usr/sbin/vsftpd



VPC 네트워크  > 방화벽 규칙 > 방화벽 규칙 만들기  > tcp 21 , udp 20  , 태그 ftp


VM 인스턴스 > 수정 > 테크에 ftp 추가하면 ftp가 허용된다.




6.  환경 설정 파일  providers.tf 설정


root@cloudshell:~/terra (just-lore-264018)$ vi  providers.tf
provider "google" {
version = "~> 2.5"
credentials = "${file("terra-access.json")}"
project = "just-lore-264018"
region = "asia-northeast1"
}



7.  만들고자 하는 리소스에 대한 테라폼 파일 만들기


vpc 만들고자 한다.


main.tf


root@instance-1 terra]# more main.tf

resource "google_compute_subnetwork" "network-with-private-secondary-ip-ranges" {

  name          = "se-test 1 k"

  ip_cidr_range = "10.200.0.0/22"

  region        = "us-central1"

  network       = google_compute_network.custom-test.self_link

  secondary_ip_range {

    range_name    = "tf-test-secondary-range-update1"

    ip_cidr_range = "192.168.10.0/24"

  }

}

resource "google_compute_network" "custom-test" {

  name                    = "test-network"

  auto_create_subnetworks = false

}


https://www.terraform.io/docs/providers/google/r/compute_subnetwork.html



more main.tf


terraform init
terraform plan


terraform apply

Enter a value: yes





8. 삭제


terraform  destroy


Enter a value: yes





<5>  검토사항


Cloud Shell 환경과 테라폼 다운로드 파일과 기본 환경이 맞지 않는다.

아래 Cloud Shell로 하는법의 환경에서 실행하면 잘 된다.


문제점:  Cloud Shell 에서  생성하려 했는데  권한 문제로 오류가 난다.


Error: Failed to instantiate provider "google" to obtain schema: fork/exec /root/terra/.terraform/plugins/linux_amd64/terraform-provider-google_v3.3.0_x5: permission denied


해결 방안:  아래 Cloud Shell을 사용하면 환경을 맞추어 주어 잘 된다.

https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/terraform-google-modules/docs-examples.git&cloudshell_git_branch=oics-blog&cloudshell_working_dir=oics-blog&cloudshell_image=gcr.io/graphite-cloud-shell-images/terraform:latest&cloudshell_print=./motd&cloudshell_tutorial=./tutorial.md&cloudshell_open_in_editor=main.tf



참고사이트

https://www.hashicorp.com/blog/kickstart-terraform-on-gcp-with-google-cloud-shell/


https://github.com/terraform-google-modules/docs-examples





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


감사합니다.















작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari