Deploy rails app on Elastic Beanstalk from scratch
During the study of rails development, at some point, you may need to deploy it onto AWS Elastic Beanstalk(EB). This is what I am recently doing. Together with three other members, we are devolopping a website Bidder to bid items. This is what I did to deploy our website on EB. I used Command Line Interface(CLI) instead of console for enviroment setup.
Reference: Bryce’s instruction, AWS documents
1. Launch an EC2 instance
- Go to AWS console, click EC2, click launch instance
- Select Amazon Linux AMI 2016.09.0 (HVM), SSD Volume Type - ami-b04e92d0
- Select t2.micro(free tier eligible), click “review and launch”
- Click “edit security group”
- Choose “create a new security group”, use the default name
- Choose type “ssh”, restrict source with “myip”.
- Click launch
- Choose an existing key pair or create a new key pair
Note: instance type, security group, ssh source can be modified according to the needs
2. SSH to your instance with your key pair
- view your instance from AWS console, get its public DNS
chmod 400 your_key_pair_name.pem
ssh -i yourkeypair.pem ec2-user@your_public_DNS
3. Set up environment
1. Install the Elastic Beanstalk Command Line Interface (EB CLI)
pip install —upgrade —user awsebcli
- add the path to the executable file to your PATH (add below to ~/.bash_profile and source it)
export PATH=~/.local/bin:$PATH
- verify that eb cli has been installed using
eb —version
2. Make changes to your rails app, clone your git repo
- make changes following Bryce’s instruction
- install git in your EC2 instance
sudo yum install git
- clone your git repo to your EC2 instance
3. init(configure) EB
- cd to your git repo
- run
eb init
- select region(use default)
- provide your credentials
- to get a credential
- go to AWS console, click your name on the right side of the header, click security credentials
- click Get Started with IAM Users( or Users on the left side bar)
- click create new users, enter name, check generate an access key for each user
- download the credential
- give user permission
- back to Users main page
- click the user name
- click permissions
- click attach policy, add amazon EC2 full access, AWSElasticBeanstalkFullAccess
- to get a credential
- select an application to use: choose default
- It seems you are using Ruby, is it correct?: choose y
- select platform version: 1) Ruby 2.3
- Do you want to set up SSH for your instances?:y
- Select a key pair.: choose your key pair
4. create a deployment for demo
eb create -db.engine postgres -db.i db.t2.micro -db.user u --envvars SECRET_KEY_BASE=RANDOM_SECRET --single DEPLOYMENT_NAME
Replace DEPLOYMENT_NAME with a unique name for your deployment. Consider teamname-demo.
Replace RANDOM_SECRET with some semi-random large string of alphanumeric characters. Note, for real sites you want this to be really random as your cookies are signed/encrypted using a key derived from this string.
Hit enter and type in password(at least 8 letters’ long)