This script allows you to synchronizes songs from a source playlist to a target playlist using the Spotify API. The function is triggered by a scheduled event in AWS CloudWatch Events, which executes the function at a specified time interval using a cron schedule.
The script is written in Python and is orchestrated the following AWS services:
- AWS Lambda: the function is hosted on the Lambda service.
- AWS Secrets Manager: stores the client ID, client secret, and refresh token, access token, and expiry time for the Spotify API.
- AWS CloudWatch Events: function is triggered by a scheduled event created using CloudWatch Events.
- AWS CodeBuild: builds and deploys the Lambda function to AWS.
+-------------------------+
| CloudWatch Events |
+-------------------------+
|
v
+-----------------+ +--------------------+
| AWS Lambda | < -- | AWS Code Build |
+-----------------+ +--------------------+
|
v
+-----------------+
| Secrets Manager |
+-----------------+
|
v
+-----------------+
| Spotify API |
+-----------------+
The function flow is as follows:
- The Lambda function source code is pushed to the AWS CodeCommit repository.
- AWS CodeBuild builds the Lambda function from the source code.
- The built function is deployed to AWS Lambda.
- The Lambda function is triggered by the scheduled event from CloudWatch Events.
- The function retrieves the client ID, client secret, and refresh token from AWS Secrets Manager.
- The function then retrieves the access token and its expiry time from AWS Secrets Manager, refreshing if needed.
- The function initializes the Spotify object with the access token.
- The function retrieves the tracks in the source playlist and the target playlist.
- The function filters out the tracks that already exist in the target playlist.
- The function adds the new tracks to the target playlist.
- The function returns the number of new tracks added to the target playlist.
In order to use this implementation, you need:
-
AWS Account
-
Spotify Premimum Account
-
The following AWS services:
- AWS Lambda
- AWS Secrets Manager
- AWS CloudWatch Events
- AWS CodeCommit
- AWS CodeBuild
- Log in to the Spotify Developer Dashboard.
- Click the "Create an App" button and follow the prompts to create a new Spotify app.
- Add http://localhost:8080/callback/ to the Redirect URIs in the Spotify app settings.
- Once your app is created, make note of the Client ID and Client Secret values.
- In order to generate your initial refresh token, follow this guide
-
Log in to the AWS Management Console.
-
Navigate to the AWS Secrets Manager service.
-
Create a new secret with the following key-value pairs:
{ "spotify_client_id": "<YOUR_SPOTIFY_CLIENT_ID>", "spotify_client_secret": "<YOUR_SPOTIFY_CLIENT_SECRET>", "refresh_token": "<YOUR_SPOTIFY_REFRESH_TOKEN>" }
- Install the AWS CLI:
Follow the instructions provided in the AWS CLI user guide to install the AWS CLI for your operating system.
- Configure the AWS CLI:
Once you have installed the AWS CLI, you need to configure it to use your AWS account. To do this, open a terminal or command prompt and run the following command:
aws configure
You will be prompted to enter your AWS access key ID, secret access key, default region name, and default output format. Enter the requested information to configure the AWS CLI.
- Verify authentication credentials:
To verify that your authentication credentials are working, run the following command:
aws sts get-caller-identity
This command should return information about your AWS account.
-
Log in to the AWS Management Console.
-
Navigate to the AWS Lambda service.
-
Create a new Lambda function with the following settings:
- Runtime: Python 3.8
- Execution Role: New role with basic permissions to access Secrets Manager and CloudWatch Logs.
-
Copy and paste the code from the provided
lambda_function.py
script into the Lambda function editor. -
Set the
source_playlist_id
andtarget_playlist_id
variables in the code to the Spotify IDs of the source and target playlists. -
Save the Lambda function.
-
Create a new scheduled event in AWS CloudWatch Events to trigger the Lambda function at a specified time interval.
The Lambda function will automatically run at the specified time interval and add any new tracks from the source playlist to the target playlist. You can monitor the execution of the Lambda function in the CloudWatch Logs.