前回の記事でcloudformationとterraformの比較や紹介をしましたが、terraformがとっつきやすかったため、簡単なサンプルとメモを。
Contents
インストール
1 2 3 |
brew install terraform terraform --version #Terraform v0.12.26などversionが出ればOK |
実行手順
基本知識
プロバイダー
使用されるサービスのことです。今回だとAWSやそのアカウント自体のことです。他にはAzureなど。
プロバイダーは下記のようなファイルや記述をいれておくか、awscliをインストールしておけば後述する terraform init コマンドで読み込ませることができます。
1 2 3 4 5 |
provider "aws" { access_key = "ACCESS_KEY_HERE" secret_key = "SECRET_KEY_HERE" region = "ap-northeast-1" } |
変数の値の設定
1 2 3 4 5 6 7 8 9 10 11 |
variable "aws_access_key" {} variable "aws_secret_key" {} variable "region" { default = "ap-northeast-1" } provider "aws" { access_key = "${var.aws_access_key}" secret_key = "${var.aws_secret_key}" region = "${var.region}" } |
variable 〜で変数設定が可能です。
リソースの設定
実際に設定するサービスをresourceと定義します。
resource "aws_instance" "web"{} のようにリソースの種類、リソース名で定義をします。
具体的なリソースの種類の名前は公式ドキュメントを参考に。
https://www.terraform.io/docs/providers/aws/index.html
コマンド
terraform init
上記コマンドでawsのアカウントを読み込ませることが可能です。
そのあと下記のようにインフラのコードを記述し、 terraform plan で実行準備を行い、 terraform apply で反映させればOKです。
1 2 3 4 5 6 7 8 |
resource "aws_instance" "web1" { ami = "ami-0323c3dd2da7fb37d" #awsの管理画面からイメージのidを参考に。 instance_type = "t2.micro" tags = { Name = "web1" } } |
terraform plan
構文チェックと新しく追加された実行計画を表示します。
具体的には下記のような表示が出力されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.web1 will be created + resource "aws_instance" "web1" { + ami = "ami-0323c3dd2da7fb37d" + arn = (known after apply) + associate_public_ip_address = (known after apply) + availability_zone = (known after apply) + cpu_core_count = (known after apply) + cpu_threads_per_core = (known after apply) + get_password_data = false + host_id = (known after apply) + id = (known after apply) + instance_state = (known after apply) + instance_type = "t2.micro" + ipv6_address_count = (known after apply) + ipv6_addresses = (known after apply) + key_name = (known after apply) + network_interface_id = (known after apply) + outpost_arn = (known after apply) + password_data = (known after apply) + placement_group = (known after apply) + primary_network_interface_id = (known after apply) + private_dns = (known after apply) + private_ip = (known after apply) + public_dns = (known after apply) + public_ip = (known after apply) + security_groups = (known after apply) + source_dest_check = true + subnet_id = (known after apply) + tags = { + "Name" = "web1" } + tenancy = (known after apply) + volume_tags = (known after apply) + vpc_security_group_ids = (known after apply) + ebs_block_device { + delete_on_termination = (known after apply) + device_name = (known after apply) + encrypted = (known after apply) + iops = (known after apply) + kms_key_id = (known after apply) + snapshot_id = (known after apply) + volume_id = (known after apply) + volume_size = (known after apply) + volume_type = (known after apply) } + ephemeral_block_device { + device_name = (known after apply) + no_device = (known after apply) + virtual_name = (known after apply) } + metadata_options { + http_endpoint = (known after apply) + http_put_response_hop_limit = (known after apply) + http_tokens = (known after apply) } + network_interface { + delete_on_termination = (known after apply) + device_index = (known after apply) + network_interface_id = (known after apply) } + root_block_device { + delete_on_termination = (known after apply) + device_name = (known after apply) + encrypted = (known after apply) + iops = (known after apply) + kms_key_id = (known after apply) + volume_id = (known after apply) + volume_size = (known after apply) + volume_type = (known after apply) } } Plan: 1 to add, 0 to change, 0 to destroy. ------------------------------------------------------------------------ Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run. |
terraform apply
実際に環境を構築します。上記だとAWS上でEC2のインスタンスを構築します。コンソール画面を見てますと実際にEC2が立ち上がるのがわかると思います。
他に terraform show 現段階のインフラ環境の確認や terraform destroy 環境の破壊などもあります。
次回以降さらなるVPCなどの環境構築などを書いていこうと思います。