Vagrant Operations: Difference between revisions
Jump to navigation
Jump to search
Line 62: | Line 62: | ||
ENVIRONMENT_NAME = "infra-playground" | ENVIRONMENT_NAME = "infra-playground" | ||
BASTION_HOST = ' | BASTION_HOST = '1.2.3.4' | ||
AWS_KEYPAIR = 'infra-playground' | AWS_KEYPAIR = 'infra-playground' | ||
SSH_PRIVATE_KEY = "~/.ssh/#{AWS_KEYPAIR}.pem" | SSH_PRIVATE_KEY = "~/.ssh/#{AWS_KEYPAIR}.pem" | ||
Line 72: | Line 72: | ||
# of the environment. | # of the environment. | ||
# | # | ||
SUBNET_ID = 'subnet- | SUBNET_ID = 'subnet-000000000000000' | ||
# | # | ||
Line 79: | Line 79: | ||
# requirements. At minimum, it should allow inbound ssh access. | # requirements. At minimum, it should allow inbound ssh access. | ||
# | # | ||
SECURITY_GROUP_ID = 'sg- | SECURITY_GROUP_ID = 'sg-000000000000000' | ||
# | # | ||
# The AMI of the infra-worker image created by Packer | # The AMI of the infra-worker image created by Packer | ||
# | # | ||
AMI_ID = ENV['AMI_ID'] || 'ami- | AMI_ID = ENV['AMI_ID'] || 'ami-0000000000000000' | ||
INSTANCE_TYPE = 'm5.4xlarge' | INSTANCE_TYPE = 'm5.4xlarge' |
Revision as of 00:01, 17 November 2019
External
Internal
Box Operations
List
vagrant box list
Instance Operations
Start and provision the vagrant environment:
vagrant up
If property configured, bringing the machine up may create a ~/.ssh/dev/vagranthostname configuration that can be used by ssh to connect directly.
Connect to machine via SSH:
vagrant ssh
How does it know to connect to the instance it was started? More about jump box.
Status of the VM:
vagrant global-status
vagrant status [name|id]
Stops and deletes all traces of the vagrant machine:
vagrant destroy vagrant destroy [name|id]
If the corresponding VM runs in AWS EC2, this terminates the instance.
Managing a VM in an AWS Environment
Describe the VM in a Vagrantfile
This is an example. More details about Vagrantfile syntax in:
# frozen_string_literal: true
#
# Configuration
#
#
# It is assumed that the environment was already configured with AWS authentication (.aws/credentials or AWS
# environment variables). The VM will be created in the configured account/region.
#
ENVIRONMENT_NAME = "infra-playground"
BASTION_HOST = '1.2.3.4'
AWS_KEYPAIR = 'infra-playground'
SSH_PRIVATE_KEY = "~/.ssh/#{AWS_KEYPAIR}.pem"
SSH_CONFIG_FILE = "#{ENV['HOME']}/.ssh/dev/infra-worker"
#
# This is required by AWS CLI as part of instance provisioning call. It must the ID of the private subnet
# the instance will be attached to. If using environments, this subnet should be the default private subnet
# of the environment.
#
SUBNET_ID = 'subnet-000000000000000'
#
# This is required by AWS CLI as part of instance provisioning call. It must the ID of the security
# group that will protect the access to the instance. Access must be configuring depending on the instance
# requirements. At minimum, it should allow inbound ssh access.
#
SECURITY_GROUP_ID = 'sg-000000000000000'
#
# The AMI of the infra-worker image created by Packer
#
AMI_ID = ENV['AMI_ID'] || 'ami-0000000000000000'
INSTANCE_TYPE = 'm5.4xlarge'
VOLUME_SIZE_GB = 50
#
# Install vagrant plugin
#
# @param: plugin type: Array[String] desc: The desired plugin to install
def ensure_plugins(plugins)
logger = Vagrant::UI::Colored.new
result = false
plugins.each do |p|
pm = Vagrant::Plugin::Manager.new(
Vagrant::Plugin::Manager.user_plugins_file
)
plugin_hash = pm.installed_plugins
next if plugin_hash.key?(p)
result = true
logger.warn("Installing plugin #{p}")
pm.install_plugin(p)
end
if result
logger.warn('Re-run vagrant up now that plugins are installed')
exit
end
end
def aws_config(name)
value = `aws configure get #{name}`.strip
raise ArgumentError, "aws #{name} config must be set; use 'aws configure set #{name} <value>'" if value.empty?
value
end
def username
ENV['USER'] || 'anonymous'
end
def hostname
"infra-worker-#{username}"
end
#
# Create and configure the AWS instance(s)
#
Vagrant.configure('2') do |config|
ensure_plugins(%w[vagrant-aws])
config.vm.define :infra_worker do |t|
end
config.vm.hostname = hostname
#
# Use dummy AWS box
#
config.vm.box = 'dummy'
config.vm.synced_folder '.', '/vagrant', disabled: true
#
# Specify AWS provider configuration
#
config.vm.provider 'aws' do |aws, override|
#
# Specify SSH keypair to use, which should match SSH_PRIVATE_KEY
#
aws.keypair_name = AWS_KEYPAIR
aws.instance_type = INSTANCE_TYPE
aws.associate_public_ip = false
aws.elastic_ip = false
#
# Launch configuration
#
aws.ami = AMI_ID
aws.subnet_id = SUBNET_ID
aws.security_groups = [SECURITY_GROUP_ID]
aws.block_device_mapping = [{ 'DeviceName' => '/dev/xvda', 'Ebs.VolumeSize' => VOLUME_SIZE_GB }]
aws.tags = {
'Name' => hostname,
'Created by' => username,
'Environment' => ENVIRONMENT_NAME
}
#
# Specify username and private key path
#
config.ssh.forward_agent = true
override.ssh.username = 'ec2-user'
override.ssh.private_key_path = SSH_PRIVATE_KEY
override.ssh.proxy_command = "ssh -o ExitOnForwardFailure=yes -W %h:%p -i #{override.ssh.private_key_path} %r@#{BASTION_HOST}"
end
config.trigger.after [:up] do |t|
t.info = "Writing ssh config to #{SSH_CONFIG_FILE}"
t.run = { path: '../_common_tools_and_config/bin/configure-ssh-access', args: [SSH_CONFIG_FILE.to_s] }
end
end