2019年9月22日日曜日

terraform workspaceとは?

terraformのworkspaceとは?

簡単に言うと、terraformで行った操作や、状態のデータなどを保持する場所です。

terraform workspaceのコマンド一覧は以下の通りです。
$ terraform workspace -h
Usage: terraform workspace

  New, list, show, select and delete Terraform workspaces.

Subcommands:
    delete    Delete a workspace
    list      List Workspaces
    new       Create a new workspace
    select    Select a workspace
    show      Show the name of the current workspace
  • terraform workspace delete:ワークスペースを削除
  • terraform workspace list:ワークスペースの一覧を表示する
  • terraform workspace new:ワークスペースを新規作成
  • terraform workspace select:ワークスペースを選択する
  • terraform workspace show:現在作業中(選択された)ワークスペースの名前を表示

terraform initで最初にワークスペースを初期化した直後に、defaultという名前のワークスペースが作られます。

2019年9月20日金曜日

terraformでRoute53にレコードを追加しようとしたら、「but it already exist」のエラーが発生しました

terraformを使って、Route53にロードバランサーのレコードを追加しようとしたら、エラーが起きました。
使ったソースコードは以下の通りです。
resource "aws_route53_record" "www" {
  zone_id = "Z1234txxxxx"
  name    = "test.com"
  type    = "A"

  alias {
    name                   = "${aws_lb.elb.dns_name}"
    zone_id                = "${aws_lb.elb.zone_id}"
    evaluate_target_health = true
  }
}
調査したところ、タイプ「A」のレコードは一件しか存在できないです。追加ではなく、もし古いレコードが存在していればそれを削除しておく必要があります。
解決方法はとても簡単です。
引数allow_overwrite = trueを追加すれば、もしレコードが存在している場合は上書きしてくれます。
変更後のソースコードは以下になります。
resource "aws_route53_record" "www" {
  zone_id = "Z1234txxxxx"
  name    = "test.com"
  type    = "A"
  allow_overwrite = true

  alias {
    name                   = "${aws_lb.elb.dns_name}"
    zone_id                = "${aws_lb.elb.zone_id}"
    evaluate_target_health = true
  }
}

2019年9月19日木曜日

gitのエラー「fatal: refusing to merge unrelated histories」の解消方法

 git push -u origin masterでソースコードをリモートにpushしようとしたら、以下のエラーがおきました。

$ git push -u origin master
To https://git.xxxx.com/test/xxx.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://git.xxxx.com/test/xxx.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

どうや先にpullしないとだめみたいです。$ git pull origin masterでpullをしたら、今度またエラーがおきました。
fatal: refusing to merge unrelated histories
解決方法は、オプション--allow-unrelated-historiesを追加することです。

以下で無事に解決できました。
$ git pull origin master --allow-unrelated-histories
From https://git.xxxx.com/test/xxx.git
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 README.md | 2 ++

 1 file changed, 2 insertions(+)