Using List Comprehension and Functions in Ludwig to Create Multiple Resources Automatically

Fugue Support


In this code snippet, we’re going to use Ludwig’s powerful functional programming characteristics to minimize code sprawl and create the needed resources logically.

For this example, we’re creating the DynamoDB backend for a game that keeps player’s scores monthly.

One way you could do this is to create a composition that declares the individual tables:


Individual declarations of the monthly DDB “Score Tables”

With this approach, however, we’re talking about 10 lines of code per table. This explicitly violates the accepted coding idea of DRY - Don’t Repeat Yourself.

Fortunately, Ludwig’s functional nature has an easy way around this that will eliminate lines of code and streamline the entire infrastructure as code workflow by using functions.

Rather than writing Ludwig that creates the 12 tables individually, we can use a function that takes a string corresponding to the month of the year. The strings will come from a list in our Ludwig, and since Ludwig understands list comprehension, it minimizes the code that needs to be written.

Take a look at the example below:


Using functions and list comprehension to minimize code sprawl in Ludwig

Let’s break down what’s happening here:

  • First, we declare a function that takes a string for the name parameter and returns a DynamoDB table. With this workflow, we only need to declare the parameters of the table once


  • We create a list of months that contain strings for each month of the year.


  • After this, we pass some code that may be familiar to Python programmers, telling the Ludwig to run the makeTable function once for each month in the months list.

Using this workflow, we’ve reduced the code from over 100 lines for the year down to just 35!

Additionally, this composition can be used as a library (by removing the composition header) and imported into other compositions to separate concerns. To learn more about this, check out our docs!

This quick code snippet is just an introduction to the power of Ludwig and functional programming. The same workflow could be used to spin up a large cluster of ELBs or EC2 instances.

If you have questions on functions and list comprehension, give Fugue Support a holler: or visit

Happy Composing!

Fugue Support