关于 aws-ami-builder
这项技能通过 Packer 的 `amazon-ebs` 构建器创建 Amazon Machine Image,面向云工程师,涵盖模板、源 AMI 筛选、AWS 身份验证和构建验证。
主要功能
- 01Packer `amazon-ebs` 模板
- 02Ubuntu 和 Amazon Linux 筛选器
- 03AWS 凭证配置
- 04模板验证与构建命令
适用场景
- 01从 Ubuntu 或 Amazon Linux 创建 AMI
- 02在多个 AWS 区域构建 AMI
- 03排查 SSH、命名和卷大小问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ hashicorp/ agent-skills --skill aws-ami-builder安装后在 Claude Code 中输入/aws-ami-builder确认已加载。
详细介绍
AWS AMI 构建器
使用 Packer 的 amazon-ebs 构建器构建 Amazon Machine Image(AMI)。
参考: Amazon EBS 构建器
注意: 构建 AMI 会产生 AWS 费用(EC2 实例、EBS 存储和数据传输)。根据预配的复杂程度,构建通常需要 10-30 分钟。
基本 AMI 模板
packer {
required_plugins {
amazon = {
source = "github.com/hashicorp/amazon"
version = "~> 1.3"
}
}
}
variable "region" {
type = string
default = "us-west-2"
}
locals {
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}
source "amazon-ebs" "ubuntu" {
region = var.region
instance_type = "t3.micro"
source_ami_filter {
filters = {
name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"] # Canonical
}
ssh_username = "ubuntu"
ami_name = "my-app-${local.timestamp}"
tags = {
Name = "my-app"
BuildDate = local.timestamp
}
}
build {
sources = ["source.amazon-ebs.ubuntu"]
provisioner "shell" {
inline = [
"sudo apt-get update",
"sudo apt-get upgrade -y",
]
}
}
常见源 AMI 筛选器
Ubuntu 22.04 LTS
source_ami_filter {
filters = {
name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"] # Canonical
}
Amazon Linux 2023
source_ami_filter {
filters = {
name = "al2023-ami-*-x86_64"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["amazon"]
}
多区域 AMI
source "amazon-ebs" "ubuntu" {
region = "us-west-2"
instance_type = "t3.micro"
source_ami_filter {
filters = {
name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"
}
most_recent = true
owners = ["099720109477"]
}
ssh_username = "ubuntu"
ami_name = "my-app-${local.timestamp}"
# Copy to additional regions
ami_regions = ["us-east-1", "us-east-2", "eu-west-1"]
}
身份验证
Packer 使用 AWS 凭证解析方式:
- 环境变量:
AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY - AWS 凭证文件:
~/.aws/credentials - IAM 实例配置文件(在 EC2 上运行时)
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_REGION="us-west-2"
packer build .
构建命令
# Initialize plugins
packer init .
# Validate template
packer validate .
# Build AMI
packer build .
# Build with variables
packer build -var "region=us-east-1" .
常见问题
SSH 超时
- 确保安全组允许 SSH(端口 22)
- 确认子网可以访问互联网
AMI 已存在
- AMI 名称必须唯一
- 在名称中使用时间戳:
my-app-${local.timestamp}
卷大小过小
- 检查源 AMI 的卷大小
- 相应设置
launch_block_device_mappings.volume_size
参考资料
评论
支持 Markdown