DEV Community

Haripriya Veluchamy
Haripriya Veluchamy

Posted on

Automating Spotify Playlist Creation with Terraform: My Journey to "Perfect"

Creating Spotify playlists manually is cool, but automating it? Now that's where the magic begins! ✨ I decided to take on this challenge using Terraform, and what better way to symbolize my learning journey than by creating a playlist featuring Ed Sheeran's Perfect?

I'm calling it setchukos playlist—you can check it out here 🎧.

🌟 Why Terraform?

Terraform is an amazing tool for automating infrastructure, but did you know you can use it for API-based projects too? 🤔 This was my first venture outside the usual Terraform use cases, and while I'm not perfect at Terraform yet, this playlist project felt like the perfect way to learn. 💪

🛠️ How I Built the Playlist

Here's a step-by-step guide on how I used Terraform to create my Spotify playlist:

Step 1: 🔑 Setting Up Spotify Developer Credentials

  1. Go to the Spotify Developer Dashboard and log in with your account.
  2. Create a new app and note down the Client ID and Client Secret.
  3. Add the redirect URI http://localhost:27228/spotify_callback in your app settings.

Step 2: ✅ Authenticating with Spotify

To get the API key, I created a .env file:

SPOTIFY_CLIENT_ID=<your_client_id>
SPOTIFY_CLIENT_SECRET=<your_client_secret>
Enter fullscreen mode Exit fullscreen mode

Then, I ran the Spotify authentication proxy using Docker:

docker run --rm -it -p 27228:27228 --env-file .env ghcr.io/conradludgate/spotify-auth-proxy
Enter fullscreen mode Exit fullscreen mode

🎉 Once authenticated, I received the API key to connect Terraform with Spotify.

Step 3: 📄 Writing the Terraform Code

I created a main.tf file to define the playlist and add Ed Sheeran's "Perfect" track:

provider "spotify" {
  api_key = "<your_api_key>"
}

resource "spotify_playlist" "setchukos_playlist" {
  name        = "setchukos playlist"
  description = "A playlist featuring Ed Sheeran's Perfect."
  public      = true
}

resource "spotify_track" "perfect" {
  uri         = "spotify:track:0tgVpDi06FyKpA1z0VMD4v"
  playlist_id = spotify_playlist.setchukos_playlist.id
}
Enter fullscreen mode Exit fullscreen mode

Step 4: created terraform.tfvars , var.tf file

To store the sensitive information like api key i used tfvars file then i refered the api_key into my var.tf file

Step 5: 🚀 Deploying with Terraform

Initialize the Terraform configuration:

terraform init
Enter fullscreen mode Exit fullscreen mode

Apply the configuration:

terraform apply
Enter fullscreen mode Exit fullscreen mode

🎉 The Result

After running the Terraform code, my setchukos playlist was live on Spotify! ✨

It's a simple playlist with just one song—Perfect by Ed Sheeran—but it means a lot to me.

Here's the link to the playlist: setchukos playlist 🎶.

🤔 What I Learned

This project was a fantastic learning experience. Here are my key takeaways:

  • Terraform is Versatile: It's not just for cloud infrastructure—you can use it for creative projects too!
  • APIs Open Doors: Working with Spotify's API taught me how APIs can be leveraged for automation.
  • Learning is a Journey: I may not be perfect at Terraform yet, but this project brought me closer to my goals. 💪

📝 Conclusion

If you're new to Terraform or looking for a fun project, try creating your own Spotify playlist! 🚀 It's a great way to learn, explore, and listen to your favorite tunes.


Did you find this article helpful? Let me know in the comments below! And don't forget to follow me for more tech tutorials and automation tips. 🚀

Top comments (0)