mitchellh/vagrant-aws

Wrapping AWS Provider in Function Doesn't work

soudaburger opened this issue · 0 comments

settings = YAML.load_file('settings.yaml')

def launch_on_aws(config, settings)
  config.vm.define :awsbox, autostart: false do |awsbox|
    awsbox.vm.box      = "dummy"
    awsbox.vm.provider :aws do |aws, override|
      aws.access_key_id         = settings['aws']['access_key_id']
      aws.secret_access_key     = settings['aws']['secret_access_key']
      aws.keypair_name          = settings['aws']['keypair_name']
      aws.ami                   = settings['aws']['ami']
      aws.block_device_mapping  = settings['aws']['block_device_mapping']
      aws.instance_type         = settings['aws']['instance_type']
      aws.monitoring            = settings['aws']['monitoring']
      aws.region                = settings['aws']['region']
      aws.elastic_ip            = settings['aws']['elastic_ip']
      aws.security_groups       = settings['aws']['security_groups']
      aws.associate_public_ip   = settings['aws']['associate_public_ip']
      aws.ssh_host_attribute    = settings['aws']['ssh_host_attribute']
      aws.subnet_id             = settings['aws']['subnet_id']
      aws.tenancy               = settings['aws']['tenancy']
      aws.terminate_on_shutdown = settings['aws']['terminate_on_shutdown']
      aws.block_device_mapping  = settings['aws']['primary_disk']
      override.ssh.username         = "ubuntu"
      override.ssh.private_key_path = settings['aws']['ssh']['private_key_path']
    end
  end
end

config.vm.define :myawstestbox, autostart: false do | nginxaws1 |
    nginxaws1.vm.box      = "dummy"
    nginxaws1.vm.hostname = "myawstestbox1"
    launch_on_aws(config, settings)
  end

I currently have multiple instances defined within my Vagrantfile so that I can configure/launch whichever instance I am testing at the time. All other node configuration is done through puppet, so really I just need custom provider settings for each instance. I have a wrapper for puppet nodes I want to launch that specify, for instance, which box to launch with (one with puppet) instead of the default ubuntu one.

Relocating the config.vm.define section out of the function entirely works without issue. I would like to be able to specify specific boxes to be launched in AWS when I want without having to copy past those 20+ lines of code into every instance I want to launch.

Is this even possible? Or am I stuck repeating that code block for every AWS instance I want to launch ?