How do I set up PostGIS on an AWS EC2 instance?
This guide will walk you through setting up an EC2 instance on AWS with PostGIS installed using Pulumi. PostGIS is a spatial database extender for PostgreSQL, adding support for geographic objects. By the end of this guide, you will have an EC2 instance running PostGIS.
Key points:
- Provision an EC2 instance on AWS.
- Install PostgreSQL and PostGIS on the instance.
- Configure security groups to allow access.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create a new security group to allow SSH and PostgreSQL access
const securityGroup = new aws.ec2.SecurityGroup("postgis-sg", {
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"] },
],
});
// Get the latest Amazon Linux 2 AMI
const ami = aws.ec2.getAmi({
filters: [
{ name: "name", values: ["amzn2-ami-hvm-*-x86_64-gp2"] },
],
owners: ["137112412989"], // Amazon
mostRecent: true,
});
// Create an EC2 instance
const instance = new aws.ec2.Instance("postgis-instance", {
instanceType: "t2.micro",
ami: ami.then(ami => ami.id),
securityGroups: [securityGroup.name],
userData: `#!/bin/bash
# Install PostgreSQL
amazon-linux-extras install postgresql10 -y
yum install postgresql-server -y
# Initialize PostgreSQL
postgresql-setup initdb
# Start PostgreSQL service
systemctl start postgresql
systemctl enable postgresql
# Install PostGIS
yum install postgis postgis-utils -y
# Create a PostgreSQL database and enable PostGIS
sudo -i -u postgres psql -c "CREATE DATABASE gisdb;"
sudo -i -u postgres psql -d gisdb -c "CREATE EXTENSION postgis;"
`,
tags: {
Name: "PostGIS-Instance",
},
});
// Export the public IP of the instance
export const publicIp = instance.publicIp;
export const publicDns = instance.publicDns;
Key Points
- Security Group: A security group is created to allow SSH and PostgreSQL access.
- EC2 Instance: An EC2 instance is provisioned using the latest Amazon Linux 2 AMI.
- User Data Script: A user data script is provided to install PostgreSQL, initialize it, start the service, install PostGIS, and set up a PostGIS-enabled database.
Summary
In this guide, we created an AWS EC2 instance with PostgreSQL and PostGIS installed. We set up a security group to allow necessary access and used a user data script to automate the installation and configuration of PostgreSQL and PostGIS. The public IP and DNS of the instance are exported for easy access.
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.