Skip to content

Commit 2860cbe

Browse files
committed
[#1] feat - add vpc resource
- vpc 생성을 위한 리소스 정의(실제 생성에 사용한 tfvars 파일 제외)
1 parent e8e2a2e commit 2860cbe

File tree

6 files changed

+231
-0
lines changed

6 files changed

+231
-0
lines changed

terraform/aws/resources/vpc/README.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# VPC 리소스 생성 코드
2+
3+
AWS Provider 기준 코드
4+
5+
## Installation
6+
7+
사전에 VPC, EC2, RDS 관련 권한이 필요합니다.
8+
9+
필요한 권한
10+
11+
- `VPCFullAccess`
12+
- `RDSFullACcess`
13+
- `AmazonEC2FullAccess`
14+
15+
Instance Profile을 활용하거나
16+
Access Key를 발급해 사용이 가능합니다.
17+
18+
직접 키 값을 넣어 사용하는 경우
19+
20+
```
21+
provider "aws" {
22+
region = "us-west-2"
23+
access_key = "anaccesskey"
24+
secret_key = "asecretkey"
25+
}
26+
```
27+
28+
환경 변수를 이용한 설정
29+
30+
```
31+
export AWS_ACCESS_KEY_ID="anaccesskey"
32+
export AWS_SECRET_ACCESS_KEY="asecretkey"
33+
export AWS_DEFAULT_REGION="ap-northeast-2"
34+
```
35+
36+
## Usage
37+
38+
### VPC 리소스 생성하기
39+
40+
User를 생성하고 엑세스 키 발급 후 Programming access 방식을 이용해 VPC를 생성하는 경우 아래와 같은 User를 생성한다.
41+
42+
![vpc-role](../../../../assets/images/vpc-role.png)
43+
44+
해당 User를 이용해 엑세스 키를 발급하고 아래 내용을 환경 변수에 추가한다.
45+
46+
```bash
47+
export AWS_ACCESS_KEY_ID="anaccesskey"
48+
export AWS_SECRET_ACCESS_KEY="asecretkey"
49+
export AWS_DEFAULT_REGION="ap-northeast-2"
50+
```
51+
52+
아래 내용을 참고해 vpc.tfvars 파일을 구성한다.
53+
54+
### Inputs
55+
56+
| Name | Description | Type | Default | Required |
57+
| ------------------- | ------------------------------------------- | :----: | :---------: | :------: |
58+
| name | 모듈에서 정의하는 모든 리소스 이름의 prefix | string | n/a | yes |
59+
| cidr | VPC에 할당한 CIDR block | string | n/a | yes |
60+
| public_subnets | Public Subnet IP 리스트 | list | n/a | yes |
61+
| private_subnets | Private Subnet IP 리스트 | list | n/a | yes |
62+
| database_subnets | Database Subnet IP 리스트 | list | n/a | yes |
63+
| availability_zones | 사용할 availability zones 리스트 | list | n/a | yes |
64+
| bastion_name | 모듈에서 정의하는 모든 리소스 이름의 prefix | string | n/a | yes |
65+
| instance_type | bastion EC2 instance type | string | `"t2.nano"` | no |
66+
| ec2_ssh_key | bastion이 사용할 keypair name | string | n/a | yes |
67+
| nat_gateway_enable | nat gateway 설정 여부, false인 경우 instance 사용 | string | n/a | yes |
68+
| ingress_cidr_blocks | bastion SSH 접속을 허용할 CIDR block 리스트 | list | n/a | yes |
69+
| tags | 모든 리소스에 추가되는 tag 맵 | map | n/a | yes |
70+
71+
72+
그리고 아래 명령어를 순차적으로 실행 해 Vpc, Subnet, Bastion(NAT)의 리소스들을 생성한다.
73+
74+
해당 이름의 pem 키를 미리 만들어두지 않으면 에러가 발생한다.
75+
76+
```bash
77+
$ git clone https://github.com/tramyu/tram-infrastructure.git
78+
$ cd aws/resources/vpc
79+
$ terraform init
80+
$ terraform plan -var-file=vpc.tfvars
81+
$ terraform apply -var-file=vpc.tfvars
82+
```
83+
84+
### Reference
85+
86+
Vpc 모듈과 Bastion을 생성하는 terraform code는 ausbubam님의 블로그를 참고하였습니다.
87+
88+
[ausbubam blog](https://blog.2dal.com/2017/10/28/aws-vpc-with-terraform-modules/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "aws" {
2+
}
3+
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
variable "name" {
2+
description = "모듈에서 정의하는 모든 리소스 이름의 prefix"
3+
type = string
4+
}
5+
6+
variable "cidr" {
7+
description = "VPC에 할당한 CIDR block"
8+
type = string
9+
}
10+
11+
variable "public_subnets" {
12+
description = "Public Subnet IP 리스트"
13+
type = list(string)
14+
}
15+
16+
variable "private_subnets" {
17+
description = "Private Subnet IP 리스트"
18+
type = list(string)
19+
}
20+
21+
variable "database_subnets" {
22+
description = "Database Subnet IP 리스트"
23+
type = list(string)
24+
}
25+
26+
variable "availability_zones" {
27+
description = "사용할 availability zones 리스트"
28+
type = list(string)
29+
}
30+
31+
variable "bastion_name" {
32+
description = "모듈에서 정의하는 모든 리소스 이름의 prefix"
33+
type = string
34+
}
35+
36+
variable "instance_type" {
37+
description = "bastion EC2 instance type"
38+
default = "t2.nano"
39+
}
40+
41+
variable "ec2_ssh_key" {
42+
description = "bastion이 사용할 keypair name"
43+
type = string
44+
}
45+
46+
variable "ingress_cidr_blocks" {
47+
description = "bastion SSH 접속을 허용할 CIDR block 리스트"
48+
type = list(string)
49+
}
50+
51+
variable "nat_gateway_enable" {
52+
description = "nat gateway enabled"
53+
default = false
54+
}
55+
56+
variable "tags" {
57+
description = "모든 리소스에 추가되는 tag 맵"
58+
type = map(string)
59+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
terraform {
3+
required_version = ">= 0.12"
4+
}

terraform/aws/resources/vpc/vpc.tf

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module "aws_vpc" {
2+
source = "../../modules/vpc"
3+
4+
name = var.name
5+
6+
# VPC의 CIDR block을 정의한다.
7+
cidr = var.cidr
8+
9+
# VPC가 사용할 AZ를 정의한다.
10+
availability_zones = var.availability_zones
11+
12+
# VPC의 Public Subnet CIDR block을 정의한다.
13+
public_subnets = var.public_subnets
14+
15+
# VPC의 Private Subnet CIDR block을 정의한다.
16+
private_subnets = var.private_subnets
17+
18+
# VPC의 Private DB Subnet CIDR block을 정의한다. (RDS를 사용하지 않으면 이 라인은 필요없다.)
19+
database_subnets = var.database_subnets
20+
21+
bastion_id = module.aws_nat_instance_bastion.bastion_id
22+
23+
nat_gateway_enable = var.nat_gateway_enable
24+
25+
# VPC module이 생성하는 모든 리소스에 기본으로 입력될 Tag를 정의한다.
26+
tags = var.tags
27+
}
28+
29+
module "aws_nat_instance_bastion" {
30+
source = "../../modules/nat-instance-bastion"
31+
32+
name = var.bastion_name
33+
34+
vpc_id = module.aws_vpc.vpc_id
35+
36+
instance_type = var.instance_type
37+
availability_zone = var.availability_zones[0]
38+
subnet_id = module.aws_vpc.public_subnets_ids[0]
39+
40+
default_security_group_id = module.aws_vpc.default_security_group_id
41+
42+
ec2_ssh_key = var.ec2_ssh_key
43+
44+
ingress_cidr_blocks = var.ingress_cidr_blocks
45+
46+
private_subnets = var.private_subnets
47+
48+
tags = var.tags
49+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name = ""
2+
3+
# Vpc cidr block
4+
cidr = "0.0.0.0/16"
5+
6+
availability_zones = ["ap-northeast-2a", "ap-northeast-2c"]
7+
8+
# Public subnet cidr block
9+
public_subnets = ["0.0.0.0/24", "0.0.0.0/24"]
10+
11+
# Private subnet cidr block
12+
private_subnets = ["0.0.0.0/24", "0.0.0.0/24"]
13+
14+
# DB subnet cidr block
15+
database_subnets = ["0.0.0.0/24", "0.0.0.0/24"]
16+
17+
tags = {
18+
"TerraformManaged" = "true"
19+
"Creator" = "KimKyuNam"
20+
}
21+
22+
ec2_ssh_key = ""
23+
24+
ingress_cidr_blocks = ["0.0.0.0/24"]
25+
26+
nat_gateway_enable = false
27+
28+
bastion_name = ""

0 commit comments

Comments
 (0)