How-tos

Build a Static IP Rails app with Bluemix and Statica

Share this post:

Many web services require consumers to specify known IP addresses that their requests will originate from to prevent abuse and attacks from the wider Internet. This is commonly referred to as “IP whitelisting”. Due to the scalable nature of Bluemix, the IP address of your Bluemix app will frequently change as you deploy and scale making it impossible to guarantee your IP address ahead of time.

Statica solves this problem by offering a proxy service with guaranteed static IP addresses that you can send your traffic through as shown in the diagram below. The firewalled resource on the right will see all requests from your application as originating from your Statica IP address 2.2.2.2 rather than the dynamic IPs of your Bluemix application instances:

Statica IP mapping

In this article, we’ll be using Statica Service on Bluemix to send HTTP requests from a Ruby on Rails app deployed on Bluemix via a Static IP address. It’s really simple; so let’s get started!

What you’ll need to build your application

  • Familiarity with Ruby on Rails
  • A Bluemix account. If you don’t have one, register here.
  • A DevOps Services account

Steps to clone and deploy the sample application

Step 1: Get the project code

Click statica-bluemix-rails-sample to visit the project page on DevOps Services. You can either clone the project via its GitHub URL or download a zip of the project and extract it locally. This will drop a ready to go Rails app in to your local file system.

To get the example running instantly, click the Deploy to Bluemix button and a running version of the project will be instantly setup in your own Bluemix account:

Deploy to Bluemix

Step 2: Create your Bluemix app

  • Log into Bluemix and click “Create An App” from your Dashboard.
  • Click Web and then Ruby and enter in an app name, e.g. statica-bluemix-rails-123.

Step 3: Add Statica to your app

  • On your application overview click Add a service or API.
  • Browse down until you find Statica under the Web and Application category.
  • Leave the default free Starter plan selected and create the service.

Step 4. Test your app locally

Now that you have some Statica credentials you can test your app locally.

  • Click Show Credentials and copy the STATICA_URL value.
  • On your local machine set the STATICA_URL environment variable with this value (this connection string includes your username & password so keep it safe), e.g. export STATICA_URL=http://username:password@proxy.statica.io:9293.
  • Change in to your project folder (where you cloned or extracted the project files to).
  • Run bundle install in your project folder.
  • When installed run rails s to start your server.
  • Go to http://localhost:3000 to see your demo application running. On the left you should see your local IP address. On the right you should see your Statica Static IP address. This shows that some outbound HTTP requests have gone successfully via your Statica proxy.

Step 5: Understand what’s going on

We have now sent some requests via a Static IP address, but how is this configured?

Open up say_controller.rb under app/controllers to see how Statica is being used. We are making an HTTP request to ip.statica.io as an example of accessing an HTTP web service using the RestClient library.

Making a request with no RestClient options will by default originate from the IP of your Bluemix app – this is the dynamic IP address that is liable to change.

To make the request via a Static IP, we extract the Statica proxy connection string from either the Bluemix provided VCAP_SERVICES configuration variable or the STATICA_URL environment variable when running locally.

We then set the RestClient proxy variable to this connection string. All subsequent requests are now sent via Statica and you can successfully access web services from a Static IP address!

Step 6. Deploy to Bluemix

Lastly the most fun part – seeing your application run on Bluemix. Just run:

cf push {your-app-name}

When deployment is complete navigate to your app’s URL and you should see something like this:

Conclusion

You’ve now created a Rails app capable of connecting to web services with IP whitelisting from Bluemix. As you can see it takes only a few lines of code to integrate and can be used both from your own development environment and all your Bluemix apps. Check out Statica’s documentation for examples on connecting with databases and LDAP services and for samples in lots of different languages and frameworks.

More stories
May 1, 2019

Two Tutorials: Plan, Create, and Update Deployment Environments with Terraform

Multiple environments are pretty common in a project when building a solution. They support the different phases of the development cycle and the slight differences between the environments, like capacity, networking, credentials, and log verbosity. These two tutorials will show you how to manage the environments with Terraform.

Continue reading

April 29, 2019

Transforming Customer Experiences with AI Services (Part 1)

This is an experience from a recent customer engagement on transcribing customer conversations using IBM Watson AI services.

Continue reading

April 26, 2019

Analyze Logs and Monitor the Health of a Kubernetes Application with LogDNA and Sysdig

This post is an excerpt from a tutorial that shows how the IBM Log Analysis with LogDNA service can be used to configure and access logs of a Kubernetes application that is deployed on IBM Cloud.

Continue reading