본문 바로가기

* 퍼블릭 클라우드

[packer] aws ami 이미지 빌드

반응형

aws ami 이미지 빌드

packer 파일 작성

vim pkr-shell-script-aws-linux.pkr.hcl
packer {
  required_plugins {
    amazon = {
      version = ">= 1.1.1"
      source  = "github.com/hashicorp/amazon"
    }
  }
}

################################################################################
################################################################################
################################################################################
variables {
  aws_profile      = "terraform"
  aws_region       = "ap-northeast-2" #{ 서울:ap-northeast-2, 도쿄:ap-northeast-1, 싱가포르:ap-southeast-1, 버지니아:us-east-1, 오리건:us-west-2 }
  ami_name         = "ubuntu18" #amazon2 | ubuntu22 | ubuntu18
  source_name      = "ubuntu18" #amazon2 | ubuntu22 | ubuntu18
  ami_version      = "0.1"
  instance_type    = "t3.small"
  project_name     = "web project"
  owner_name       = "pkradmin"
  environment_name = "development" #development | testing | staging | production 
  description      = "Managed by Packer"

}

################################################################################
################################################################################
################################################################################
locals {
  #   timestamp = regex_replace(timestamp(), "[- TZ:]", "")
  timestamp   = formatdate("YYMMDDhhmm", timestamp())
  ami_name    = var.ami_name
  ami_version = var.ami_version
  common_tags = {
    Name        = "${local.ami_name}-${local.ami_version}_${local.timestamp}"
    Project     = var.project_name
    Owner       = var.owner_name
    Environment = var.environment_name
    Packer      = true
    Description = var.description
  }
}

################################################################################
################################################################################
################################################################################
##### amazon(official accounts : 137112412989)
source "amazon-ebs" "amazon2" {
  profile                     = var.aws_profile
  region                      = var.aws_region
  ami_name                    = "${local.ami_name}-${local.ami_version}_${local.timestamp}"
  instance_type               = var.instance_type
  ssh_username                = "ec2-user"
  associate_public_ip_address = true

  source_ami_filter {
    filters = {
      name                = "amzn2-ami-hvm-2.0.*-x86_64-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["137112412989"]
  }

  tags = merge(
    local.common_tags,
    {
      Description = var.description
    }
  )

  run_tags = merge(
    local.common_tags,
    {
      Description = var.description
    }
  )

  snapshot_tags = merge(
    local.common_tags,
    {
      Description = var.description
    }
  )
}

#### ubuntu(official accounts : 099720109477)
source "amazon-ebs" "ubuntu22" {
  profile                     = var.aws_profile
  region                      = var.aws_region
  ami_name                    = "${local.ami_name}-${local.ami_version}_${local.timestamp}"
  instance_type               = var.instance_type
  ssh_username                = "ubuntu"
  associate_public_ip_address = true

  source_ami_filter {
    filters = {
      name                = "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["099720109477"]
  }

  tags = merge(
    local.common_tags,
    {
      Description = var.description
    }
  )

  run_tags = merge(
    local.common_tags,
    {
      Description = var.description
    }
  )

  snapshot_tags = merge(
    local.common_tags,
    {
      Description = var.description
    }
  )
}

#### ubuntu(official accounts : 099720109477)
source "amazon-ebs" "ubuntu18" {
  profile                     = var.aws_profile
  region                      = var.aws_region
  ami_name                    = "${local.ami_name}-${local.ami_version}_${local.timestamp}"
  instance_type               = var.instance_type
  ssh_username                = "ubuntu"
  associate_public_ip_address = true

  source_ami_filter {
    filters = {
      name                = "ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["099720109477"]
  }

  tags = merge(
    local.common_tags,
    {
      Description = var.description
    }
  )

  run_tags = merge(
    local.common_tags,
    {
      Description = var.description
    }
  )

  snapshot_tags = merge(
    local.common_tags,
    {
      Description = var.description
    }
  )
}

##### centos(official accounts : 125523088429)

################################################################################
################################################################################
################################################################################
build {
  name = "${local.ami_name}-${local.ami_version}_${local.timestamp}"
  sources = [
    "source.amazon-ebs.${var.source_name}"
  ]

  provisioner "shell" {
    environment_vars = [
      "FOO=hello world",
    ]
    script       = "provisioning-scripts/z.sh"
    pause_before = "10s"
    timeout      = "10s"
  }

  provisioner "shell" {
    inline = [
      "sleep 30",
      "echo \"FOO is $FOO\" > example.txt",
    ]
  }
}

z.sh 스크립트 파일 작성

mkdir provisioning-scripts
vim provisioning-scripts/z.sh
#!/bin/bash

# Wait for cloud-init to finish
while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done

sleep 30

echo "script execute"

echo \"FOO is $FOO\" > example.txt
packer init .
packer fmt .
packer validate .
$ packer validate .
The configuration is valid.
packer build .
$ packer build .
ubuntu-0.1_2208301441.amazon-ebs.ubuntu: output will be in this color.

==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Prevalidating any provided VPC information
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Prevalidating AMI Name: ubuntu-0.1_2208301441
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Found Image ID: ami-08f869ae259b6bc98
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Creating temporary keypair: packer_630e2186-36cd-9da6-6e53-61836da7eb7e
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Creating temporary security group for this instance: packer_630e2188-d048-4500-66f7-14ba550c80ab
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Authorizing access to port 22 from [0.0.0.0/0] in the temporary security groups...
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Launching a source AWS instance...
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Packer": "true"
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Project": "project_name"
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Description": "Managed by Packer"
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Environment": "environment_name"
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Name": "ubuntu-0.1_2208301441"
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Owner": "owner_name"
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Instance ID: i-0a70b7e2d59d93116
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Waiting for instance (i-0a70b7e2d59d93116) to become ready...
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Using SSH communicator to connect: 3.35.71.219
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Waiting for SSH to become available...
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Connected to SSH!
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Pausing 10s before the next provisioner...
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Provisioning with shell script: provisioning-scripts/z.sh
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: script execute
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Provisioning with shell script: /var/folders/56/1ltpc_xj6wb35k7pfvv9h0340000gn/T/packer-shell257669287
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: This provisioner runs last
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Stopping the source instance...
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Stopping instance
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Waiting for the instance to stop...
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Creating AMI ubuntu-0.1_2208301441 from instance i-0a70b7e2d59d93116
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: AMI: ami-0496287d45333c2dd
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Waiting for AMI to become ready...
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Skipping Enable AMI deprecation...
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tags to AMI (ami-0496287d45333c2dd)...
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Tagging snapshot: snap-0f83f54246394e406
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Creating AMI tags
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Description": "Managed by Packer"
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Environment": "environment_name"
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Name": "ubuntu-0.1_2208301441"
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Owner": "owner_name"
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Packer": "true"
    ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Adding tag: "Project": "project_name"
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Creating snapshot tags
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Terminating the source AWS instance...
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Cleaning up any extra volumes...
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: No volumes to clean up, skipping
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Deleting temporary security group...
==> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: Deleting temporary keypair...
Build 'ubuntu-0.1_2208301441.amazon-ebs.ubuntu' finished after 3 minutes 55 seconds.

==> Wait completed after 3 minutes 55 seconds

==> Builds finished. The artifacts of successful builds are:
--> ubuntu-0.1_2208301441.amazon-ebs.ubuntu: AMIs were created:
ap-northeast-2: ami-0496287d45333c2dd

AWS Management Console > EC2 > 이미지 > AMI

 

참고URL

- packer 설치 : https://scbyun.com/1313

https://learn.hashicorp.com/tutorials/packer/get-started-install-cli

 

728x90
반응형