{
"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`}}",
}],
}
這個設定選項,會從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
},
在執行的過程中可以去AWS EC2 Console觀察變化
packer output in linux
instance change in AWS EC2 Console
Security Groups change in AWS EC2 Console
Your AMI created in AWS EC2 Console
待續...