This repository is part of this blog post.
Create a Vagrant + Chef configuration to create a AWS EC2 instance with NFS. Why? Because I want to use this instance as a NFS server to share directories with large files.
- Install these software:
- Vagrant: Virtual Machine provider
- Vagrant AWS Plug-in: integrate Vagrant with AWS EC2
- Vagrant Omnibus Plug-in: Omnibus to install Chef client
- Chef SDK: to upload Chef artifacts
- (Optional) VirtualBox: if you want to test it locally
- Configure the following accounts:
- Fork this repository
- Configure your AWS credentials (aws.properties)
- Upload Chef cookbooks and roles
- Update and run Vagrant configuration
If you have downloaded the repository, you should have the following structure:
Where boxes are Vagrant configurations and chef-repo is the Chef repository with 3 cookbooks (line and nfs downloaded from Chef Supermarket) and 2 roles: one for NFS Server and other for NFS Clients.
You should add the following file to your aws directory "boxes/aws":
keys:
access_key_id: [access key id]
secret_access_key:[secret access key]
key_pair_location: [path to <key_pair>.pem]
You should upload Chef artifacts to consume them from AWS instance. If you try it locally, you don't need to update them.
-
Copy cookbooks and roles directories on you chef-repo directory from your Chef Starter Kit. (To learn more about Chef: Learn Chef)
-
Upload your artifacts using knife tool:
cd [chef-repo directory]
knife upload *
Now that you have your Chef recipes online, you can configure your Vagrant files:
Vagrantfile:
# To load properties files
require "yaml"
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Load box properties
props = YAML.load_file("box.properties")
# Load AWS properties (credentials)
aws_props = YAML.load_file("../aws.properties")
config.vm.box = "#{props['box']['name']}"
# Target to aws.box file that only contains AMI id (is not used)
config.vm.box_url = "file://#{props['box']['base_location']}"
config.vm.provider :aws do |aws, override|
# Set AWS credentials
aws.access_key_id = "#{aws_props['keys']['access_key_id']}"
aws.secret_access_key = "#{aws_props['keys']['secret_access_key']}"
aws.keypair_name = "jeqo"
# Set instance configurations - define it depending on your requirements
aws.instance_type = "t2.micro"
aws.region = "us-east-1"
aws.availability_zone = "us-east-1a"
aws.ami = "ami-9eaa1cf6"
override.ssh.username = "ubuntu"
override.ssh.private_key_path = "#{aws_props['keys']['key_pair_location']}"
aws.tags = {
'Name' => "#{props['box']['name']}"
}
# Define storage
aws.block_device_mapping = [{
'DeviceName' => '/dev/sda1',
'Ebs.VolumeSize' => props['box']['disk_size']
}]
# Define security group (inbound ports that should be open: TCP: 111, 2049, 32768, 44182, 54508 and UDP: 111, 2049, 32768, 32770-32800)
aws.security_groups = "nfs-group"
config.ssh.pty = true
end
# Instal last chef client software
config.omnibus.chef_version = :latest
# Increment SWAP on AWS EC2 Instance
config.vm.provision "shell" do |s|
s.path = "increase_swap.sh"
end
# Chef server configuration
config.vm.provision "chef_client" do |chef|
chef.chef_server_url = "https://api.opscode.com/organizations/[organization]"
chef.validation_client_name = "[organization]-validator"
chef.validation_key_path = "#{props['chef']['repo_location']}/.chef/[organization]-validator.pem"
chef.node_name = "#{props['box']['name']}"
# Role uploaded to configure NFS Server
chef.add_role "nfs-server"
end
end
It's important to follow these recommendations to create AWS EC2 Security Group.
You can check how to use it in this video:
TODO: upload youtube video
Note: If you want to test locally, you need to download this box for NFS Server (Ubuntu) and this one for NFS Client (Oracle Linux) and change box url on box.properties file.