Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Atlas Tutorial | Atlas+TerraformでAWSのインスタンスを作成する

f:id:tbpg:20150819232628j:plain

Atlas+TerraformでAWSインスタンスを作成する

手順

サインイン

Atlasにサインインします。

Tutorialのメニュー選択

Create and manage infrastructure with Terraformを選択します

f:id:tbpg:20150826225819p:plain

Install Packer

Terraformのバージョンを確認します。
ver 0.4.1以上が必要です。

$ terraform version
Terraform v0.6.3

Create Terraform project

Terraformプロジェクトの存在を確認されます。
Noと答えると、Exampleリポジトリのcloneの手順が表示されるので、
そのコマンドを実行します。

$ git clone "https://github.com/hashicorp/atlas-terraform-tutorial.git"
cd atlas-terraform-tutorial

Generate Token

APIトークンを生成します。

set environment

環境変数に取得したAPIトークンを設定します。

$ export ATLAS_TOKEN="your token"

Verify

$ curl "https://atlas.hashicorp.com/ui/tutorial/check?access_token=$ATLAS_TOKEN"
Success! Now go to your browser to move on to the next step.

Terraformのremote stateをAtlasに保存します

$ terraform remote config -backend-config "name=tbpgr/example"
Initialized blank state with remote state enabled!

Terraformの設定をAtlasにPushする

$ terraform push -name "tbpgr/example"
var.access_key
  Please enter your AWS access key

  Enter a value: input your access key

var.secret_key
  Please enter your AWS secret key

  Enter a value: input your secret key

The following variables will be set or overwritten within Atlas from
their local values. All other variables are already set within Atlas.
If you want to modify the value of a variable, use the Atlas web
interface or set it locally and use the -overwrite flag.


  * access_key
  * secret_key

Uploading Terraform configuration...
Configuration "tbpgr/example" uploaded! (v1)

AWSのアクセスキーが必要です

AtlasのEnvironmentsを確認

f:id:tbpg:20150826225538p:plain

Confirm & Applyボタン

tfの内容をEC2に適用します。

結果を確認します

Atlasの画面実行結果

f:id:tbpg:20150826225545p:plain

EC2の管理コンソールの確認結果

f:id:tbpg:20150826225551p:plain

ブラウザの確認結果

f:id:tbpg:20150826225557p:plain

tfファイルを変更します

EC2のインスタンスのtags.Nameの設定を追加してみます。

  • example.tf
#--------------------------------------------------------------
# Instance
#--------------------------------------------------------------
resource "aws_instance" "main" {
    instance_type = "t2.micro"

    # Trusty 14.04
    ami = "ami-2a734c42"

    # This will create 1 instances
    count = 1
    # 追加内容
    tags = {
      Name = "Test tag name"
    }
    subnet_id = "${aws_subnet.main.id}"
    security_groups = ["${aws_security_group.allow_all.id}"]
}

# 略

Atlasに変更をPush

$ terraform push -name "tbpgr/example"

AtlasのEnvironmentsを確認

変更内容がコンソールログに表示されていることを確認できます。

f:id:tbpg:20150826225607p:plain

Confirm & Applyボタン

tfの内容をEC2に適用します。

結果を確認します

EC2の管理コンソールの確認結果

Tagの内容がNameに表示されていることを確認できます。 f:id:tbpg:20150826225612p:plain

AtlasのEnvironmentsで履歴を確認

stateの履歴を確認できます

f:id:tbpg:20150826225619p:plain