Creating Multiple Spotify Playlists Using Terraform

Creating Multiple Spotify Playlists Using Terraform

Project Overview

This project involves using Terraform to create multiple Spotify playlists for different occasions like morning, evening, party night, etc. Terraform will be used to automate the creation and management of these playlists.

Prerequisites

  1. Terraform Installed: Ensure Terraform is installed on your machine.

  2. Docker Installed: Make sure Docker is installed and running.

  3. Spotify Account: You need a Spotify account (without premium access)

  4. Spotify Developer Account: Register and create an application to get the Client ID and Client Secret.

  5. Spotify Provider for Terraform: Install and configure the Spotify provider for Terraform.

  6. VS Code Editor: Recommended for editing Terraform files.

Steps to Complete the Project

1. Creating Terraform Code

Start by setting up your Terraform project.

  1. Create a new directory for your Terraform project and navigate to it in your terminal.

  2. Create a file named main.tf.

2. Define Provider

In provider.tf, define the Spotify provider:

Resources used (communtiy provider) : https://registry.terraform.io/providers/conradludgate/spotify/latest

provider.tf

terraform {
  required_providers {
    spotify = {
      source = "conradludgate/spotify"
      version = "0.2.7"
    }
  }
}

provider "spotify" {
  # Configuration options
}

3. Need API Key

To interact with Spotify's API, you need a Client ID and Client Secret.

API KEY + Client ID + Client Secret : To get this data go to “Spotify developer dashboard

Login to dev eloper spotify > Dashboard > Create APP

Fill Out the details and create app:

Name: My Playlist through Terraform
Description: Create multiple Spotify playlists using Terraform.

*Redirect URIs: http://localhost:27228/spotify_callback

To get Client ID + Secret Key > Go to Settings:

Get your client ID and Secret from here:

Create a file named .env to store your Spotify application's Client ID and Secret:

SPOTIFY_CLIENT_ID=<your_spotify_client_id> 
SPOTIFY_CLIENT_SECRET=<your_spotify_client_secret>

4. Using “Spotify-auth-proxy”

Helps to connect Terraform with Spotify : Proxy created by same person who created Spotify provider : Check terraform registry .

5. Run the Spotify AUTH App and Get the API Key

Make sure Docker Desktop is running, and start the authorization proxy server:

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

Here’s how you will get the API Key : Put this key in code with help of variable

You should get “Authorization Successful” Message.

6. Initialize and Apply Terraform Configuration

terraform init

Ready to create playlist

Apply the Terraform configuration

terraform plan
terraform apply

In order to configure playlist go to the resources provided: Named Spotify playlist

https://registry.terraform.io/providers/conradludgate/spotify/latest/docs/resources/playlist

7. Verify Playlists on Spotify

Files to be created:

.env

SPOTIFY_CLIENT_ID=07e78a331ddc4da38ff9b6021f2c2b37
SPOTIFY_CLIENT_SECRET=6aed187073024bca9cca4012f59540a4
SPOTIFY_REDIRECT_URI=http://localhost:27228/spotify_callback

playlist.tf

resource "spotify_playlist" "best_of_bollywood" {
  name = "Bollywood"
  tracks = ["0eCajpR75pDW0r64U6hP2x"]
}

providers.tf

terraform {
  required_providers {
    spotify = {
      source = "conradludgate/spotify"
      version = "0.2.7"
    }
  }
}

provider "spotify" {
  # Configuration options
  api_key = var.api_key
}

terraform.tfvars

api_key = "TZN1iNiV1tDVXuzkfEpqXJRo5m2Qr5v9aasJf7pGR5a-i1kzrAPMRLtI2PpYwjuV"

variables.tf

variable "api_key" {
  type = string
}

Conclusion

By following these steps, you can automate the creation and management of multiple Spotify playlists using Terraform. This approach not only saves time but also ensures consistency across your playlists. Customize the playlists and tracks as per your preference to suit different occasions.