đ This article is part of a series on setting up Neon for dev, test, and staging environments while keeping your prod database in RDS. Find out what weâre talking about.Â
In a previous blog post, we showed you how to create a Neon Twin using a GitHub Action that automatically runs a pg_dump of your RDS production database and restores it to Neon on a recurring nightly schedule.
In this blog post, weâll guide you through setting up a Slack Webhook to send notifications to a public Slack channel. This will inform your team about the latest pg_dump and restore activities.
By using Slack instead of relying on GitHub email notifications, you can avoid the need to add every developer who needs this information to the GitHub Repository where the Action runs.Â
Weâll also cover how to surface more detailed information about the dump and restore including the database name, size how long the job took to complete, and how to handle failures.
PrerequisitesÂ
Before diving in, ensure you have the following:
- Completion of previous steps. Make sure you have completed all the steps outlined in the previous article, particularly the setup of the Neon Twin and the GitHub Action for pg_dump and restore.
- Slack API admin access. Youâll need administrator privileges to your companyâs Slack workspace to create and manage Slack apps and webhooks.
- GitHub repo access. Ensure you have access to the GitHub repository where the Actions will run, including permissions to manage Actions and Secrets.
Quick start
đ All the code shown in this article can be found on this GitHub repo: create-neon-twin-slack.yml.Â
There are two additional Actions that you might like to use for reference:Â Â
- create-neon-twin-default.yml: A simple workflow that handles just a pg_dump and restore
- create-neon-twin-ssl.yml: An example of how to handle SSL Certificates as outlined in this community blog post: How to Use PostgreSQL SSL certificates in GitHub Actions.
How to build Slack notifications to monitor pg_dump and restoresÂ
In this section, we will guide you through how to set up Slack notifications for monitoring your pg_dump and restore workflows. The process looks like this:Â
- Creating a Slack channel in your companyâs Slack workspace for receiving notification
- Setting up and configuring a Slack App using the Slack developer console
- Modify your existing GitHub Actions workflow (from the previous blog post)
- Develop JavaScript functions to post formatted notifications to your Slack channel
- Commit changes and deploy the action
Letâs get to it.
Create the Slack channelÂ
To start, set up a dedicated channel in your companyâs Slack workspace for receiving notifications. For this example, weâll use the channel name #rds-to-neon-twin.
Create a Slack app
Weâll use the Slack developer console to create and configure an app. This involves naming the app, granting it access to your Slack workspace, and generating a Webhook URL for sending notifications.
If you have administrator privileges to your companyâs Slack account, head over to https://api.slack.com/apps and click Create New App.
Create Slack app from manifest
From the available options select, From an app manifest.
Select workspaceÂ
From the dropdown list: Pick a workspace to develop your app.
Slack app name
Edit the JSON data and give your application a name.
App summary
Click Create to finish creating your Slack application.
Basic Information
With your app created, navigate to: Basic Information from the sidebar and update the Display Information. These details will be visible when you add the app to your companyâs Slack workspace.
Active Slack webhooks
Navigate to: Incoming Webhook and click the toggle switch to On.
Add webhook to workspace
On the same page, scroll down and click the Add New Webhook to Workspace button.
Select channel
From the dropdown list select the channel youâd like to post notification to.
Webhook URL
You will now be able to Copy the Webhook URL. Add this as an env var in your project and name it SLACK_WEBHOOK_URL
as youâll need this in the next step.
Update GitHub Action
Now, itâs time to update the workflow we set up in the previous blog post. To post a notification to Slack with information about the pg_dump/restore job there are a number of additions that need to be made to the .yml file, and three new JavaScript files that are responsible for posting a formatted message to Slack for both success and failure scenarios.Â
To begin with, add the following to your .yml file and weâll explain what each part is for.
Environment variables
There are two new variables this action needs. The Slack Webhook URL from the previous step (this one also needs to be added to your GitHub Secrets) and a Node version which is set âgloballyâ so that later steps can run the success and failure JavaScript scripts.Â
Capture start time
The capture-start-time
job does as the name describes and captures the time the job started. Using GitHub Actions outputs
, the value of the current time is stored as start_time
so it can be referenced by the post-to-slack-success
, which weâll explain in a later step. Â
Query database
The db-query
job is where you can query the production database and determine the database_size
, and database_name
. You could perform any queries in this job that suit your needs. Both values are stored as outputs so they can also be referenced by the post-to-slack-success
job. This job needs
the dump-and-restore
job.Â
Capture end time
Similar to the start time job this job captures the value of the current time and stores it as end_time
so it can also be referenced by the post-to-slack-success
job.Â
Post success
This job is triggered by GitHub Actions built in if: ${{ success() }}
condition. Only if the previous jobs as defined by needs
are successful will this job run. The four environment variables are set using values from the previous jobs. They are:
- DATABASE_SIZE
- DATABASE_NAME
- JOB_START_TIME
- JOB_END_TIME
Setting environment variables in this way makes them available to JavaScript / Node environments.Â
The last part of this job is to run the src/slack-success-.js
file which weâll cover in a moment.Â
Post failure
Similar to the success job, this job is triggered by if: ${{ failure() }}
and is only run if any of the previous jobs defined by needs
fail. Unlike the success job, there are no environment variables.
The last part runs the src/slack-failure.js
file which weâll cover in the following steps.
Create Slack notification scripts
To complete the setup, we need to create the scripts that will handle sending notifications to Slack. There are three essential files required for this:
- src/slack-success.js: This script is triggered when all jobs complete successfully and sends a success notification to the Slack channel.
- src/slack-failure.js: This script is triggered when any job fails and sends a failure notification to the Slack channel.
- src/format-date.js: A utility function to format the date and time values captured by the previous jobs.
Additionally, you need to install the dotenv dependency to allow these scripts to access your environment variables and GitHub Secrets.
To install the dependency, run:
Format date
Create a directory named src
, then create a file named format-date.js
and add the following code.
Slack success
Create a file named slack-success.js
and add the following code. This file is used when all jobs complete successfully.
The above code will create a formatted message using Slack special message syntax and includes information about your job. The message that appears in Slack will look similar to the below. You can read more about posting messages in the Slack documentation.
You can use Slackâs Block Kit Builder to create a message format that suits your needs and or create different environment variables that can be used to surface important information within the message.
Slack failure
Create a file named slack-failure.js
and add the following code. This file is used when any jobs fail.
The above code will create a formatted message using Slack special message syntax but doesnât include information about your job. The message that appears in Slack will look similar to the below. You can read more about posting messages in the Slack documentation.
You can use Slackâs Block Kit Builder to create a message format that suits your needs.
Deploy Action
The final step is to commit your changes and deploy the action. To complete the deployment,Â
- Navigate to your GitHub repoÂ
- Go to Settings > Secrets and variables > Actions
- Click on New repository secret
- Add the following environment variable:
SLACK_WEBHOOK_URL
ââDuring development, you can manually trigger the workflow from the GitHub UI:
- Navigate to Actions in your repository
- Select the create-neon-twin workflow
- Click on Run workflow
And you can run the JavaScript files directly from your terminal for testing, although some environment variables, DATABASE_SIZE
, JOB_START_TIME
, etc, wonât be available.Â
Finished
And thatâs it. You now have a full pg_dump and restore of your production database ready and waiting for you as a Neon Twin, plus, a convenient way for everyone involved to be notified when a new Twin is available!
Next steps
Continue building the workflow: Navigate to Part IV of the series â How to deploy a chance tested in Neon to prod in RDS
In case you missed itÂ
Part I: Building a Neon Twin: Move Dev/Test/Staging to Neon, Keep Production on RDS
Part II: Optimize your AWS RDS Dev Environments with Neon Postgres
Check out the Twin Thing appâit helps you build your Github Action workflows to build your Neon Twins: