Create a Spotify playlist with Terraform
Terraform manages infrastructure on cloud computing providers such as AWS, Azure, and GCP. But, it can also manage resources in hundreds of other services, including the music service Spotify.
In this tutorial, you will use a Terraform data source to search Spotify for an artist, album, or song, and use that data to build a playlist.
Prerequisites
To complete this tutorial, you will need:
Create Spotify developer app
Before you can use Terraform with Spotify, you need to create a Spotify developer app and run Spotify's authorization proxy server.
Login to the Spotify developer dashboard.
Click the green Create an app button.
Fill out the name and description according to the table below, check the box to agree to the terms of services, then click Create.
Name | Description |
---|---|
Terraform Playlist Demo | Create a Spotify playlist using Terraform. Follow the tutorial at learn.hashicorp.com/tutorials/terraform/spotify-playlist |
Once Spotify creates the application, find and click the green Edit Settings button on the top right side.
Copy the URI below into the Redirect URI field and click Add so that Spotify
can find its authorization application locally on port 27228
at the correct path.
Scroll to the bottom of the form and click Save.
Run authorization server
Now that you created the Spotify app, you are ready to configure and start the authorization proxy server, which allows Terraform to interact with Spotify.
Return to your terminal and set the redirect URI as an environment
variable, instructing the authorization proxy server to serve your Spotify access tokens on port 27228
.
Next, create a file called .env
with the following contents to store your Spotify application's
client ID and secret.
Copy the Client ID from the Spotify app page underneath your app's title and
description, and paste it into .env
as your SPOTIFY_CLIENT_ID
.
Click Show client secret and copy the value displayed into .env
as your SPOTIFY_CLIENT_SECRET
.
Make sure Docker Desktop is running, and start the server. It will run in your terminal's foreground.
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.
Clone example repository
Clone the example Terraform configuration for this tutorial. It contains a complete Terraform configuration that searches for songs by Dolly Parton, and creates a playlist out of them.
Change into the directory.
Explore the configuration
Open main.tf
. This file contains the Terraform configuration that searches
Spotify and creates the playlist. The first two configuration blocks in the file:
- configure Terraform itself and specify the community provider that Terraform uses to communicate with Spotify.
- configure the Spotify provider with the key you set as a variable.
The next block defines a Terraform data source to search the Spotify provider for Dolly Parton songs.
The next block uses a Terraform resource to create a playlist from the first three songs that match the search in the data source block.
Open outputs.tf
, which defines an output value for the URL of the playlist.
Set the API key
Rename the terraform.tfvars.example
file terraform.tfvars
so that Terraform can
detect the file.
The .gitignore
file in this repository excludes files with the .tfvars
extension from version control to prevent you from accidentally committing your
credentials.
Warning
Never commit sensitive values to version control.
Find the terminal window where the Spotify authorization proxy server is running and copy
the APIKey
from its output.
Open terraform.tfvars
, and replace ...
with the key from the
proxy, so that Terraform can authenticate with Spotify. Save the file.
This variable is declared for you in variables.tf
.
Install the Spotify provider
In your terminal, initialize Terraform, which will install the Spotify provider.
Create the playlist
Now you are ready to create your playlist. Apply your Terraform configuration. Terraform will show you the changes it plans to make and prompt for your approval.
Confirm the apply with a yes
, and Terraform will create your playlist.
Listen to your playlist
Open the playlist URL returned in the Terraform output and enjoy your playlist!
Customize and share!
Now that you have created a playlist using Terraform, build your own Spotify playlist. Terraform maintains a state file that allows it to manage the playlist you created. Change the playlist name, the artist, specify an album, or pick specific tracks with their Spotify song IDs or URLs. See the example below for inspiration.
When you are happy with your configuration, apply your changes to edit the
playlist, and remember to confirm the plan that Terraform presents with a yes
.
Explore the unofficial Spotify provider documentation for all the available configuration options.
Share your work!
Share the playlist on Twitter or LinkedIn with the hashtag #TerraformSpotify
. Please adhere to our community
guidelines and manually scrub explicit songs from your playlist before sharing.
Here is a tweet you can customize with your playlist ID. Happy hacking!
Next steps
Get more hands-on experience with data sources by following our Query Data Sources tutorial.
Explore how Terraform manages provider versions by following our Lock and Upgrade Provider Versions tutorial.
If you're inspired by this fun provider, search for others in the registry, or learn to write a custom provider against your favorite API.