Using Amazon SES to Send Emails from Rails

I wanted to switch from Sendgrid to Amazon SES to send emails because of the difference in pricing.  Adding Amazon SES to your Rails app is just as simple as adding Sendgrid would be.  I was able to set it up based on one of the answers to this Stack Overflow questions.  There’s a gem for this, but I prefer not using a gem when the solution is this simple.

  1. Sign-up for your Amazon SES account
  2. In your management console for Amazon SES, click the button for “Verify a New Sender”
  3. Specify the email address you want to send your emails from
  4. Add an email to test receiving emails at (you can only send to verified email addresses until you get production access)
  5. Verify your email by clicking the confirmation link from the automated email Amazon sends you
  6. Go back to Amazon SES, and click on the left side for SMTP settings
  7. Click the button for “Create My SMTP credentials”
  8. Record the user name and password created
  9. Add this to your development and production.rb files:
config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :address => "",
      :user_name => "..." # Your SMTP user here.
      :password => "...", # Your SMTP password here.
      :authentication => :login,
      :enable_starttls_auto => true

You should be all set - request production access once you've tested that your app is sending the emails ok.

Hosting Ruby on Rails on Amazon Web Services

I’m experimenting with moving my apps onto Amazon EC2 from Heroku.  To do some, I’m using Amazon Elastic Beanstalk service.  Elastic Beanstalk is a free Amazon service (you don’t pay for the EB, but you pay for the resource consumption – for example your EC2 server, Elastic Load Balancing, and RDS for the database) that sets up the environment for you, including the various tools you’ll need to manage your scalable app:

  • EC2 – server for computing
  • RDS – relational database
  • Elastic Load Balancing – to help manage traffic
  • Cloud Watch – monitor performance of EC2
  • Autoscale – based on performance measures, scale up EC2 resources
  1. Start your Amazon Web Services account
  2. Go to Services > EC2
  3. Set-up a key pair, you’ll need this for SSH access to your server
  4. Use these directions on signing up for your EB account and creating the environment
    1. I used a 32-bit Linux with Ruby 1.9.3
    2. I checked the box to set-up an RDS instance (I allocated 20GB) for my MySQL DB to be hosted on Amazon as well.  Don’t forget to record your user name and password
    3. In the options for “Existing Key Pair” enter the name of the key pair you created in step 3
  5. Next, you’ll want to upload your code.  Follow these directions, with these additional considerations I learned:
    1. when I started with the commands, I thought that they were meant for Git Bash, but for me, as a Windows users, they were just meant for the standard command prompt
    2. I had trouble with the PATH command syntax on the page, but followed the syntax in the Read me file that came with the EB CLI download instead: set PATH=<path to eb>;%PATH% (Windows only).  For me this ended up: PATH=C:\Users\Me\Downloads\AWS-ElasticBeanstalk-CLI-2.3\AWS-ElasticBeanstalk-CLI-2.3\eb\windows;%PATH%
    3. I commit my code using Git GUI and then push using the Windows command prompt window
  6. To access your RDS database remotely, you need to change the security credentials for your RDS instance.  To do this, go to Services > RDS
    1. click on your DB instance link
    2. note what security groups are assigned
    3. click DB security groups in the left nav menu
    4. edit the one that was assigned to your instance (in the lower panel once you’ve clicked on the security group)
    5. add CIDR/IP and note what range AWS thinks your IP is in (it will say in small grey font under the CIDR text box) and enter that
    6. you should now be able to access your database using your favorite DB viewer from the IP you are at.  Use the endpoint as your host name and the DB user name and password you indicated in the EB set-up (step 4) to log-in
  7. To get shell access to your server, you’ll also need to adjust the security group it’s assigned:
    1. go to Services > EC2
    2. click on the link for your instance
    3. note the security group it’s assigned
    4. click on the security group on the left menu
    5. click on your security group
    6. in the panel below the security group, click the inbound tab
    7. Select create a new rule for “SSH”
    8. Add the Rule and Apply Rule Changes
    9. You should now be able to access your EC2 instance by right clicking on the instance link you saw in step 7.2
  8. To route a URL to your app, go to Services > Route 53
    1. create a “Hosted Zone” indicating your domain name
    2. take the four addresses given to you and specify them as the name servers to point your domain to, with whoever you registered your domain with
    3. open another browser window and go to Services > EC2
    4. click on the link for Load Balancers
    5. click on the record shown and below you’ll be given the A  record address to record
    6. go back to your hosted zone in Route 53
    7. click create record sets
    8. Add a new record for Type: A, click “Yes” for Alias Target, and in the Alias Target field, enter the A record address from step 5
    9. you should now be able to access your app by using your domain name