Learn how to write a Lambda function in Ruby to create a Podcast feed.

As of Nov 29, 2018 you can write server-less functions with Ruby! You can now use your favorite Ruby tools and libraries when developing lambda functions.

In case your not familiar with lambda here is how amazon describes it.

“AWS Lambda is a compute service that lets you run code without provisioning or managing servers. AWS Lambda executes your code only when needed and scales automatically, from a few requests per day to thousands per second. You pay only for the compute time you consume - there is no charge when your code is not running. With AWS Lambda, you can run code for virtually any type of application or backend service - all with zero administration. AWS Lambda runs your code on a high-availability compute infrastructure and performs all of the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, code monitoring and logging.”

Let’s give this a try and build something. I’m going to show you how to build a podcast feed using plain Ruby in the Lambda service. I will also use S3 to host the podcast mp3s.

Create Function

Create an account with AWS, if you have Amazon shopping account, you can use the same login.

Now let’s setup the Lambda function. Open up the Lambda Console.

See Video
  • Click “Create Function”
  • Give the function a name “music-podcast”
  • Select runtime “Ruby 2.5”
  • Click create function (this will take a moment)

Add code for Function

This code will be triggered later when uploading new episodes. When run it generates feed.xml file and saves it to your s3 bucket.

See Video
  • Download function code and copy/paste into the online editor. Replace the contents of lambda_function.rb with what was downloaded.
  • Click orange “Save” button.

Create S3 Bucket

Open up the S3 Console.

See Video

Create a public S3 bucket for hosting the podcast files.

  • Name it music-podcast
  • Use the same Region as the lambda function.
  • Allow public access
  • Use the default storage option.

Connect S3 Trigger

Create a trigger for the function to run every time an object is uploaded into the bucket “music-podcast/episodes”. This way the xml feed can be rebuilt when new files are added without any additional steps.

See Video
  • In the designer, pane scroll down to S3.
  • In the pane select bucket music-podcast
  • Add a Prefix of episodes/ to watch for changes within this directory.

Give Function S3 rights to bucket

See Video
  • Click on your function music-podcast within the Designer pane.
  • Scroll past your code to a pane labeled “Execution role”
  • Click link “View the music-podcast-role-xxxxxxx”. This will open up IAM.
  • From IAM click the blue button “Attach policies”
  • In the search box type “s3”
  • Check “AmazonS3FullAccess”
  • Click Attach policy.

Create S3 user with rights to upload to bucket

The lambda function code is going to need user credentials to read from the bucket to get the episode files and then to post back the feed.xml.

See Video
  • Go to IAM
  • Click Users
  • Add User
  • User name of podcaster
  • Only click “Programmatic access”
  • Click “Next: Permissions”
  • Click “Attach existing policies directly”
  • Search “s3” and check it.
  • Click through steps and create user.
  • Make sure to copy down the “Access key ID” and the “Secret access key”. You can add it to the secrets.yml next we will upload that to our function.

Create secrets.yml in Lambda Console

Let give your function a way to access those newly created credentials.

See Video
  • Create a new file in your lambda function “secrets.yml”
  • Paste in the contents that you copied from the last step.
  • Click save.

Setup The Podcast

To have a podcast you must have some recordings preferable in mp3 format. Let’s assume that you don’t have them yet, but you need some filler episodes to test this out. So I’m going to use some Classical Music license under Creative Commons.

Each directory will represent a episode number with the mp3 and some metadata in it.

/music-podcast/episodes/1
  ↳ audio.mp3
     metadata.json
/music-podcast/episodes/2
  ↳ audio.mp3
     metadata.json
/music-podcast/episodes/3
  ↳ audio.mp3
     metadata.json

The JSON metadata will look something like this.

{
    "title": "Episode 1: Beethoven: Für Elise",
    "description": "Beethoven: Für Elise",
    "published": "2019-05-01"
}

If you don’t want to create all these files and directors yourself download my example episodes to get started.

See Video
  • Upload the episodes directory to the root of the music-podcast bucket.
  • Make sure to set it as public

Public Feed URL

Shortly after you will see the feed.xml appear in the root of the bucket. , here is what mine looks like.

Get the public URL to publish.

See Video
  • Click on the bucket music-podcast and then feed.xml
  • Scroll to the bottom and copy the link under “Object URL”

All Done!

You can play around, add a few other episodes, edit the metadata, and see the feed re-built.

Let me know in the comments what you think you might build with Lambda using Ruby.

P.S

Special Thanks to Jake Mauer and Lolo Zhang for creating my logo.