GithubHelp home page GithubHelp logo

asmtal / qiita-stocker-terraform Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nekochans/qiita-stocker-terraform

0.0 0.0 0.0 300 KB

https://www.mindexer.net のインフラ構成を管理するプロジェクト

Home Page: https://www.mindexer.net

License: MIT License

JavaScript 0.60% TypeScript 15.37% HCL 84.02%

qiita-stocker-terraform's Introduction

qiita-stocker-terraform

事前準備

AWSクレデンシャルの設定

自身の利用しているMacOS PC上で brew install awscli を実行します。

aws configure --profile qiita-stocker-dev を実行します。

対話式のインターフェースに以下を入力します。

AWS Access Key ID [None]: `アクセスキーIDを入力`
AWS Secret Access Key [None]: `シークレットアクセスキーを入力`
Default region name [None]: ap-northeast-1
Default output format [None]: json

同じく aws configure --profile qiita-stocker-dev を実行します。

先程と同じように対話式のインターフェースに従い必要な情報を入力します。

~/.aws/credentials に下記のように設定されていればOKです。

[qiita-stocker-dev]
aws_access_key_id = 開発・ステージング用アクセスキーID
aws_secret_access_key = 開発・ステージング用シークレットアクセスキー
[qiita-stocker-prod]
aws_access_key_id = 本番用アクセスキーID
aws_secret_access_key = 本番用シークレットアクセスキー

brew install awscli を実行しない場合でもともかく ~/.aws/credentials が上記の状態になっていればOKです。

terraformを実行に必要なconfigファイルを作成する

本プロジェクトは開発・ステージングと本番用のAWSアカウントを使い分けることを想定しています。

その為以下のファイルはGitの管理対象外となっています。

  • backend.tf
  • provider.tf
  • terraform.tfvars

以下のコマンドを実行することで、それぞれの環境に応じた設定ファイルを作成出来ます。

仕組みとしてはAWS SecretsManagerにある機密情報にアクセスを行い、それを元に設定ファイルを作成しています。

開発・ステージング用のconfigファイルを作成

yarn run createConfig:dev

本番用のconfigファイルを作成

yarn run createConfig:prod

設定ファイルに関する注意点

設定ファイルの切り替えを行った後は terraform init の実行が必要になります。

ステージングと本番両方で作業する際に混乱する可能性もあります。

以下のように本Gitリポジトリを開発・ステージング用、本番用と分けて作業すると良いかもしれません。

$ git clone [email protected]:nekochans/qiita-stocker-terraform.git dev-qiita-stocker-terraform
$ git clone [email protected]:nekochans/qiita-stocker-terraform.git prod-qiita-stocker-terraform

こうしておくと一度 createConfig を実行してしまえば、設定ファイルの内容に変更がない限りはそのまま各環境で作業が可能です。

コーディング規約

以下の命名規則に従って命名します。

項目名 命名規則
ファイル名 ケバブケース
ディレクトリ名 ケバブケース
リソースID スネークケース
リソース名 ケバブケース
変数名 スネークケース

リソースIDというのは resourcedata 等のTerraformの予約語に付けられる名前です。

resource "aws_security_group_rule" "ssh_from_all_to_bastion" {
}

リソース名はそのリソースの中で一意になっている必要がある値です。

下記の例だと key_name がそれに該当します。

resource "aws_key_pair" "ssh_key_pair" {
  public_key = "${file(var.ssh_public_key_path)}"
  key_name   = "${terraform.workspace}-ssh-key"
}

他にもタグ名を良く付ける事がありますが、それもこちらのルールの通りケバブケースで命名します。

  tags {
    Name = "${lookup(var.bastion, "${terraform.env}.name", var.bastion["default.name"])}-1a"
  }

このようなややこしい規則になっている理由ですが、RDSCluster等、一部のリソース名で _ が禁止文字に設定されている為です。

他にもインデント等細かいルールがありますが、こちらに関しては terraform fmt を実行すれば自動整形されるのでこれを利用すれば良いでしょう。

terraform fmt は必ずプロジェクトルートで実行を行ってください。

そうしないと全ての .tf ファイルに修正が適応されません。

qiita-stocker-terraform's People

Contributors

keitakn avatar kobayashi-m42 avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.