如何使用Terraform从创建的实例创建AWS AMI?

时间:2019-08-05 07:12:51

标签: amazon-web-services terraform

我正在使用wordpress安装设置aws实例,并希望使用创建的实例创建AMI。下面我附上我的代码。

provider "aws" {

    region = "${var.region}"
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
}

resource  "aws_instance" "test-wordpress" {

    ami = "${var.image_id}"
    instance_type = "${var.instance_type}"
    key_name = "test-web"
    #associate_public_ip_address = yes

    user_data = <<-EOF

      #!/bin/bash

      sudo yum update -y
      sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
      sudo yum install -y httpd mariadb-server
      cd /var/www/html
      sudo echo "healthy" > healthy.html
      sudo wget https://wordpress.org/latest.tar.gz
      sudo tar -xzf latest.tar.gz
      sudo cp -r wordpress/* /var/www/html/
      sudo rm -rf wordpress
      sudo rm -rf latest.tar.gz
      sudo chmod -R 755 wp-content
      sudo chown -R apache:apache wp-content
      sudo service httpd start
      sudo chkconfig httpd on

      EOF


    tags = {
      Name = "test-Wordpress-Server"
    }
} 


resource  "aws_ami_from_instance" "test-wordpress-ami" {
    name               = "test-wordpress-ami"
    source_instance_id = "${aws_instance.test-wordpress.id}"

  depends_on = [
      aws_instance.test-wordpress,
      ]

  tags = {
      Name = "test-wordpress-ami"
  }

}

将创建AMI,但是当我使用该AMI创建另一个实例时,wordpress安装不在其中。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

因此,您可以按照documentation

中所述的标签搜索AMI。

在您的情况下:

data "aws_ami" "example" {
  executable_users = ["self"]
  most_recent      = true
  owners           = ["self"]

  filter {
    name   = "tag:Name"
    values = ["test-wordpress-ami"]
  }
}

,然后将ID称为${data.aws_ami.example.image_id}

答案 1 :(得分:0)

我认为创建 AMI 图像的最佳方法是使用 Packer,也来自 Hashicorp,如 terraform。

什么是打包机?

<块引用>

带 Packer 的 Provision Infrastructure Packer 是 HashiCorp 的开源工具,用于从源创建机器映像 配置。您可以使用操作来配置 Packer 映像 适合您特定用例的系统和软件。

Packer 创建一个具有临时密钥对、security_group 和 IAM 角色的实例。在供应商“shell”中,可以自定义内联命令。之后,您可以将此 ami 与您的 terraform 代码一起使用。

示例脚本可能如下所示:

packer {
  required_plugins {
    amazon = {
      version = ">= 0.0.2"
      source  = "github.com/hashicorp/amazon"
    }
  }
}

source "amazon-ebs" "linux" {
  # AMI Settings
  ami_name                      = "ami-oracle-python3"
  instance_type                 = "t2.micro"
  source_ami                    = "ami-xxxxxxxx"
  ssh_username                  = "ec2-user"
  associate_public_ip_address   = false
  ami_virtualization_type       = "hvm"
  subnet_id                     = "subnet-xxxxxx" 
  
  launch_block_device_mappings {
    device_name = "/dev/xvda"
    volume_size = 8
    volume_type = "gp2"
    delete_on_termination = true
    encrypted = false
  }

  # Profile Settings
  profile                       = "xxxxxx"
  region                        = "eu-central-1"
}

build {
  sources = [
    "source.amazon-ebs.linux"
  ]
  provisioner "shell" {
    inline = [
        "export no_proxy=localhost"
    ]
  }
}

您可以找到文档 here