Skip to content

Commit 224cf1a

Browse files
committed
ecs-fargate-wordpress-rds-terraform added
cloud-run-v2-wordpress-sql-terraform added gitignore added
1 parent 4b2796f commit 224cf1a

File tree

21 files changed

+814
-97
lines changed

21 files changed

+814
-97
lines changed

.gitignore

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1 @@
1-
# Local .terraform directories
2-
**/.terraform/*
3-
4-
# .tfstate files
5-
*.tfstate
6-
*.tfstate.*
7-
8-
# Crash log files
9-
crash.log
10-
crash.*.log
11-
12-
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
13-
# password, private keys, and other secrets. These should not be part of version
14-
# control as they are data points which are potentially sensitive and subject
15-
# to change depending on the environment.
16-
*.tfvars
17-
*.tfvars.json
18-
19-
# Ignore override files as they are usually used to override resources locally and so
20-
# are not checked in
21-
override.tf
22-
override.tf.json
23-
*_override.tf
24-
*_override.tf.json
25-
26-
# Include override files you do wish to add to version control using negated pattern
27-
# !example_override.tf
28-
29-
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
30-
# example: *tfplan*
31-
32-
# Ignore CLI configuration files
33-
.terraformrc
34-
terraform.rc
1+
cloud-run-v2-wordpress-sql-terraform/gcp_key.json

.terraform.lock.hcl

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
resource "google_project_service" "compute" {
2+
project = var.project_id
3+
service = "compute.googleapis.com"
4+
5+
timeouts {
6+
create = "30m"
7+
update = "40m"
8+
}
9+
lifecycle {
10+
ignore_changes = [service]
11+
}
12+
disable_dependent_services = true
13+
disable_on_destroy = false
14+
}
15+
16+
resource "google_project_service" "service_networking" {
17+
project = var.project_id
18+
service = "servicenetworking.googleapis.com"
19+
20+
timeouts {
21+
create = "30m"
22+
update = "40m"
23+
}
24+
lifecycle {
25+
ignore_changes = [service]
26+
}
27+
disable_dependent_services = true
28+
disable_on_destroy = false
29+
}
30+
31+
resource "google_project_service" "sql_admin" {
32+
project = var.project_id
33+
service = "sqladmin.googleapis.com"
34+
35+
timeouts {
36+
create = "30m"
37+
update = "40m"
38+
}
39+
lifecycle {
40+
ignore_changes = [service]
41+
}
42+
disable_dependent_services = true
43+
disable_on_destroy = false
44+
}
45+
46+
resource "google_project_service" "cloud_run" {
47+
project = var.project_id
48+
service = "run.googleapis.com"
49+
50+
timeouts {
51+
create = "30m"
52+
update = "40m"
53+
}
54+
55+
lifecycle {
56+
ignore_changes = [service]
57+
}
58+
59+
disable_dependent_services = true
60+
disable_on_destroy = false
61+
}
62+
63+
resource "google_project_service" "cloud_sql" {
64+
project = var.project_id
65+
service = "sql-component.googleapis.com"
66+
67+
timeouts {
68+
create = "30m"
69+
update = "40m"
70+
}
71+
72+
lifecycle {
73+
ignore_changes = [service]
74+
}
75+
disable_dependent_services = true
76+
disable_on_destroy = false
77+
}
78+
79+
resource "google_project_service" "vpc_access" {
80+
project = var.project_id
81+
service = "vpcaccess.googleapis.com"
82+
83+
timeouts {
84+
create = "30m"
85+
update = "40m"
86+
}
87+
88+
lifecycle {
89+
ignore_changes = [service]
90+
}
91+
disable_dependent_services = true
92+
disable_on_destroy = false
93+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# private zone
2+
resource "google_compute_global_address" "private_ip_address" {
3+
provider = google-beta
4+
5+
name = "private-sql-range"
6+
purpose = "VPC_PEERING"
7+
address_type = "INTERNAL"
8+
prefix_length = 24
9+
network = google_compute_network.vpc.id
10+
}
11+
12+
# private peering
13+
resource "google_service_networking_connection" "private_vpc_connection" {
14+
provider = google-beta
15+
16+
network = google_compute_network.vpc.id
17+
service = "servicenetworking.googleapis.com"
18+
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
19+
20+
depends_on = [google_project_service.service_networking, google_compute_network.vpc]
21+
22+
deletion_policy = "ABANDON"
23+
}
24+
25+
# create random suffix for database
26+
# the name can be reserve until 15 days after removed
27+
resource "random_id" "db_name_suffix" {
28+
byte_length = 4
29+
}
30+
31+
# instance cloudsql
32+
resource "google_sql_database_instance" "sql_db_instance" {
33+
provider = google-beta
34+
35+
name = "wordpress-mysql-${random_id.db_name_suffix.hex}"
36+
database_version = var.cloud_sql_version
37+
region = var.region
38+
deletion_protection = false
39+
40+
settings {
41+
tier = var.cloud_sql_tier
42+
disk_size = var.cloud_sql_size
43+
44+
ip_configuration {
45+
ipv4_enabled = false
46+
require_ssl = false
47+
private_network = google_compute_network.vpc.id
48+
49+
# authorized_networks {
50+
# name = "allow-all"
51+
# value = "0.0.0.0/0" # Allow access from all IP addresses (not recommended for production)
52+
# }
53+
}
54+
55+
backup_configuration {
56+
binary_log_enabled = false
57+
enabled = false
58+
}
59+
}
60+
61+
depends_on = [google_project_service.sql_admin, google_service_networking_connection.private_vpc_connection]
62+
}
63+
64+
# databases
65+
resource "google_sql_database" "sql_db" {
66+
name = var.cloud_sql_database
67+
instance = google_sql_database_instance.sql_db_instance.name
68+
69+
depends_on = [google_sql_database_instance.sql_db_instance]
70+
}
71+
72+
# users
73+
resource "google_sql_user" "sql_user" {
74+
name = var.cloud_sql_user
75+
instance = google_sql_database_instance.sql_db_instance.name
76+
password = var.cloud_sql_password
77+
78+
depends_on = [google_sql_database_instance.sql_db_instance]
79+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
terraform {
2+
required_providers {
3+
google = {
4+
source = "hashicorp/google"
5+
version = "~> 5.21.0"
6+
}
7+
}
8+
}
9+
10+
provider "google" {
11+
credentials = file("gcp_key.json")
12+
project = var.project_id
13+
region = var.region
14+
}
15+
16+
provider "google-beta" {
17+
credentials = file("gcp_key.json")
18+
project = var.project_id
19+
region = var.region
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# vpc values
2+
output "vpc" {
3+
value = google_compute_network.vpc.id
4+
}
5+
6+
# private subnet values
7+
output "subnet-private" {
8+
value = google_compute_subnetwork.subnet_public_wp_tf.id
9+
}
10+
11+
# public subnet values
12+
output "subnet-public" {
13+
value = google_compute_subnetwork.subnet_private_wp_tf.id
14+
}
15+
16+
# firewall values
17+
output "firewall-common" {
18+
value = google_compute_firewall.common.id
19+
}
20+
21+
# Database values
22+
output "database" {
23+
value = google_sql_database_instance.sql_db_instance.ip_address
24+
}
25+
26+
# Output Cloud Run service URL
27+
output "wordpress_url" {
28+
value = google_cloud_run_v2_service.wordpress_service.uri
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
variable "region" {
2+
type = string
3+
default = "us-east1"
4+
}
5+
6+
variable "project_id" {
7+
type = string
8+
default = "sharp-unfolding-417703"
9+
}
10+
11+
// VPC
12+
variable "subnet_public_cidr_block" {
13+
description = "Public Subnet WP"
14+
default = "10.0.1.0/28"
15+
}
16+
17+
variable "subnet_private_cidr_block" {
18+
description = "Private Subnet WP"
19+
default = "10.0.2.0/28"
20+
}
21+
22+
# Database
23+
variable "cloud_sql_version" {
24+
default = "MYSQL_8_0"
25+
}
26+
27+
variable "cloud_sql_tier" {
28+
default = "db-f1-micro"
29+
}
30+
31+
variable "cloud_sql_size" {
32+
default = 10
33+
}
34+
35+
variable "cloud_sql_user" {
36+
default = "wordpress"
37+
}
38+
39+
variable "cloud_sql_password" {
40+
default = "Qwerty1234@"
41+
}
42+
43+
variable "cloud_sql_database" {
44+
default = "wp-db-tf"
45+
}

0 commit comments

Comments
 (0)