How do I install TimescaleDB on AWS EC2?
This guide demonstrates how to install TimescaleDB on an AWS EC2 instance using Pulumi. We will provision an EC2 instance, configure security groups, and use a startup script to install TimescaleDB.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Define a new security group to allow SSH and PostgreSQL access
const securityGroup = new aws.ec2.SecurityGroup("timescaleDbSecurityGroup", {
description: "Allow SSH and PostgreSQL access",
ingress: [
{ protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] }, // SSH
{ protocol: "tcp", fromPort: 5432, toPort: 5432, cidrBlocks: ["0.0.0.0/0"] }, // PostgreSQL
],
egress: [
{ protocol: "tcp", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] }, // Allow all outbound traffic
],
});
// Define an EC2 instance to run TimescaleDB
const timescaleDbInstance = new aws.ec2.Instance("timescaleDbInstance", {
instanceType: "t2.micro",
ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2 AMI
securityGroups: [securityGroup.name],
userData: `#!/bin/bash
# Update the package repository
sudo yum update -y
# Install PostgreSQL
sudo amazon-linux-extras install -y postgresql10
# Add TimescaleDB repository and install it
echo "[timescale]
name=timescale
baseurl=https://packagecloud.io/timescale/timescaledb/el/7/x86_64
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/timescale/timescaledb/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt" | sudo tee /etc/yum.repos.d/timescale_timescaledb.repo
sudo yum install -y timescaledb-postgresql-10
# Enable TimescaleDB in PostgreSQL
sudo echo "shared_preload_libraries = 'timescaledb'" >> /var/lib/pgsql/10/data/postgresql.conf
# Restart PostgreSQL service
sudo systemctl restart postgresql-10
# Enable PostgreSQL service to start on boot
sudo systemctl enable postgresql-10
`,
tags: {
Name: "TimescaleDB-Instance",
},
});
// Export the public IP of the instance
export const publicIp = timescaleDbInstance.publicIp;
export const publicDns = timescaleDbInstance.publicDns;
Key Points
- Security Group: Configured to allow SSH and PostgreSQL access.
- EC2 Instance: Provisioned with Amazon Linux 2 AMI, and a startup script to install TimescaleDB.
- Startup Script: Installs PostgreSQL and TimescaleDB, configures PostgreSQL to use TimescaleDB, and ensures the service starts on boot.
Summary
In this guide, we created an AWS EC2 instance and installed TimescaleDB using Pulumi. The instance is configured to allow SSH and PostgreSQL access, and a startup script handles the installation and configuration of TimescaleDB.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.