Creating a Spotify Playlist with Terraform

In my quest to master Terraform, I decided to take on a few challenges. While Terraform is widely used for managing cloud infrastructure, it can also interact with various services including Spotify. In this tutorial, you’ll learn how to use Terraform to search for an artist, album, or song on Spotify and create a playlist automatically.

Prerequisites

Before getting started, ensure you have the following:

  • Terraform installed
  • Docker Desktop installed
  • A Spotify account with developer access

Step 1: Set Up a Spotify Developer App

To integrate Terraform with Spotify, you must first create a Spotify developer app and configure its authorization settings.

  • Click Create an App
  • And fill in the details:
    • Name: DJ Terraform Playlist Demo
    • Description: Create a Spotify playlist using Terraform
    • Add the following Redirect URI http://localhost:27228/spotify_callback  and click Add.
  • Agree to the terms and click Create.

Step 2: Run the Authorization Server

The authorization proxy server allows Terraform to authenticate with Spotify. 

  • In your terminal, set the redirect URI as an environment variable: export SPOTIFY_CLIENT_REDIRECT_URI=http://localhost:27228/spotify_callback
  • Create an .env file and store your Spotify Client ID and Client Secret:
  • Go to Dashboard. Then click on the name of your app

  • At the top right side, click on Settings
  • In Basic information panel, copy the Client ID  and paste it into .env as your SPOTIFY_CLIENT_ID.
  • Click on View client secret and copy the Client Secret to paste it into .env as your SPOTIFY_CLIENT_SECRET.

You should get something similar to:

  • Next, run the authorization server using Docker:
    • Open Docker Desktop 
    • Open your terminal and run docker run –rm -it -p 27228:27228 –env-file ./.env ghcr.io/conradludgate/spotify-auth-proxy 
       
  • Visit the authorization server’s URL by visiting the link that your terminal output lists after Auth:.

  • The server will redirect you to Spotify to authenticate. After authenticating, the server will display Authorization successful, indicating that the Terraform provider can use the server to retrieve access tokens.

  • Leave the server running.

  • Create a folder named learn-terraform-spotify
  • Create terraform configuration files inside the folder: main.tf, outputs.tf, terraform.tfvars, variables.tf

Step 4: Configure Terraform

  • Open main.tf, which contains Terraform configurations for:
    • Defining the Spotify provider
    • Searching for tracks
    • Creating a playlist
  • Set the Spotify API key:
    • Copy the APIKey from the authorization proxy server output.
    • Open terraform.tfvars and paste it.
  • This variable is declared for you in variables.tf.

Find all the terraform configuration files in this repository: learn-terraform-spotify.

Step 5: Install the Spotify provider

  • In your terminal, initialize Terraform, which will install the Spotify provider.
  • Run terraform init

Step 6: Create the Playlist

  • Now that everything is set up, it’s time to create your playlist.
  • Apply your Terraform configuration, and Terraform will display the planned changes before asking for your approval.
  • Type Yes, then Enter.
  • It’s time to listen to your playlist: Open the playlist URL returned in the Terraform output and enjoy your playlist. 

Voilà! You have created a Spotify playlist with terraform. You can now modify the Terraform configuration to create playlists with your favorite artists and genres.

Leave a Reply

Your email address will not be published. Required fields are marked *