-
Notifications
You must be signed in to change notification settings - Fork 0
/
bastion.tf
129 lines (103 loc) · 3.51 KB
/
bastion.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
data "google_iam_policy" "bastion_iam_policy" {
binding {
role = "roles/compute.osAdminLogin"
members = [
for email in var.iam_member_emails :
"user:${email}"
]
}
}
resource "google_compute_instance" "bastion" {
name = "${var.env}-bastion-host"
machine_type = "e2-micro"
zone = var.availability_zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-12"
}
}
allow_stopping_for_update = true
network_interface {
network = google_compute_network.main.self_link
subnetwork = google_compute_subnetwork.public.self_link
network_ip = local.ip[var.env].bastion
access_config {
nat_ip = google_compute_address.bastion.address
}
}
service_account {
email = google_service_account.bastion_sa.email
scopes = ["cloud-platform"]
}
metadata = {
enable-oslogin = "FALSE"
ssh-keys = "ludo:${tls_private_key.bastion.public_key_openssh}"
# Startup script to modify /etc/hosts
user-data = <<-EOF
#!/bin/bash
# SSH 키 파일 생성
echo '${tls_private_key.private.private_key_pem}' > /home/ludo/.ssh/id_rsa
chmod 600 /home/ludo/.ssh/id_rsa
# .bashrc에 alias 추가
echo "# Custom SSH aliases" >> /home/ludo/.bashrc
echo "alias conn_alb='ssh ludo@${google_compute_instance.alb.network_interface[0].network_ip}'" >> /home/ludo/.bashrc
echo "alias conn_app='ssh ludo@${google_compute_instance.app.network_interface[0].network_ip}'" >> /home/ludo/.bashrc
echo "alias conn_db='ssh ludo@${google_compute_instance.db.network_interface[0].network_ip}'" >> /home/ludo/.bashrc
# 소유권 및 권한 설정
chown -R ludo:ludo /home/ludo/.ssh /home/ludo/.bashrc
chmod 700 /home/ludo/.ssh
EOF
}
tags = ["bastion"]
}
# resource "google_compute_instance_iam_policy" "bastion_iam" {
# project = var.project_id
# zone = var.availability_zone
# instance_name = google_compute_instance.bastion.name
# policy_data = data.google_iam_policy.bastion_iam_policy.policy_data
# }
# # # IAP를 통한 SSH 접근을 위한 방화벽 규칙
# # resource "google_compute_firewall" "iap_to_bastion" {
# # name = "allow-iap-to-bastion"
# # network = data.google_compute_network.vpc.name
# # allow {
# # protocol = "tcp"
# # ports = ["22"]
# # }
# # source_ranges = ["35.235.240.0/20"] # IAP의 IP 범위
# # target_tags = ["bastion"]
# # }
# resource "google_project_iam_member" "iap_tunnel_users" {
# count = length(var.iam_member_emails)
# project = var.project_id
# role = "roles/compute.osLogin"
# member = "user:${var.iam_member_emails[count.index]}"
# }
# # IAP 사용을 위한 IAM 권한 설정
# resource "google_project_iam_member" "iap_tunnel_user" {
# count = length(var.iam_member_emails)
# project = var.project_id
# role = "roles/iap.tunnelResourceAccessor"
# member = "user:${var.iam_member_emails[count.index]}"
# }
resource "google_service_account" "bastion_sa" {
account_id = "${var.env}-bastion-sa"
display_name = "Bastion SA"
}
resource "google_compute_firewall" "allow_bastion_from_internet" {
name = "${var.env}-allow-bastion-from-internet"
network = google_compute_network.main.name
source_ranges = ["0.0.0.0/0"]
target_tags = ["bastion"]
allow {
protocol = "tcp"
ports = ["22"]
}
allow {
protocol = "icmp"
}
}
resource "google_compute_address" "bastion" {
name = "${var.env}-bastion-ip"
region = var.region
}