iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 5
0
Cloud Native

AWS高手同事離職後不止30天系列 第 5

建立各種平台上的「機器」並產生「映像檔」 > Packer III

  • 分享至 

  • xImage
  •  

四、範例

Packer Template(sample.json)

{
  "variables": {
    "aws_access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
    "aws_secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
    "region": "{{env `AWS_DEFAULT_REGION`}}"
  },
  "builders": [{
    "type": "amazon-ebs",
    "access_key": "{{user `aws_access_key`}}",
    "secret_key": "{{user `aws_secret_key`}}",
    "region": "{{user `region`}}",
    "source_ami_filter": {
      "filters": {
      "virtualization-type": "hvm",
      "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
      "root-device-type": "ebs"
      },
      "owners": ["099720109477"],
      "most_recent": true
    },
    "instance_type": "t2.micro",
    "ssh_username": "ubuntu",
    "ami_name": "packer-example {{timestamp}}"
  }],
  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "sleep 30",
        "sudo apt-get update",
        "sudo apt-get install -y redis-server"
      ]
    }
  ]
}

省略設定

前一天跟大家提到AWS credentials 授權(packer設定方式),小編是使用「AWS credentials 檔案」(linux > $HOME/.aws/credentials),所以在Template中以下區塊可以省略,留著也會套用$HOME/.aws/credentials,但如果你有設定環境變數的話(AWS_ACCESS_KEY_ID & AWS_SECRET_ACCESS_KEY & AWS_DEFAULT_REGION), 則會以「環境變數」為主!!!。

{
  "variables": {
    "aws_access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
    "aws_secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
    "region": "{{env `AWS_DEFAULT_REGION`}}"
  },
  "builders": [{
    "access_key": "{{user `aws_access_key`}}",
    "secret_key": "{{user `aws_secret_key`}}",
    "region": "{{user `region`}}",
  }],
}

source_ami_filter

這個設定選項,會從aws public images當中根據你的條件產生「source_ami」(也可以直接設定source_ami)
至於filters當中可以使用的條件可以參考(AWS)DescribeImages

"source_ami_filter": {
  "filters": {
  "virtualization-type": "hvm",
  "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
  "root-device-type": "ebs"
  },
  "owners": ["099720109477"],
  "most_recent": true
},

執行 packer build sample.json

在執行的過程中可以去AWS EC2 Console觀察變化

  • packer output in linux
    01_packer build_linux_change.png

  • instance change in AWS EC2 Console
    02_packer build_instance_change.png

  • Security Groups change in AWS EC2 Console
    03_packer build_security_group_change.png

  • Your AMI created in AWS EC2 Console
    04_packer build_ami_change.png


待續...


上一篇
建立各種平台上的「機器」並產生「映像檔」 > Packer II
下一篇
IT自動化工具。設定系統,佈署安裝軟體 > Ansible I
系列文
AWS高手同事離職後不止30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言