Update: A video of this post is available here.
After creating a Cloud Foundry Micro on an Amazon EC2 instance (described here), I decided to make it available as a public AMI so that others could try it out (especially since the Micro is not available unless you built it yourself using Altoros vagrant or Nise BOSH).
To use this you need to sign up for an Amazon AWS account.
Start a Cloud Foundry Instance
- Once you have an account, launch ami-98c956a8 (currently available in us-west-2; add a comment if you want it available elsewhere).
- Confirm that the manifest reads “366179850620/Cloud Foundry Micro” and click Continue.
- Change the instance type to m1.small and click Continue.
- In the ‘User Data:’ field you may enter some optional customizations (each on its own line) and click Continue.
- password: mySecret (AMI rules prohibit default passwords so if you don’t provide your own we will generate a random one);
- domain: cloud.example.com (if you don’t provide a domain, we will assign <public-IP>.xip.io as the domain); and
- debug: true (adds some debugging information to /var/log/boot.log).
- Review the ‘Storage Device Configuration’ (an 8 GB root volume and an ephemeral instance store) and click Continue.
- You may give a ‘Name’ tag to your EC2 instance, say CF Demo, and click Continue.
- You may select or create a key pair to be used to log in to your server (optional, but useful), and click Continue.
- Select or create a Security Group with at least HTTP access and click Continue.
- ICMP – Echo Request (optional, to allow your server to respond to ping);
- TCP – SSH (optional, to allow you to log on to your server using a private key); and
- TCP – HTTP (required, to interact with Cloud Foundry and the applications you push to the server).
- Review the configuration information and click Launch.
- Click View your instances on the Instances page to discover the public IP address.
Identify the Domain
To use the server you need to know its domain name.
- If you provided a domain in the User Data above, then you need to create a record set in your domain name server to point your domain and all subdomains (using the ‘*’ wildcard match) to the indicated address; or
- If you did not provide a domain, then your domain is <public-IP>.xip.io (a DNS that maps all requests to the given IP).
Log on to the Server (Optional)
- Identify the path to the private key associated with the key pair you selected or created when you created the EC2 instance.
- From a command shell (on Mac, Linux, or Unix), or an SSH client on Windows (such as PuTTY), connect to the server. E.g.,
ssh -i /path/to/my/private/key.pem ubuntu@domain
- Once connected you can explore the server.
sudo /var/vcap/bosh/bin/monit summary # check Cloud Foundry status (all running except cloud_controller_jobs) tail /var/log/boot.log # check here if things don't seem to start properly cat ~/domain # show the configured domain (from User Data or public IP) cat ~/password # show the configured password (from User Data or random generation) cd /var/vcap/data/sys; sudo chmod +rx log; cd log; ll # list of log file directories
- You can execute a single command the server using ssh:
ssh -i /path/to/my/private/key.pem ubuntu@hostname_or_domain cat password
Use the Server
To use the server you can refer to the cf command line reference. For example, on your local machine create and set up the environment:
mkdir ~/cloud ~/cloud/ruby; cd ~/cloud/ruby
sudo gem install bundle sinatra cf
Create three files using your favorite text editor:
Gemfile:
source 'https://rubygems.org' ruby '1.9.3' gem 'sinatra'
env.rb:
require 'rubygems'
require 'sinatra'
configure do
disable :protection
end
get '/' do
host = ENV['VCAP_APP_HOST']
port = ENV['VCAP_APP_PORT']
"<h1>Hello World!</h1><h2> I am in the Cloud! via: #{host}:#{port}</h2>"
end
get '/env' do
res = ''
ENV.each do |k, v|
res << "#{k}: #{v}<br/>"
end
res
end
config.ru:
require ‘./env.rb’
run Sinatra::Application
To create a ‘Gemfile.lock’ from the ‘Gemfile’ run the following command:
bundle
I can test the application by running the following command:
ruby env.rb
When it tells me that Sinatra has taken the stage I enter http://localhost:4567/ and http://localhost:4567/env in a web browser.
Then I can use ‘cf’ to set my target, login, do some configuration, and push my application to the cloud (replacing <my-ip> and mySecret with your server’s public IP and password):
cf target http://api.<my-ip>.xip.io
cf login --password mySecret admin
# okay to ignore CFoundry::InvalidRelation error in next command
# (see https://github.com/cloudfoundry/cf/issues/9)
cf create-space development
cf target --space development
cf map-domain --space development <my-ip>.xip.io
cf push
If the push is successful, it will show the URL at which you can see the application. When you care done you can stop and/or terminate your EC2 instance.