Terraform で EC2 に ssh でログイン可能な CentOS7.3 のインスタンスを立てたときの手順メモ
検証環境
Windows10 Home Edition Terraform v0.9.6
Terraform のインストール
ダウンロード
こちらのサイトから zip ファイルをダウンロード。解凍した実行ファイルを PATH の通ったフォルダにコピー
www.terraform.io
terraform の動作確認
$ terraform version Terraform v0.9.6
ディレクトリ構成
+---aws_terraform centos7.tf
設定ファイル
aws_terraform/centos7.tf
provider "aws" { access_key = "MY ACCESS_KEY" secret_key = "MY SECRET_KEY" region = "ap-northeast-1" } resource "aws_instance" "example" { ami = "ami-571e3c30" instance_type = "t2.micro" key_name = "MY KEY_PAIR" security_groups = ["${aws_security_group.centos7_test.name}"] } resource "aws_security_group" "centos7_test" { name = "centos7_test" description = "Used in the terraform" # SSH access from anywhere ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } }
最新の CentOS の AMI ID はこちらのサイトで確認できます
インスタンスへ ssh で接続
AWS コンソール "EC2"→"インスタンス"→"接続" からインスタンスの接続方法を確認
下記のコマンドでログイン(ユーザーIDは "centos" )
$ ssh -i my_key_pair.pem centos@ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com
インスタンス破棄
事前確認
$ terraform plan -destroy -out=.\terraform.tfplan
破棄実行
$ terraform apply .\terraform.tfplan