專案

一般

配置概況

Terraform建立AWS EC2 Instance

利用Iac自動化腳本部署資源和手動操作AWS管理主控台的步驟是一樣的,本例欲建立一個EC2 Instance,這牽涉到的是虛擬機器安裝在哪一個VPC裡面的哪一個Subnet裡?同時也要指定搭配虛擬機器的磁碟區、安全群組、金鑰對...等資源,以下範例我們排除Default配置,配置一個全新客製化的環境給所需EC2。

先決條件:

  • 已安裝 terraform
  • 已安裝 awc cli
  • 已設定 aws configure 並擁有創建aws相關資源的使用者權限

Terrafrom初始化

隨意建立一個空目錄給terraform專案使用(本例為 /home/test/terrform ),進入該目錄後,參考以下code編寫成 terraform-provider.tf,將provider指定為Amazon aws並選定資源所在區域,然後進行terraform初始化

[test@host terraform]$ cat terraform-provider.tf
provider "aws" {
  region     = "ap-northeast-1"
}
[test@host terraform]$ terraform init

VPC

參考以下code編寫成 vpc.tf ,替基礎建設規劃一個專用的VPC和Subnet(網段不能與default-VPC衝突),同時為了讓該VPC能上網,還需建立一個Internet gateway,並將其加入路由。

[test@host terraform]$ cat vpc.tf
# VPC
resource "aws_vpc" "example" {
  enable_dns_hostnames    = "true"
  cidr_block = "10.0.0.0/16"
  tags       = {
    Name = "example-VPC"
  }
}
# Subnet
resource "aws_subnet" "example-subnet" {
  vpc_id = aws_vpc.example.id
  cidr_block = "10.0.0.0/20"
  map_public_ip_on_launch = "true"
  tags       = {
    Name = "example-subnet"
  }  
}
# Internet Gateway
resource "aws_internet_gateway" "example-igw" {
  vpc_id = aws_vpc.example.id
}
# Routing
resource "aws_default_route_table" "example-rtb" {
  default_route_table_id = aws_vpc.example.default_route_table_id   
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id  = aws_internet_gateway.example-igw.id
  }
}

Security

參考以下code編寫成 security.tf ,替VPC規劃一個專用ACL、替EC2建立一個專用的SecurityGroup,ingress是傳入規則,egress是傳出規則,特別注意在ACL部分最後一條 All Deny 是系統預設值,而SecurityGroup暫時只開放22 port讓ssh可連入,我們會於下一步驟創建ec2時套用這個安全群組。

[test@host terraform]$ cat security.tf
# ACL
resource "aws_default_network_acl" "example-acl" {
  default_network_acl_id = aws_vpc.example.default_network_acl_id

  ingress {
    protocol   = -1
    rule_no    = 100
    action     = "allow"
    cidr_block = "0.0.0.0/0"
    from_port  = 0
    to_port    = 0
  }
  egress {
    protocol   = -1
    rule_no    = 100
    action     = "allow"
    cidr_block = "0.0.0.0/0"
    from_port  = 0
    to_port    = 0
  }
}
# Security Group
resource "aws_security_group" "example-sg" {
    name   = "example-sg"
    vpc_id = aws_vpc.example.id

    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"]
    }
}

Subscribe AMI

在建立EC2之前,要先確認AMI ID,也就是欲安裝的作業系統, ami 的id可以透過AWS管理主控台「創建執行個體」時查看,若ami位於 Marketplace 中,需要預先訂閱,這樣跑IaC自動化時才不會報錯。

EC2 Instance

參考以下code編寫成 ec2.tf ,像是機器規格、磁碟規格與大小等都可以指定,詳情可參閱 官方文件,最後配置 aws_eip 資源給EC2,讓虛擬機器擁有一個固定IP。

key_name 是登入ec2使用的金鑰對名稱,請改為你AWS上的金鑰對名稱。

[test@host terraform]$ cat ec2.tf
# Instances
resource "aws_instance" "example-ec2" {
  ami                    = "ami-037e95e96da4d9a4d"
    root_block_device {
      delete_on_termination = true
      volume_size = 15
      volume_type = "gp3"
    }
  instance_type          = "t3.small"
  subnet_id              = aws_subnet.example-subnet.id
  vpc_security_group_ids = [aws_security_group.example-sg.id]
  key_name               = "mytest"
  tags       = {
    Name = "example-ec2"
  }
}

resource "aws_eip" "example-ec2-eip" {
  instance = aws_instance.example-ec2.id
  tags       = {
    Name = "example-ec2-eip"
  }
}

運行Iac腳本,並至AWS管理主控台驗證資源部署的狀況。

[test@host terraform]$ terraform apply

回到頁首