Developing in Rails on AWS Cloud9
I used Cloud9 a few years back for a Rails project while they were still an independent startup and it was all pretty seamless to get up and running on their cloud IDE.
Fast forward a few years and they have been acquired by Amazon and integrated into the AWS suite of services. However, upon logging into my AWS and trying out AWS Cloud9, I realised that it’s not as straightforward getting a Rails dev environment set up as before.
After much Googling, I finally got everything set up so I decided to compile this handy guide for future reference.
Setup Cloud9 on AWS
To get started, you need to have an AWS account. Sign in to your AWS console at https://console.aws.amazon.com/.
Configure IAM Groups and Users
Whilst you can use your AWS root account to create Cloud9 environments, it is highly recommended that you create IAM users with appropriate Cloud9 access rights. This prevents unrestricted access to other services within your AWS account and allows you to manage access for other Cloud9 users in your team.
We will create two IAM Groups, Cloud9Admins and Cloud9Users:
- Cloud9Admins have permission to create new Cloud9 environments. This group should be restricted to trusted users for administering the platform.
- Cloud9Users have permission to use existing Cloud9 environments. Regular team members should be assigned to the Cloud9Users group.
- From the AWS console open IAM from the Services menu.
- In the IAM console’s navigation pane, choose Groups.
- Choose Create New Group.
- On the Set Group Name page, for Group Name, enter either Cloud9Admins OR Cloud9Users
- Choose Next Step.
- On the Attach Policy page, search for the filter term: cloud9
7. Select either AWSCloud9Administrator (if creating the Cloud9Admins group) OR AWSCloud9User (if creating the Cloud9Users group)
8. Choose Next Step, then Create Group.
9. Repeat steps 1–8 until both Cloud9Admins and Cloud9Users groups are created.
Create Users and Assign to Groups
- In the IAM navigation pane, choose Users.
- Choose Add user.
- On the Details page, for User name, type a name for the new user.
- Select Programmatic access and AWS Management Console access.
- Choose a password method for the user to suit your requirements.
- Choose Next: Permissions.
- On the Permissions page, leave the default choice of Add users to group.
- In the list of groups, select the box next to the group you want to add the user to. Either Cloud9Admins OR Cloud9Users
- Choose Next: Review.
- On the Review page, choose Create user
- Repeat steps 1–10 to create additional users or admins.
Create a Cloud9 Environment
Note: Cloud9 uses an AWS EC2 instance to host your files and services. In this example we will use a t2.micro instance which may be eligible for free-tier pricing. However, you could be charged EC2 hosting fees for Cloud9 past this point. Please ensure you are aware of the free-tier and other usage fees for your environment before continuing.
- Sign-in to your AWS console as a Cloud9Admins user.
- Open Cloud9 from the Services menu.
- Choose Create environment.
- From the Name environment page, enter a Name for your environment
- From the Configure settings page, select both Create a new instance for environment (EC2) and t2.micro (1GiB RAM + 1 vCPU)
- Choose Next step.
- Choose Create environment
Install Postgres via Yum
sudo yum install postgresql96 postgresql96-server postgresql96-devel postgresql96-contrib postgresql96-docs
Run postgres service init
sudo service postgresql96 initdb
Edit postgres conf to connect via localhost:5432
sudo vim /var/lib/pgsql96/data/postgresql.conf
listen_addresses = 'localhost'
port = 5432
pg_hba.conf file for
sudo vim /var/lib/pgsql9/data/pg_hba.conf
Edit conf to be:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all ec2-user 127.0.0.1/0 trust
# IPv6 local connections:
host all all ::1/128 md5
Start / Restart postgres server
sudo service postgresql96 start
sudo service postgresql96 restart
Login as Postgres User and Change Password / Add ec2-user
sudo su - postgres # login as postgres userpsql -U postgres # login to postgres db as postgres user
ALTER USER postgres WITH PASSWORD 'YOUR_PASSWORD';
CREATE USER "ec2-user" SUPERUSER;
ALTER USER "ec2-user" WITH PASSWORD 'YOUR_PASSWORD';
Logout of postgres user account:
Set up Rails app
Clone or create a new application then run
Configure a Run Configuration
AWS Cloud9 has built-in tools for running a Rails server for previewing your app in a web browser. Configure as follows:
- Right-click the Run menu, then choose New Run Configuration
- From the Terminal area (Fig 7), in the Run Config Name field, enter <appname>
- From the Terminal area, in the Command field, enter rails s -b 0.0.0.0
- From the Terminal area, choose CWD, then choose the <appname> folder from the list.
- Click Run.
Previewing in your Browser
To view the output of your rails server in a browser, Cloud9 provides an automatically generated private URL that is connected to your server:
- From the main menu, choose Preview > Preview Running Application
- In the Browser window that appears, click Pop Out Into New Window
- The application should now be running in your browser!