4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

EC2をTerraformで操作、ssh

Last updated at Posted at 2015-07-10

参考

その他リンク集


terraformのダウンロード

terraform設定ファイル

  • access_keyとsecret_keyの取得方法は
    • EC2ダッシュボード -> 右上のAWSにようこそ -> 認証情報 -> ユーザ -> ユーザ操作 -> アクセスキーの管理 -> アクセスキーの作成 -> ユーザーのセキュリティ認証情報を表示 -> アクセスキー ID, シークレットアクセスキー
example.et
provider "aws" {
    access_key = "XXXXXXXXXXXXXXXXXXXX"
    secret_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    region = "ap-northeast-1"
}

resource "aws_instance" "example" {
    ami = "ami-cbf90ecb" #AmazonLinux
    #ami = "ami-936d9d93" #Ubuntu
    instance_type = "t2.micro"
    key_name = "aws_key1"
    security_groups = ["${aws_security_group.default.name}"]
}

# SSH access from anywhere
resource "aws_security_group" "default" {
    name = "terraform_example"
    description = "Used in the terraform"
    ingress {
        from_port = 22 #適宜変更
        to_port = 22
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
}
  • remote-exeでコマンド発行やfileでファイル転送ができるようだが自分は成功しなかったので別の方法(itamaeとか)で実施する。

terraformコマンド

適用
./terraform apply
  • applyすると、現在のEC2インスタンスがterminatedになり、新規インスタンスがrunningになる。
変更点確認
./terraform plan
現状確認
./terraform show
削除
./terraform play -destroy
./terraform destroy
# yes を入力。一応EC2ダッシュボードから削除(=terminated状態)か確認しておくこと。

詳細なログを出したい場合は

TF_LOG=1 ./terraform showなど

ssh接続

GlobalIP確認

$ ./terraform show | grep public_ip
  public_ip = 52.69.110.23

AmazonLinux(ami-cbf90ecb)の場合

ssh -i aws_key1.pem ec2-user@52.69.110.23

Ubuntu(ami-936d9d93)の場合

ssh -i aws_key1.pem ubuntu@52.69.110.23

設定の分割

example.tf
provider "aws" {
  access_key = "${var.access_key}"
  secret_key = "${var.secret_key}"
  region = "${var.region}"
}

resource "aws_instance" "example" {
    ami = "${lookup(var.amis, var.region)}"
    instance_type = "${var.instance_type}"
    key_name = "aws"
    security_groups = ["${aws_security_group.default.name}"]
}

resource "aws_security_group" "default" {
    name = "terraform_example"
    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"]
    }
}
variables.tf
variable "access_key" {
  description = "AWS access key"
  default     = "XXXXXXXXXXXXXXXXXXXX"
}

variable "secret_key" {
  description = "AWS secret access key"
  default     = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}

variable "region" {
  description = "AWS region to host your network"
  default     = "ap-northeast-1"
}

variable "instance_type" {
  default = "t2.micro"
}

variable "amis" {
  default = {
    "ap-northeast-1" = "ami-cbf90ecb" #AmazonLinux
    #"ap-northeast-1" = "ami-936d9d93" #Ubuntu14.04
  }
}
実行方法は今までと変わらない(Enterを何度か押す必要はある)
./terraform apply
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?