Terraform - Deploy Server - Single Server

做一些简单的联系,Deploy一些不同的Server。

Pre-condition

为了让Terraform和AWS工作,必须把AWS User的AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY加到环境变量。

1
2
set AWS_ACCESS_KEY_ID=(your access key id)
set AWS_SECRET_ACCESS_KEY=(your secret access key)

Single Server

  1. 创建Config文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    provider "aws" {
    region = "us-east-1"
    }

    resource "aws_instance" "yongfeiuall" {
    ami = "ami-1853ac65"
    instance_type = "t2.micro"
    tags{
    Name = "simple single server"
    }
    }
  2. 执行命令terraform planterraform apply

  3. 验证成功

Single Web Server

  1. 创建Config文件
    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
    provider "aws" {
    region = "ap-northeast-1"
    }

    resource "aws_instance" "yongfeiuall" {
    ami = "ami-28ddc154"
    instance_type = "t2.micro"

    tags{
    Name = "simple web server"
    }
    user_data = <<-EOF
    #!/bin/bash
    yum update -y
    yum install -y httpd
    service httpd start
    echo '<html><h1> single web server from terraform </h1></html>' > /var/www/html/index.html
    EOF

    vpc_security_group_ids = ["${aws_security_group.http.id}"]
    }

    resource "aws_security_group" "http" {
    name = "yonfeiuall_single_web"

    # HTTP access from anywhere
    ingress {
    from_port = 80
    to_port = 80
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    }

    # Outbound internet access
    egress {
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
    }
    }

说明:

  • AWS默认不允许任何Incoming和Outcoming,我们要建一个Security Group(要同时有Inbound和Outbound)
  • 把SG要添加到EC2上,用到SG的ID,Terraform里,用"${TYPE.NAME.ATTRIBUTE}"语法来引用其他Resources的Attribute
  • The <<-EOF and EOF are allows you to create multiline strings without having to insert newline characters all over the place.
  1. 执行命令terraform planterraform apply
  2. 验证成功
    1
    2
    yongfeiuall@automation:~$ curl http://13.113.195.209
    <html><h1> single web server from terraform </h1></html>

用Browser打开,可以看到。

Configurable Web Server

为了更好的管理,Terraform允许定义Input变量:

1
2
3
variable "NAME" {
[CONFIG ...]
}

The body of the variable declaration can contain three parameters, all of them
optional:

  • description
    Use this parameter to document how a variable is used.
  • default
    There are a number of ways to provide a value for the variable.
  • type
    Must be one of “string”, “list”, or “map”.
    E.g.,
    1
    2
    3
    4
    5
    variable "list_example" {
    description = "An example of a list in Terraform"
    type = "list"
    default = [1, 2, 3]
    }

用下面的方式来读取变量"${var.VARIABLE_NAME}"

同时,Terraform还允许定义Output变量:

1
2
3
output "NAME" {
value = VALUE
}

可以返回创建Instance后你想获得的一些属性,例如:

1
2
3
output "public_ip" {
value = "${aws_instance.example.public_ip}"
}

  1. 创建Config文件

    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
    variable "server_port" {
    description = "The port the server will use for HTTP requests"
    default = 80
    }
    output "public_ip" {
    value = "${aws_instance.yongfeiuall.public_ip}"
    }

    provider "aws" {
    region = "ap-northeast-1"
    }

    resource "aws_instance" "yongfeiuall" {
    ami = "ami-28ddc154"
    instance_type = "t2.micro"

    tags{
    Name = "configurable web server"
    }
    user_data = <<-EOF
    #!/bin/bash
    yum update -y
    yum install -y httpd
    service httpd start
    echo '<html><h1> configurable web server from terraform </h1></html>' > /var/www/html/index.html
    EOF

    vpc_security_group_ids = ["${aws_security_group.http.id}"]
    }

    resource "aws_security_group" "http" {
    name = "yonfeiuall_single_web"

    # HTTP access from anywhere
    ingress {
    from_port = "${var.server_port}"
    to_port = "${var.server_port}"
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    }

    # Outbound internet access
    egress {
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
    }
    }
  2. 执行命令terraform planterraform apply

  3. 验证成功
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!