iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 14
0
DevOps

今晚我想來點 Terraform 佐 AWS系列 第 14

今晚我想認識 Terraform 的好伙伴 Packer

  • 分享至 

  • xImage
  •  

Packer 是自動化的映像檔製作工具。透過 JSON 格式的組態檔,讓我們可以更容易的結合一些工具製作映像檔。

Packer 支援許多的雲端服務平台跟虛擬化引擎。

安裝 Packer

你可以到官方網站 https://www.packer.io/downloads.html 上直接下載,或是透過套件管理工具安裝。

Mac 上的 Homebrew 有官方提供的檔案,所以只需要一個簡單的指令就可以完成。指令如下:

$ brew install hashicorp/tap/packer

安裝完畢之後我們執行 packer 指令測試看看,看到完整的使用說明就代表安裝成功了。

$ packer
usage: packer [--version] [--help] <command> [<args>]

Available commands are:
    build       build image(s) from template
    fix         fixes templates from old versions of packer
    inspect     see components of a template
    validate    check that a template is valid
    version     Prints the Packer version

建立映像檔

安裝完 Packer 之後,我們馬上來建立一個映像檔試試看。

一樣選擇在 AWS 平台上,建立 AMI 映像檔。所以還需要準備 AWS 的 access keysecret key

建立檔案 hello.json,內容如下:

{
  "variables": {
    "aws_access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
    "aws_secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
    "region": "ap-northeast-1"
  },
  "builders": [
    {
      "type": "amazon-ebs",
      "access_key": "{{user `aws_access_key`}}",
      "secret_key": "{{user `aws_secret_key`}}",
      "region": "{{user `region`}}",
      "ami_name": "packer-ubuntu-aws-hello-demo-{{timestamp}}",
      "source_ami_filter": {
        "filters": {
          "virtualization-type": "hvm",
          "name": "ubuntu/images/*ubuntu-focal-20.04-amd64-server-*",
          "root-device-type": "ebs"
        },
        "owners": ["099720109477"],
        "most_recent": true
      },
      "instance_type": "t2.micro",
      "ssh_username": "ubuntu"
    }
  ],
  "provisioners": [
    {
      "type": "file",
      "source": "./hello.txt",
      "destination": "/home/ubuntu/"
    },
    {
      "type": "shell",
      "inline": [
        "ls -al /home/ubuntu",
        "cat /home/ubuntu/hello.txt"
      ]
    }
  ]
}

執行 build 指令

packer build hello.json
amazon-ebs: output will be in this color.

==> amazon-ebs: Prevalidating any provided VPC information
==> amazon-ebs: Prevalidating AMI Name: packer-ubuntu-aws-hello-demo-1600187357
    amazon-ebs: Found Image ID: ami-09b86f9709b3c33d4
==> amazon-ebs: Creating temporary keypair: packer_5f60ebdd-8a0d-c62f-ca3e-3e19608689f0
==> amazon-ebs: Creating temporary security group for this instance: packer_5f60ebdf-d17d-67d5-dc2f-27507d99b537
==> amazon-ebs: Authorizing access to port 22 from [0.0.0.0/0] in the temporary security groups...
==> amazon-ebs: Launching a source AWS instance...
==> amazon-ebs: Adding tags to source instance
    amazon-ebs: Adding tag: "Name": "Packer Builder"
    amazon-ebs: Instance ID: i-039020f658810c6e7
==> amazon-ebs: Waiting for instance (i-039020f658810c6e7) to become ready...
==> amazon-ebs: Using ssh communicator to connect: 52.196.198.21
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Uploading ./hello.txt => /home/ubuntu/
    amazon-ebs: hello.txt 14 B / 14 B [==============================================================================================] 100.00% 0s
==> amazon-ebs: Provisioning with shell script: /var/folders/qc/26b601512_lcvbbb8qg8vrfm0000gp/T/packer-shell981772827
    amazon-ebs: total 32
    amazon-ebs: drwxr-xr-x 4 ubuntu ubuntu 4096 Sep 15 16:29 .
    amazon-ebs: drwxr-xr-x 3 root   root   4096 Sep 15 16:29 ..
    amazon-ebs: -rw-r--r-- 1 ubuntu ubuntu  220 Feb 25  2020 .bash_logout
    amazon-ebs: -rw-r--r-- 1 ubuntu ubuntu 3771 Feb 25  2020 .bashrc
    amazon-ebs: drwx------ 2 ubuntu ubuntu 4096 Sep 15 16:29 .cache
    amazon-ebs: -rw-r--r-- 1 ubuntu ubuntu  807 Feb 25  2020 .profile
    amazon-ebs: drwx------ 2 ubuntu ubuntu 4096 Sep 15 16:29 .ssh
    amazon-ebs: -rw-r--r-- 1 ubuntu ubuntu   14 Sep 15 16:29 hello.txt
    amazon-ebs: Hello Packer!
==> amazon-ebs: Stopping the source instance...
    amazon-ebs: Stopping instance
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Creating AMI packer-ubuntu-aws-hello-demo-1600187357 from instance i-039020f658810c6e7
    amazon-ebs: AMI: ami-0c91dff33f9c5d66e
==> amazon-ebs: Waiting for AMI to become ready...
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Cleaning up any extra volumes...
==> amazon-ebs: No volumes to clean up, skipping
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' finished after 2 minutes 46 seconds.

==> Wait completed after 2 minutes 46 seconds

==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:
ap-northeast-1: ami-0c91dff33f9c5d66e

我們成功建立映像檔了!

現在到 AWS 的 EC2 裡面檢查,你可以在 AMIs 管理頁面跟 Snapshots 管理頁面看到 Packer 建立的檔案。

刪除映像檔

Packer 只能夠建立映像檔。

想要刪除映像檔,請先到 AMIs 管理頁面執行 deregister,再到 Snapshots 管理頁面執行 Delete


上一篇
今晚我想透過使用者資料建立一台網頁伺服器
下一篇
今晚我想多認識一點 Packer 的模版
系列文
今晚我想來點 Terraform 佐 AWS30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言