aws-ami-builder

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

AWS AMI Builder

AWS AMI 构建指南

Build Amazon Machine Images (AMIs) using Packer's
amazon-ebs
builder.
Note: Building AMIs incurs AWS costs (EC2 instances, EBS storage, data transfer). Builds typically take 10-30 minutes depending on provisioning complexity.
使用Packer的
amazon-ebs
构建器创建Amazon Machine Images(AMIs)。
参考文档: Amazon EBS 构建器
注意: 构建AMI会产生AWS费用(包括EC2实例、EBS存储、数据传输)。根据配置复杂度,构建通常需要10-30分钟。

Basic AMI Template

基础AMI模板

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

Common Source AMI Filters

常用源AMI过滤器

Ubuntu 22.04 LTS

Ubuntu 22.04 LTS

hcl
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
}
hcl
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

Amazon Linux 2023

hcl
source_ami_filter {
  filters = {
    name                = "al2023-ami-*-x86_64"
    root-device-type    = "ebs"
    virtualization-type = "hvm"
  }
  most_recent = true
  owners      = ["amazon"]
}
hcl
source_ami_filter {
  filters = {
    name                = "al2023-ami-*-x86_64"
    root-device-type    = "ebs"
    virtualization-type = "hvm"
  }
  most_recent = true
  owners      = ["amazon"]
}

Multi-Region AMI

多区域AMI

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

  # 复制到其他区域
  ami_regions = ["us-east-1", "us-east-2", "eu-west-1"]
}

Authentication

身份验证

Packer uses AWS credential resolution:
  1. Environment variables:
    AWS_ACCESS_KEY_ID
    ,
    AWS_SECRET_ACCESS_KEY
  2. AWS credentials file:
    ~/.aws/credentials
  3. IAM instance profile (when running on EC2)
bash
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_REGION="us-west-2"

packer build .
Packer会按以下顺序解析AWS凭证:
  1. 环境变量:
    AWS_ACCESS_KEY_ID
    AWS_SECRET_ACCESS_KEY
  2. AWS凭证文件:
    ~/.aws/credentials
  3. IAM实例配置文件(在EC2上运行时)
bash
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_REGION="us-west-2"

packer build .

Build Commands

构建命令

bash
undefined
bash
undefined

Initialize plugins

初始化插件

packer init .
packer init .

Validate template

验证模板

packer validate .
packer validate .

Build AMI

构建AMI

packer build .
packer build .

Build with variables

使用变量构建

packer build -var "region=us-east-1" .
undefined
packer build -var "region=us-east-1" .
undefined

Common Issues

常见问题

SSH Timeout
  • Ensure security group allows SSH (port 22)
  • Verify subnet has internet access
AMI Already Exists
  • AMI names must be unique
  • Use timestamp in name:
    my-app-${local.timestamp}
Volume Size Too Small
  • Check source AMI's volume size
  • Set
    launch_block_device_mappings.volume_size
    accordingly
SSH 超时
  • 确保安全组允许SSH(22端口)
  • 验证子网具有互联网访问权限
AMI 已存在
  • AMI名称必须唯一
  • 在名称中使用时间戳:
    my-app-${local.timestamp}
卷大小过小
  • 检查源AMI的卷大小
  • 相应设置
    launch_block_device_mappings.volume_size

References

参考资料