Terraform Provider for Spotify

2022-04-280:1621975github.com

Terraform provider for spotify. Contribute to conradludgate/terraform-provider-spotify development by creating an account on GitHub.

docs downloads latest version code coverage

This is a terraform provider for managing your spotify playlists.

Featured tutorial - https://learn.hashicorp.com/tutorials/terraform/spotify-playlist

Featured interview - https://www.hashicorp.com/blog/build-your-summer-spotify-playlist-with-terraform

I am not affiliated with Hashicorp or Terraform.

If you are having trouble with the provider, try updating to the latest version before submitting a bug report

Example

resource "spotify_playlist" "playlist" {
  name        = "My playlist"
  description = "My playlist is so awesome"
  public      = false

  tracks = flatten([
    data.spotify_track.overkill.id,
    data.spotify_track.blackwater.id,
    data.spotify_track.overkill.id,
    data.spotify_search_track.search.tracks[*].id,
  ])
}

data "spotify_track" "overkill" {
  url = "https://open.spotify.com/track/4XdaaDFE881SlIaz31pTAG"
}
data "spotify_track" "blackwater" {
  spotify_id = "4lE6N1E0L8CssgKEUCgdbA"
}

data "spotify_search_track" "search" {
  name   = "Somebody Told Me"
  artist = "The Killers"
  album  = "Hot Fuss"
}

output "test" {
  value = data.spotify_search_track.search.tracks
}

Installation

Add the following to your terraform configuration

terraform {
  required_providers {
    spotify = {
      source  = "conradludgate/spotify"
      version = "~> 0.2.0"
    }
  }
}

How to use

First, you need an instance of a spotify oauth2 server running. This acts as a middleware between terraform and spotify to allow easy access to access tokens.

Public proxy

For a simple way to manage your spotify oauth2 tokens is to use https://oauth2.conrad.cafe. (source code)

Register a new account, create a spotify token with the following scopes

  • user-read-email
  • user-read-private
  • playlist-read-private
  • playlist-modify-private
  • playlist-modify-public
  • user-library-read
  • user-library-modify

Then take note of the token id in the URL and the API key that is shown on the page

Configure the terraform provider like so

provider "spotify" {
  auth_server = "https://oauth2.conrad.cafe"
  api_key = var.spotify_api_key
  username = "your username"
  token_id = "your token id"
}

variable "spotify_api_key" {
  type = string
}

Self hosted

If you want a bit more control over your tokens, you can self host a simple instance of the oauth2 proxy designed specifically for this terraform provider

See spotify_auth_proxy to get started.

Once you have the server running, make note of the API Key it gives you.

Configure the terraform provider like so

variable "spotify_api_key" {
  type = string
}

provider "spotify" {
  api_key = var.spotify_api_key
}

Page 2

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.


Read the original article

Comments

  • By MattIPv4 2022-04-281:365 reply

    Every time I see an integration with Spotify built for a single user to run themselves, it pains me that you have to setup an OAuth application and server to get a token for your own account. I just wish Spotify would let you generate tokens for your own account, so you can properly automate playlists etc.

    • By conradludgate 2022-04-287:01

      I provided a proxy service[0] to do the oauth handling. It gives you a permanent token that let's you access your up to date oauth tokens, of course with the caveat that you have to trust me. This is not really a scalable solution.

      I wanted to try and get the terraform provider to handle the oauth flow but it's tricky because of the way terraform providers run

      [0] https://github.com/conradludgate/oauth2-proxy

    • By beart 2022-04-283:07

      Yes! Just last weekend I threw together a quick project to create a Discord bot that adds all spotify links to a playlist. It was smooth as butter until I got to the Spotify Auth flow. Decided it wasn't worth the time at that point.

    • By izzygonzalez 2022-04-282:151 reply

      Can you automate token generation?

      The repo uses a middleware server. I'm wondering what other approaches there are.

      • By tmpz22 2022-04-282:361 reply

        You could use a browser emulation tool to complete the oauth flow but it would be a pain and ultimately brittle to the smallest changes

        • By larusso 2022-04-284:05

          I wonder how saml2aws people do this. I know not directly oauth but still a pain. And the tool works great.

    • By krageon 2022-04-287:54

      Can't you just MITM a login and use the login token? The login flow never materially changes (it's always the same two credentials), that seems like way less of a pain than whatever this OAuth flow is supposed to be.

  • By mherdeg 2022-04-284:045 reply

    OK so I'm a little puzzled by this demo (https://learn.hashicorp.com/tutorials/terraform/spotify-play...).

    The example directive "spotify_search_track" runs a server-side search on Spotify and returns a list of results, which you can use to populate a playlist.

    This is like … very cool, but also isn't it a little against the spirit of immutable infrastructure? Every time Spotify re-ranks their search results my terraform apply will populate different songs into my playlist, without anything changing in my main.tf in source control.

    • By manojlds 2022-04-286:071 reply

      That's how data sources work for serious uses as well.

      For example - get AWS AMI where we can pin it or let it be latest.

      > the spirit of immutable infrastructure

      Immutable means changes recreate the resources. Not that that there won't be any changes. Mutable infra is where we keep patching the same resource and potentially end up in a state where we are not able to recreate it.

      • By mbarneyme 2022-04-2815:211 reply

        I think they mean "deterministic," not "immutable"

        • By mherdeg 2022-04-2815:46

          Yeah on further reflection maybe the "immutable" part of this is that I never use the Spotify UI to modify my playlist - it only gets changed during terraform apply phases, which may change the playlist in random ways on each re-apply.

    • By nikanj 2022-04-287:273 reply

      This is very much the spirit of 2022: You don't change anything, but a butterfly flaps their wings in Tokyo, and now your build is broken.

      • By klabb3 2022-04-287:35

        And you can't ask the butterfly anything unless you sign an NDA.

      • By truetuna 2022-04-287:59

        Indeed. If you don't like it, prepare to get into a lot of arguments. EKS/Helm providers in combination with controllers are wild. It forces you to understand every little facet your cluster if _anything_ goes wrong.

    • By electroly 2022-04-284:18

      Maybe, but the official serious providers do stuff like this, too. Consider the feature that lets you automatically use the latest Linux or Windows AMI; it changes without you touching your .tf file.

    • By caymanjim 2022-04-284:201 reply

      There's nothing that says it has to be immutable. It's not uncommon to perform similar searches in e.g. AWS to select the latest version of some AMI or another resource that is subject to change, and applying with no changes to the Terraform code would update the resource. This isn't really "infrastructure" either. It's a neat use case.

      • By cube2222 2022-04-287:271 reply

        Moreover, you can pair it with drift detection (available in i.e. Spacelift[0]), and have it automatically detect when a data source changes (and apply the resulting infra changes).

        Very common scenario with lots of neat use cases around it. One of the more interesting ones - use a GitHub pull request listing data source to create an ephemeral environment for each PR.

        Disclaimer: Software Engineer at Spacelift

        [0]:https://spacelift.io

        • By darkwater 2022-04-2819:41

          Just received (I hope it was by chance) a cold mail from your company today. If I might be interestes by your post here, now I'm not anymore.

    • By mherdeg 2022-04-284:211 reply

      OK, here's my 2020 playlist materialized as code so that I can't accidentally clear it out and lose a track with no trace!:

      https://open.spotify.com/playlist/6NO3v396w2tZc6MnXSKNPM

      https://github.com/mherdeg/learn-terraform-spotify/blob/main...

  • By tony_cannistra 2022-04-2814:031 reply

    Let us not forget the similarly-useful Dominos Pizza terraform provider: https://github.com/nat-henderson/terraform-provider-dominos

    I could see one combining these into a nice evening.

    • By monatron 2022-04-2815:211 reply

      I once demo'ed Terraform to one of my old teams using the Dominos Pizza provider as an example. I ended up inadvertently ordering a Hawaiian style pizza to my house. It was....fine.

HackerNews