デプロイメント¶
本章では、サービスのデプロイメントについて、解説します。
AWS環境のデプロイ¶
AWS環境の構築には、Terraform を使用します。
terraformディレクトリには、プロジェクトのAWS環境をデプロイするためのコードが含まれています。
.
├── Makefile
├── README.md
├── infrastructure
│ ├── Makefile
│ ├── README.md
│ ├── oqtopus-dev
│ └── modules
└── service
├── Makefile
├── README.md
├── oqtopus-dev
└── modules
7 directories, 6 files
infrastructureディレクトリには、ネットワークやデータストアなどのインフラ環境をデプロイするためのコードが含まれています。 一方で比較的頻繁に設定が変わる層は、serviceディレクトリに切り出しています。
まずはネットワークやデータストアなどのインフラ環境をデプロイするための手順について説明します。
環境変数ファイルの生成¶
環境変数ファイルを生成するために、以下のコマンドを実行します:
cd terraform
make generate-env
このコマンドで、4つの環境変数ファイルが作成されます:
.
├── infrastructure
│ └── oqtopus-dev
│ ├── oqtopus-dev.tfbackend
│ └── terraform.tfvars
└── service
└── oqtopus-dev
├── oqtopus-dev.tfbackend
└── terraform.tfvars
インフラ層のデプロイ¶
terraform/infrastructure/oqtopus-devが各環境のデプロイメントディレクトリです。 stateファイルはS3で管理されるため、S3バケットを作成する必要があります。 以下のコマンドを実行して、S3バケットを作成します。
aws s3api create-bucket --bucket tfstate.oqtopus-oqtopus-dev --profile oqtopus-dev --region ap-northeast-1 --create-bucket-configuration LocationConstraint=ap-northeast-1
standby環境の場合は以下のようにバケットを作成します:
aws s3api create-bucket --bucket tfstate.oqtopus-oqtopus-standby --profile oqtopus-standby --region ap-northeast-3 --create-bucket-configuration LocationConstraint=ap-northeast-3
次に、terraformの設定ファイルを用意します。以下の2つのファイルを編集します。
```hcl:infrastructure/oqtopus-dev/oqtopus-dev.tfbackend
infrastructure/oqtopus-dev.tfbackend¶
bucket = "tfstate.oqtopus-oqtopus-dev" key = "infrastructure.tfstate" encrypt = true profile = "oqtopus-dev" region = "ap-northeast-1" use_lockfile = true
standby環境の場合:
```hcl:infrastructure/oqtopus-prod/oqtopus-prod.tfbackend
# infrastructure/oqtopus-prod.tfbackend
bucket = "tfstate.oqtopus-oqtopus-standby"
key = "infrastructure/oqtopus-prod/terraform.tfstate"
region = "ap-northeast-3"
```hcl:infrastructure/oqtopus-dev/terraform.tfvars
infrastructure/terraform.tfvars¶
product="oqtopus" org="oqtopus" env="dev" region = "ap-northeast-1"
enable_guardduty = false enable_guardduty_s3_protection = false
それぞれ、stateファイルの保存先と、環境変数を設定しています。
`terraform init`で初期化を行います。以下のコマンドを実行します。
```bash
cd infrastructure/oqtopus-dev
terraform init -backend-config=oqtopus-dev.tfbackend
その後terrafom applyでデプロイを行います。
terraform apply
サービス層のデプロイ¶
次に、サービスのデプロイについて説明します。
先ほどと同様に、terraformの設定ファイルを用意します。以下の2つのファイルを編集します。
```hcl:service/oqtopus-dev/oqtopus-dev.tfbackend
service/oqtopus-dev.tfbackend¶
bucket = "tfstate.oqtopus-oqtopus-dev" key = "service.tfstate" encrypt = true profile = "oqtopus-dev" region = "ap-northeast-1" use_lockfile = true
standby環境の場合:
```hcl:service/oqtopus-prod/oqtopus-prod.tfbackend
# service/oqtopus-prod.tfbackend
bucket = "tfstate.oqtopus-oqtopus-standby"
key = "service/oqtopus-prod/terraform.tfstate"
region = "ap-northeast-3"
```hcl:service/oqtopus-dev/terraform.tfvars
service/terraform.tfvars¶
product = "oqtopus" org = "oqtopus" env = "dev" region = "ap-northeast-1" state_bucket = "tfstate.oqtopus-oqtopus-dev" remote_state_key = "infrastructure.tfstate" profile = "oqtopus-dev"
waf_enable_common_rules = false waf_enable_rate_limiting = false waf_rate_limit = 1000 waf_cloudwatch_metrics_enabled = false waf_sampled_requests_enabled = false
repository = "oqtopus-cloud" github_user = "oqtopus-team" branch = "develop" aws_account_id = "ここにAWSアカウントIDを記述"
`terraform init`で初期化を行います。以下のコマンドを実行します。
```bash
cd service/oqtopus-dev
terraform init -backend-config=oqtopus-dev.tfbackend
アプリケーションのデプロイ¶
マルチアカウント構成¶
マルチアカウントでのデプロイメントを行うために環境ごとにディレクトリを分割しています。
├── README.md
├── oqtopus-dev
│ ├── Makefile
│ └── .env
└── foo-dev
├── Makefile
└── .env
続いて、各ディレクトリの環境変数の設定とデプロイ方法について説明します。
環境変数の設定¶
サービスをデプロイする前に、次の内容で .env ファイルを作成する必要があります。
PROFILE=foo-dev
CLIENT_API_URL=https://foo-bar.execute-api.ap-northeast-1.amazonaws.com
CLIENT_COGNITO_USER_POOL_ID=ap-northeast-1_foobar
CLIENT_COGNITO_CLIENT_ID=foobar
CLIENT_COGNITO_USER_NAME=foobar
CLIENT_COGNITO_USER_PASSWORD=FooBar@123
SERVER_API_URL=https://baz-qux.execute-api.ap-northeast-1.amazonaws.com
SERVER_COGNITO_USER_POOL_ID=ap-northeast-1_bazqux
SERVER_COGNITO_CLIENT_ID=bazqux
SERVER_COGNITO_USER_NAME=bazqux
SERVER_COGNITO_USER_PASSWORD=BazQux@123
ディレクトリ構成は以下のようになります:
foo-dev
├── .env
└── Makefile
サービスのデプロイ¶
デプロイするには、次のコマンドを実行します:
make deploy-usr
make deploy-provider
サービスのテスト¶
サービスのテストを行うには、次のコマンドを実行します:
make test-user
make test-provider
リリース¶
バージョニングはセマンティックバージョニングを採用しています。
リリースノートの作成は自動化しているため、タグ打ちをすることでリリースノートの作成が行われます。
リリースはmainブランチで行うため、事前にmainブランチに切り替えておいてください。
手順¶
- リリース用のPRを確認し、
main←developマージする。このとき、コミットログを整えるためにCreate Merge Commitを選択する。 - ターミナルで以下のコマンドを実行する。(バージョン番号は適宜変更してください)
git checkout main
git tag v0.1.0
git push origin v0.1.0
コマンド一覧¶
make help
Usage: make [target]
Available targets:
all-user Deploy User API Lambda Package and Test
all-provider Deploy Provider API Lambda Package and Test
all Deploy All Lambda Packages and Test
clean Clean up Binaries
deploy-all Deploy All Lambda Packages
deploy-user Deploy User API Lambda Package
deploy-provider Deploy Provider API Lambda Package
help Show this help message
test-all Test All APIs(connect to the dev environment)
test-user Test User API(connect to the dev environment)
test-provider Test Provider API API(connect to the dev environment)
zip-all Build All Lambda Packages
zip-user Build User API Lambda Package
zip-provider Build Provider API Lambda Package
GuardDutyの設定¶
enable_guardduty - trueに設定すると、AWS環境の監視および分析機能を提供するGuardDutyサービスが有効になります。不正アクセス試行、侵害されたインスタンスや認証情報、データ流出の試みなどの脅威を検出します。
enable_guardduty_s3_protection - trueに設定すると、S3バケットの監視に特化したGuardDutyディテクターの拡張機能が有効になります。不審なダウンロード、侵害された認証情報、または異常なアクセスパターンを検出できます。
WAFの設定¶
waf_enable_common_rules - SQLインジェクション、XSS、パストラバーサル、悪意のあるヘッダー、不正な形式のリクエストなど、最も一般的なHTTP攻撃に対する包括的な保護を提供します。
waf_enable_rate_limiting - レート制限機能を有効にし、ボットやAPIの乱用に対する一般的な保護を提供します。
waf_rate_limit - 1つのソース(IP)からの5分間の最大リクエスト数を指定します。この数を超えると、WAFはそれ以降のリクエストをブロックします。
waf_cloudwatch_metrics_enabled - trueに設定すると、WAFは処理されたリクエストに関するメトリクスをCloudWatchに送信します。
waf_sampled_requests_enabled - trueに設定すると、WAFは処理したサンプルリクエストを保持します。これは分析に使用でき、主にデバッグ目的で使用されます。