Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update terraform #156

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/firebase.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004

# Copyright (C) 2004 Sam Hocevar <[email protected]>

# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.

# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

# 0. You just DO WHAT THE FUCK YOU WANT TO.

# WTFPLなので、オープンソース
# よしかわたいき https://github.com/yoshikawa

name: deploy to firebase

on:
Expand Down
10 changes: 0 additions & 10 deletions django/.env

This file was deleted.

4 changes: 2 additions & 2 deletions django/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# 本当は駄目だけど、許してニャン
!.env
# ついに .envをけすことになったにゃん!
.env
tmp
9 changes: 4 additions & 5 deletions django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ ENV PORT 8000
EXPOSE 8000
RUN apt-get update

RUN apt-get install -y libgl1-mesa-glx
RUN apt-get install libx264-dev
RUN apt-get install -y ffmpeg
RUN apt-get install -y libgl1-mesa-glx libx264-dev \
ffmpeg nasm yasm libmp3lame-dev \
libopus-dev libvorbis-dev libvpx-dev

ENV PYTHONUNBUFFERED 1
RUN mkdir /code

WORKDIR /code

RUN bash install-ffmpeg.sh

COPY requirements.txt /code/
RUN pip3 install -r requirements.txt
COPY . .
# RUN bash install-ffmpeg.sh

CMD ["python", "./manage.py", "runserver", "0.0.0.0:8000"]
14 changes: 9 additions & 5 deletions django/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@
'example.middleware.AuthMiddleware',
]

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = [
"http://localhost:3000", # TODO: デプロイ時変更
"https://a4shittyo-frontend.web.app",
"https://a4shittyo-frontend.firebaseapp.com"
]

ROOT_URLCONF = 'example.urls'
Expand Down Expand Up @@ -102,18 +106,18 @@
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': env('DATABASE_NAME'), #  作成したデータベース名
'PASSWORD': env('DATABASE_PASSWORD'),
'USER': env('DATABASE_USER'), # ログインユーザー
'HOST': env('DATABASE_HOST'), # コンテナ名
'NAME': 'test', #  作成したデータベース名
'PASSWORD': 'test',
'USER': 'test', # ログインユーザー
'HOST': env('db_private_ip'), # コンテナ名
'PORT': '3306',
}
}

SESSION_ENGINE = 'redis_sessions.session'

SESSION_REDIS = {
'host': env('REDIS_HOST'),
'host': env('redis_host'),
'port': 6379,
'db': 0,
'prefix': 'session',
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: '3.7'
version: "3.7"

services:
db:
image: mysql:latest
image: mysql:5.7
restart: always
container_name: mysql
environment:
Expand All @@ -17,7 +17,7 @@ services:
volumes:
- mysqldata:/var/lib/mysql
- ./.docker/mysql/conf:/etc/mysql/conf.d

redis:
image: redis:latest
ports:
Expand Down
Binary file added images/terraform.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 16 additions & 2 deletions terraform/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# Deploy it with Terraform

### デプロイ先の構成要素

![terraform-build](../images/terraform.png)

### Contributors

[Yoshikawa Taiki](https://github.com/yoshikawa)

### Docker Image を gcr にアップロード

```sh
cd ../django # DjangoのDockerfileをビルドしたいので
gcloud builds submit --tag gcr.io/a4shittyo-app/django
vim .env # .env でsendgridのアカウント、パスワードを指定してください
gcloud builds submit --timeout=1800s --tag gcr.io/a4shittyo-app/django
```

### GCP の初期設定
Expand Down Expand Up @@ -46,10 +55,15 @@ gcloud projects add-iam-policy-binding $PROJECT_ID \
gcloud iam service-accounts keys create CREDENTIALS_FILE.json --iam-account=terraform@$PROJECT_ID.iam.gserviceaccount.com --project $PROJECT_ID
```

**Google API が足りないというエラーが出たら、URL 先から APIwo 有効化してください**
**Google API が足りないというエラーが出たら、URL 先から API を有効化してください**

```sh
terraform init
terraform plan
terraform apply
```

### References

[Build it with Terraform](https://hackersandslackers.com/terraform-with-google-cloud/)
[Cloud Run から Cloud SQL や Memorystore への繋ぎ方](https://allabout-tech.hatenablog.com/entry/2020/06/30/133859)
36 changes: 26 additions & 10 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004

# Copyright (C) 2004 Sam Hocevar <[email protected]>

# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.

# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

# 0. You just DO WHAT THE FUCK YOU WANT TO.

# WTFPLなので、オープンソース
# よしかわたいき https://github.com/yoshikawa
locals {
database_version = "MYSQL_5_7" # "MYSQL_5_7"
network = "default" # Network name
network = "default" # Network name
region = "asia-northeast1" # asia-northeast1
project_id = "a4shittyo-app" # GCP Project ID
db_instance_name = "testdayo"
db_instance_name = "murikamo"
}

// Configure the Google Cloud provider
Expand Down Expand Up @@ -33,15 +49,15 @@ module "cloudrun" {
network = local.network
project = local.project_id
region = local.region
database_version = local.database_version
database_version = local.database_version
database_instance = local.db_instance_name
instance_name = local.db_instance_name
display_name = "redis" # Display Name
name = "redis-sittyo" # Instance name
location = ""
redis_version = "REDIS_5_0" # 5.0
size = "1" # 1
tier = "STANDARD_HA" # STANDARD_HA
instance_name = local.db_instance_name
display_name = "lastredis" # Display Name
name = "lastredis" # Instance name
location = ""
redis_version = "REDIS_5_0" # 5.0
size = "1" # 1
tier = "STANDARD_HA" # STANDARD_HA
}

# module "memorystore" {
Expand Down
55 changes: 36 additions & 19 deletions terraform/modules/cloudrun/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004

# Copyright (C) 2004 Sam Hocevar <[email protected]>

# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.

# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

# 0. You just DO WHAT THE FUCK YOU WANT TO.

# WTFPLなので、オープンソース
# よしかわたいき https://github.com/yoshikawa
locals {
db_network = join("/", ["projects", var.project, "global", "networks", var.network])
}
Expand All @@ -8,6 +24,7 @@ resource "google_compute_global_address" "private_ip_address" {
# name = var.private_ip_name
name = "private-ip-address"
purpose = "VPC_PEERING"
address = "10.2.0.0"
address_type = "INTERNAL"
prefix_length = 16
network = "projects/${var.project}/global/networks/default"
Expand All @@ -17,7 +34,7 @@ resource "google_compute_global_address" "private_ip_address" {
resource "google_service_networking_connection" "private_vpc_connection" {
provider = google-beta

network = "projects/${var.project}/global/networks/default"
network = "projects/${var.project}/global/networks/default"
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
}
Expand All @@ -27,7 +44,7 @@ resource "google_cloud_run_service" "mywebapp" {
project = var.project
location = var.region

depends_on = [
depends_on = [
google_vpc_access_connector.connector,
google_redis_instance.cache
]
Expand All @@ -40,15 +57,15 @@ resource "google_cloud_run_service" "mywebapp" {
container_port = 8000
}
env {
name = "redis_host"
name = "redis_host"
value = google_redis_instance.cache.host
}
env {
name = "redis_port"
name = "redis_port"
value = google_redis_instance.cache.port
}
env {
name = "db_private_ip"
name = "db_private_ip"
value = google_sql_database_instance.instance.private_ip_address
}
}
Expand All @@ -71,7 +88,7 @@ resource "google_sql_database_instance" "instance" {
provider = google-beta

database_version = var.database_version
project = var.project
project = var.project
name = var.instance_name
region = var.region

Expand All @@ -95,28 +112,28 @@ resource "google_sql_user" "users" {
}

resource "google_redis_instance" "cache" {
display_name = var.display_name
name = var.name
memory_size_gb = var.size
location_id = var.location
project = var.project
redis_version = var.redis_version
region = var.region
tier = var.tier
display_name = var.display_name
name = var.name
memory_size_gb = var.size
location_id = var.location
project = var.project
redis_version = var.redis_version
region = var.region
tier = var.tier
}


# Add a private VPC connector to for private access from Cloud Run to CloudSQL(MySQL) and Memorystore(redis).
resource "google_vpc_access_connector" "connector" {
provider = google-beta

name = "connector"
ip_cidr_range = "10.0.0.0/28"
region = var.region
network = "default"
project = var.project
region = var.region
network = "default"
project = var.project

depends_on = [
google_vpc_access_connector.connector
]
}
}
19 changes: 18 additions & 1 deletion terraform/modules/cloudrun/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004

# Copyright (C) 2004 Sam Hocevar <[email protected]>

# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.

# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

# 0. You just DO WHAT THE FUCK YOU WANT TO.

# WTFPLなので、オープンソース
# よしかわたいき https://github.com/yoshikawa

variable "project" {
description = "Project ID"
}
Expand Down Expand Up @@ -54,4 +71,4 @@ variable "size" {

variable "tier" {
description = "Service Tier"
}
}
17 changes: 17 additions & 0 deletions terraform/modules/cloudsql/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004

# Copyright (C) 2004 Sam Hocevar <[email protected]>

# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.

# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

# 0. You just DO WHAT THE FUCK YOU WANT TO.

# WTFPLなので、オープンソース
# よしかわたいき https://github.com/yoshikawa

resource "google_sql_database_instance" "instance" {
provider = google-beta

Expand Down
Loading