Skip to main content

Integrating with Cloud Providers

1. Using vagrant-aws Plugin

The vagrant-aws plugin allows you to use Vagrant with Amazon Web Services (AWS) to provision and manage EC2 instances.

Property Description Example Value
config.vm.box Must be set to dummy when using AWS. "dummy"
config.vm.provider Specifies the cloud provider to use. For AWS, this is "aws". "aws"
config.aws.access_key_id Your AWS access key ID. "AKIAIOSFODNN7EXAMPLE"
config.aws.secret_access_key Your AWS secret access key. "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
config.aws.region The AWS region to launch the instance in. "us-west-2"
config.aws.instance_type The EC2 instance type to use. "t2.micro"
config.aws.ami The AMI ID to use for the instance. "ami-0c55b4048a7d9236f"
config.aws.keypair_name Name of the key pair to use for SSH access. "my-key-pair"
config.aws.security_groups List of security groups to assign to the instance. ["default"]

Example: Basic Vagrantfile for AWS

 Vagrant.configure("2") do |config|
  config.vm.box = "dummy"

  config.vm.provider "aws" do |aws|
    aws.access_key_id = "AKIAIOSFODNN7EXAMPLE"
    aws.secret_access_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
    aws.region = "us-west-2"
    aws.instance_type = "t2.micro"
    aws.ami = "ami-0c55b4048a7d9236f"
    aws.keypair_name = "my-key-pair"
    aws.security_groups = ["default"]
  end
end
Note: Ensure that your AWS credentials are securely managed, preferably using environment variables or IAM roles rather than hardcoding them in the Vagrantfile.
Warning: Be cautious with costs when using AWS. EC2 instances can incur charges based on usage.

2. Configuring AWS Provider

Option Values Description
aws.access_key_id string AWS access key ID for authentication
aws.secret_access_key string AWS secret access key for authentication
aws.region string Region to launch the instance in (e.g., us-east-1)
aws.instance_type string Instance type to launch (e.g., t2.micro)
aws.ami string Amazon Machine Image ID to use for the instance
aws.keypair_name string Name of the key pair to use for SSH access
aws.security_groups array List of security groups to assign to the instance
aws.vpc_subnet_id string ID of the VPC subnet to launch the instance in

Example: Basic AWS Provider Configuration

config.vm.provider :aws do |aws|
  aws.access_key_id = "AKIAIOSFODNN7EXAMPLE"
  aws.secret_access_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
  aws.region = "us-west-2"
  aws.instance_type = "t2.micro"
  aws.ami = "ami-0c55b159cbfafe1d0"
  aws.keypair_name = "my-keypair"
  aws.security_groups = ["default"]
end
Note: Ensure that the AWS credentials have appropriate permissions to launch EC2 instances and manage VPC resources.

3. Using vagrant-azure Plugin

Property Description Example Value
config.vm.box Base box to use for the VM "azure/vagrant-azure"
config.vm.provision Provisioning script or configuration "shell", path: "bootstrap.sh"
config.vm.network Network configuration for the VM "private_network", ip: "192.168.10.100"

Example: Basic Vagrantfile for Azure

# Vagrantfile for Azure
Vagrant.configure("2") do |config|
  config.vm.box = "azure/vagrant-azure"

  # Azure-specific configuration
  config.vm.provider :azure do |azure|
    azure.vm_name = "my-vm"
    azure.vm_size = "Standard_B1s"
    azure.location = "East US"
    azure.resource_group = "my-resource-group"
    azure.subscription_id = "<your-subscription-id>"
  end

  # Networking
  config.vm.network "private_network", ip: "192.168.10.100"

  # Provisioning
  config.vm.provision "shell", path: "bootstrap.sh"
end
Note: Ensure that the vagrant-azure plugin is installed using vagrant plugin install vagrant-azure.
Warning: The Azure provider requires valid credentials and subscription details to be configured in the Vagrantfile or environment variables.

4. Configuring Azure Provider

Option Values Description
config.vm.provider "azure" Specifies the cloud provider to use for Vagrant.
azure.tenant_id string The Azure Active Directory tenant ID.
azure.client_id string The client ID of the service principal used for authentication.
azure.client_secret string The client secret of the service principal used for authentication.
azure.subscription_id string The Azure subscription ID to use for resource creation.
azure.resource_group_name string The name of the resource group to create or use.
azure.vm_size string The size of the virtual machine to create (e.g., Standard_B1s).
azure.vm_name string The name of the virtual machine to create.
azure.location string The Azure region where the VM will be created (e.g., eastus).
azure.image_reference object Configuration for the OS image to use (e.g., { "publisher": "Canonical", "offer": "UbuntuServer", "sku": "18.04-LTS", "version": "latest" }).

Example: Basic Azure Provider Configuration

Vagrant.configure("2") do |config|
  config.vm.provider "azure" do |azure|
    azure.tenant_id = "your-tenant-id"
    azure.client_id = "your-client-id"
    azure.client_secret = "your-client-secret"
    azure.subscription_id = "your-subscription-id"
    azure.resource_group_name = "vagrant-rg"
    azure.vm_size = "Standard_B1s"
    azure.vm_name = "vagrant-vm"
    azure.location = "eastus"
    azure.image_reference = {
      "publisher" => "Canonical",
      "offer" => "UbuntuServer",
      "sku" => "18.04-LTS",
      "version" => "latest"
    }
  end
end
Note: Ensure that the service principal used has the necessary permissions to create and manage resources in the specified subscription and resource group.

5. Using vagrant-google Plugin

Property Description Example
config.google.project Google Cloud project ID "my-gcp-project"
config.google.zone Zone to create instance in "us-central1-a"
config.google.machine_type Instance machine type "e2-medium"
config.google.image Base image for instance "ubuntu-os-cloud/ubuntu-20.04"
config.google.network Network name to use "default"
config.google.subnetwork Subnetwork name to use "default"
config.google.tags Network tags to apply ["web-server"]

Example: Basic Vagrantfile for Google Compute Engine

Vagrant.configure("2") do |config|
  config.vm.provider :google do |google|
    google.project = "my-gcp-project"
    google.zone = "us-central1-a"
    google.machine_type = "e2-medium"
    google.image = "ubuntu-os-cloud/ubuntu-20.04"
    google.network = "default"
    google.subnetwork = "default"
    google.tags = ["web-server"]
  end
end
Note: Requires vagrant-google plugin installed via vagrant plugin install vagrant-google.
Warning: Ensure GCP credentials are configured via gcloud auth application-default login or service account key file.
Option Values Description
config.google.service_account_email string Email of service account to use
config.google.service_account_file string Path to service account key file
config.google.disk_size_gb integer Disk size in GB
config.google.disk_type string Disk type (pd-standard, pd-ssd)
config.google.use_internal_ip boolean Use internal IP instead of external

Example: Advanced Configuration with Service Account

Vagrant.configure("2") do |config|
  config.vm.provider :google do |google|
    google.project = "my-gcp-project"
    google.zone = "us-central1-a"
    google.machine_type = "n1-standard-2"
    google.image = "ubuntu-os-cloud/ubuntu-20.04"
    google.network = "default"
    google.subnetwork = "default"
    google.tags = ["web-server", "http"]
    google.service_account_email = "my-service-account@my-gcp-project.iam.gserviceaccount.com"
    google.service_account_file = "/path/to/service-account-key.json"
    google.disk_size_gb = 20
    google.disk_type = "pd-ssd"
    google.use_internal_ip = false
  end
end

Key Features

  • Supports GCP instances with custom configurations
  • Allows specifying machine type, image, network, and zone
  • Supports service account authentication
  • Enables disk size and type customization
  • Supports internal IP configuration

6. Configuring GCP Provider

Option Values Description
config.vm.provider "google" Specifies the Google Cloud provider for Vagrant
google.project "my-gcp-project" Google Cloud Project ID
google.zone "us-central1-a" Zone where the instance will be created
google.machine_type "e2-medium" Instance machine type
google.image "ubuntu-os-cloud/ubuntu-2004" Base image for the instance
google.network "default" Network to use for the instance
google.subnetwork "default" Subnetwork to use for the instance
google.service_account "my-service-account@my-gcp-project.iam.gserviceaccount.com" Service account to use for instance permissions
google.tags ["web-server"] Network tags to apply to the instance
google.scopes ["https://www.googleapis.com/auth/cloud-platform"] OAuth scopes for the instance

Example: Basic GCP Provider Configuration

Vagrant.configure("2") do |config|
  config.vm.provider "google" do |google|
    google.project = "my-gcp-project"
    google.zone = "us-central1-a"
    google.machine_type = "e2-medium"
    google.image = "ubuntu-os-cloud/ubuntu-2004"
    google.network = "default"
    google.subnetwork = "default"
    google.service_account = "my-service-account@my-gcp-project.iam.gserviceaccount.com"
    google.tags = ["web-server"]
    google.scopes = ["https://www.googleapis.com/auth/cloud-platform"]
  end
end
Note: Ensure that the GCP service account has the necessary permissions to create and manage Compute Engine instances.

7. Using vagrant-digitalocean Plugin

Property Description Example
config.digital_ocean.token DigitalOcean API token for authentication config.digital_ocean.token = "your-api-token"
config.digital_ocean.region Region to launch the droplet in config.digital_ocean.region = "nyc1"
config.digital_ocean.size Size of the droplet (e.g., s-1vcpu-1gb) config.digital_ocean.size = "s-1vcpu-1gb"
config.digital_ocean.image Base image for the droplet (e.g., ubuntu-20-04-x64) config.digital_ocean.image = "ubuntu-20-04-x64"
config.digital_ocean.ssh_key SSH key fingerprint or ID to use for droplet access config.digital_ocean.ssh_key = "ssh-rsa AAAAB3NzaC1yc2E..."

Example: Basic Vagrantfile with DigitalOcean

Vagrant.configure("2") do |config|
  config.vm.box = "digital_ocean"
  config.digital_ocean.token = "your-api-token"
  config.digital_ocean.region = "nyc1"
  config.digital_ocean.size = "s-1vcpu-1gb"
  config.digital_ocean.image = "ubuntu-20-04-x64"
  config.digital_ocean.ssh_key = "ssh-rsa AAAAB3NzaC1yc2E..."
end
Note: Ensure your DigitalOcean API token has the necessary permissions to create and manage droplets.
Warning: Droplets created with Vagrant will be billed to your DigitalOcean account. Make sure to destroy them when no longer needed.

8. Setting Cloud Provider Region

Provider Region Setting Notes
AWS config.vm.provider "aws" do |aws|
  aws.region = "us-west-2"
Specify the AWS region for instance creation
Azure config.vm.provider "azure" do |azure|
  azure.location = "East US"
Set the Azure region for resource deployment
Google Compute Engine config.vm.provider "google" do |google|
  google.zone = "us-central1-a"
Define the GCP zone for instance placement
DigitalOcean config.vm.provider "digital_ocean" do |do|
  do.region = "nyc1"
Set the DigitalOcean region for droplet creation

Example: AWS Region Configuration

config.vm.provider "aws" do |aws|
  aws.region = "us-west-2"
  aws.instance_type = "t3.micro"
  aws.keypair_name = "my-keypair"
end

Example: GCP Zone Configuration

config.vm.provider "google" do |google|
  google.zone = "us-central1-a"
  google.machine_type = "n1-standard-1"
  google.disk_size = 20
end

9. Configuring Instance Types

Property Description Example
config.vm.provider Defines the cloud provider configuration block config.vm.provider "aws" do |aws|
instance_type Specifies the EC2 instance type to use instance_type = "t3.micro"

Example: AWS Instance Type Configuration

config.vm.provider "aws" do |aws|
  aws.instance_type = "t3.micro"
  aws.region = "us-west-2"
end
Note: Instance types vary by cloud provider. Ensure the instance type is supported in your target region.

10. Managing Cloud SSH Keys

Property Description Default
config.ssh.private_key_path Path to the private SSH key file used for authentication. nil
config.ssh.public_key_path Path to the public SSH key file used for authentication. nil
config.ssh.insert_key Whether to insert the key into the guest VM. true

Example: Setting SSH Keys in Vagrantfile

config.ssh.private_key_path = "~/.ssh/id_rsa"
config.ssh.public_key_path = "~/.ssh/id_rsa.pub"
config.ssh.insert_key = true
Note: Ensure the SSH keys are properly configured and have correct permissions (e.g., chmod 600) to avoid authentication issues.
Warning: If insert_key is set to false, Vagrant will not manage the keys, and you must ensure they are available in the guest VM.

Was this article helpful?