2017-04-06 49 views
0
云初始化实例

我尝试使用云init进程initilice AWS实例,我用的是未来terraform代码进行测试:我如何initialice与Terraform

variable "hostname" {} 
variable "domain_name" {} 


variable "filename" { 
    default = "cloud-config.cfg" 
} 

data "template_file" "test" { 
    template = <<EOF 
#cloud-config 
hostname: $${hostname} 
fqdn: $${fqdn} 
mounts: 
    - [ ephemeral, null ] 
output: 
    all: '| tee -a /var/log/cloud-init-output.log' 
EOF 

    vars { 
    hostname = "${var.hostname}" 
    fqdn  = "${format("%s.%s", var.hostname, var.domain_name)}" 
    } 
} 

data "template_cloudinit_config" "test" { 
    gzip   = false 
    base64_encode = false 

    part { 
    filename  = "${var.filename}" 
    content_type = "text/cloud-config" 
    content  = "${data.template_file.test.rendered}" 
    } 
} 


resource "aws_instance" "bootstrap2" { 
    ami = "${var.aws_centos_ami}" 
    availability_zone = "eu-west-1b" 
    instance_type = "t2.micro" 
    key_name = "${var.aws_key_name}" 
    security_groups = ["${aws_security_group.bastion.id}"] 
    associate_public_ip_address = true 
    private_ip = "10.0.0.12" 
    source_dest_check = false 
    subnet_id = "${aws_subnet.eu-west-1b-public.id}" 
    triggers { 
     template = "${data.template_file.test.rendered}" 
    } 

    tags { 
      Name = "bootstrap2" 
     } 
} 

但它没有内部的触发器“引导”资源。那么我怎么能用我定义的cloud-config来配置这个实例呢?

回答

2

triggers不是aws_instance资源的有效参数。通过配置cloud-init通常的方式是通过user_data参数,如下所示:

resource "aws_instance" "bootstrap2" { 
    ami = "${var.aws_centos_ami}" 
    availability_zone = "eu-west-1b" 
    instance_type = "t2.micro" 
    key_name = "${var.aws_key_name}" 
    security_groups = ["${aws_security_group.bastion.id}"] 
    associate_public_ip_address = true 
    private_ip = "10.0.0.12" 
    source_dest_check = false 
    subnet_id = "${aws_subnet.eu-west-1b-public.id}" 

    # Pass templated configuration to cloud-init 
    user_data = "${data.template_file.test.rendered}" 

    tags { 
    Name = "bootstrap2" 
    } 
} 
+0

真的thankss !!它完美的作品 –