3
3

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.

Terraform入門その三(hello worldの表示)

Last updated at Posted at 2019-06-19

Terraform入門その一(インストール)
Terraform入門その二(AWSのEC2作成)
Terraform入門その三(hello worldの表示)[本記事]
Terraform入門その四(AWSのELB&ASGの導入)

前回AWSのEC2作成方法を記述しました。
続きまして、EC2インスタンスにapacheをインストールしてhello worldをブラウザに表示させます。

最終的なディレクトリ構成図

working
├── main.tf
└── vars.tf

apacheのインストール

main.tf
resource "aws_instance" "example" {
    ami = "ami-04b2d1589ab1d972c"
    instance_type = "t2.micro"

# 追加。user_dataはインスタンス作成の際に、実行されるbashスクリプトです
    user_data = <<-EOF
                #! /bin/bash
                sudo yum update
                sudo yum install -y httpd
                sudo chkconfig httpd on
                sudo service httpd start
                echo "<h1>hello world</h1>" | sudo tee /var/www/html/index.html
                EOF

    tags = {
        Name = "terraform-example"
    }
}

続いて、ブラウザからのアクセスを許可するためsecurity_groupを設定します。
下記はインバウンド(igress)についてはhttp80番ポートの開放、アウトバウンド(egress)は全許可を表します。

main.tf
resource "aws_security_group" "instance" {
    name = "terraform-example-instance"
    ingress {
        from_port = 80
        to_port = 80
        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"]
  }
}

上記で記述した内容をインスタンスに紐づけます。

main.tf
resource "aws_instance" "example" {
    ami = "ami-04b2d1589ab1d972c"
    instance_type = "t2.micro"
# 追加。先程記述したsecutiry_groupをインスタンスに紐づけます
    vpc_security_group_ids = ["${aws_security_group.instance.id}"]

    user_data = <<-EOF
                #! /bin/bash
                sudo yum update
                sudo yum install -y httpd
                sudo chkconfig httpd on
                sudo service httpd start
                echo "<h1>hello world</h1>" | sudo tee /var/www/html/index.html
                EOF

    tags = {
        Name = "terraform-example"
    }
}

設定が完了しました。

下記は全体のmain.tfの記述です。

main.tf
provider "aws" {
  profile    = "default"
  region     = "ap-northeast-1"
}

resource "aws_instance" "example" {
    ami = "ami-04b2d1589ab1d972c"
    instance_type = "t2.micro"
# 追加。先程記述したsecutiry_groupをインスタンスに紐づけます
    vpc_security_group_ids = ["${aws_security_group.instance.id}"]

# 追加。user_dataはインスタンス作成の際に、実行されるbashスクリプトです
    user_data = <<-EOF
                #! /bin/bash
                sudo yum update
                sudo yum install -y httpd
                sudo chkconfig httpd on
                sudo service httpd start
                echo "<h1>hello world</h1>" | sudo tee /var/www/html/index.html
                EOF

    tags = {
        Name = "terraform-example"
    }
}

resource "aws_security_group" "instance" {
    name = "terraform-example-instance"
    ingress {
        from_port = 80
        to_port = 80
        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"]
  }
}

あとはterraform applyを実行し、AWSマネジメントコンソールでパブリックIPの確認→ブラウザからアクセスすればhello worldが表示されます。

リファクタリング

ポート番号の記述は、今後変更する可能性もあります。
今後の変更の容易性を考えて、変数を宣言します。
また、vars.tfに記述することでファイルも分けます。

vars.tf
variable "server_port" {
  description = "The port the server will use for HTTP requests"
  default = 80
}

宣言した変数を、aws_security_groupに組み込みます。"${var.server_port}"

main.tf
resource "aws_security_group" "instance" {
    name = "terraform-example-instance"
    ingress {
        from_port = "${var.server_port}"
        to_port = "${var.server_port}"
        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"]
  }
}

リファクタリング後の全体のソースコード

main.tf
provider "aws" {
  profile    = "default"
  region     = "ap-northeast-1"
}

resource "aws_instance" "example" {
    ami = "ami-04b2d1589ab1d972c"
    instance_type = "t2.micro"
    vpc_security_group_ids = ["${aws_security_group.instance.id}"]

    user_data = <<-EOF
                #! /bin/bash
                sudo yum update
                sudo yum install -y httpd
                sudo chkconfig httpd on
                sudo service httpd start
                echo "<h1>hello world</h1>" | sudo tee /var/www/html/index.html
                EOF

    tags = {
        Name = "terraform-example"
    }
}

resource "aws_security_group" "instance" {
    name = "terraform-example-instance"
    ingress {
        from_port = "${var.server_port}"
        to_port = "${var.server_port}"
        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"]
  }
}
vars.tf
output "elb_dns_name" {
  value = "${aws_elb.example.dns_name}"
}

次回は、ELBとASGの導入を解説していきます。
Terrafrom入門その四(AWSのELB&ASGの導入)

参照

TERRAFORM TUTORIAL - VPC, SUBNETS, ROUTETABLE, ELB, SECURITY GROUP, AND APACHE SERVER II

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?