titlesidebar_labeldescription
Deploy a QuestDB AMI to AWS using Packer
AWS AMI using Packer
This document describes how to build an AMI with QuestDB installed using Packer and how to run this AMI on Amazon Web Services

This guide describes how to create an AWS Amazon Machine Image (AMI) based on Amazon Linux with QuestDB installed and uses the official QuestDB packer AMI with a template that can be used as a point of reference to create your own AMIs.

This document also covers details on applying networking rules via security groups to allow access to the REST API and web console publicly accessible or by whitelisted IPs, and how to enable logging to CloudWatch via the AWS CLI.

Prerequisites

Verifying AWS CLI configuration

To check that the AWS CLI is configured correctly, run the following command:

  1. aws configure list

The configuration should be returned showing which profile, credentials and region is configured:

  1. Name Value Type Location
  2. ---- ----- ---- --------
  3. profile demo manual --profile
  4. access_key ****************XYZ1 shared-credentials-file
  5. secret_key ****************XYZ2 shared-credentials-file
  6. region eu-central-1 config-file ~/.aws/config

If no configuration has been set up for the AWS CLI yet, run the following command to set up the CLI:

  1. aws configure

Clone the Packer repository

To configure and build the machine image, clone the official GitHub repository with a Packer template:

  1. git clone https://github.com/questdb/questdb-packer-ami.git

A placeholder configuration file for QuestDB server settings can be found at the following location:

  1. ./src/config/server.conf

The included configuration does not override any settings, therefore default server configuration will be used in the AMI. For a comprehensive list of properties which may be set, refer to the QuestDB server configuration documentation.

Build and provision the AMI

The src directory contains a template.json file which may be used as a starting point for your own AMI.

  1. cd questdb-packer-ami/src
  2. # view template contents
  3. cat template.json

The template uses Amazon Linux 2 with the t3.micro instance type. The default template variables may be passed on the command line using -var <var_name>=<var_value> to specify a specific region and AMI base name, i.e.;

  • aws_region=us-east-1
  • base_ami_name=questdb

The following command will build the QuestDB machine image and create it in the eu-central-1 region with the name <base_ami_name>-amzn2-<timestamp>:

  1. packer build -var 'aws_region=eu-central-1' template.json

Log output from Packer will show a Packer Builder EC2 instance creating the image:

  1. ==> ami: Prevalidating any provided VPC information
  2. ==> ami: Prevalidating AMI Name: questdb-amzn2-...
  3. ==> ami: Creating temporary keypair: packer_607...
  4. ...
  5. ==> ami: Deleting temporary keypair...
  6. Build 'ami' finished after 5 minutes 13 seconds.
  7. ==> Builds finished. The artifacts of successful builds are:
  8. --> ami: AMIs were created:
  9. eu-central-1: ami-0a...

To view the details of the image, the following AWS CLI command will describe AMIs created by the current AWS account:

  1. aws ec2 describe-images --owners self

The QuestDB image should be listed as one of the available images. Make a note of the ImageId value which will be referred to in the following section as <ami_id>:

  1. {
  2. "Images": [
  3. {
  4. "Architecture": "x86_64",
  5. "CreationDate": "2021-04-19T14:24:15.000Z",
  6. "ImageId": "ami-0a5...",
  7. "ImageLocation": "123451234567/questdb-amzn2-1618842140",
  8. "ImageType": "machine",
  9. "Public": false,
  10. "OwnerId": "123451234567",
  11. "PlatformDetails": "Linux/UNIX",
  12. "UsageOperation": "RunInstances",
  13. "State": "available",
  14. "BlockDeviceMappings": [
  15. {
  16. "DeviceName": "/dev/xvda",
  17. "Ebs": {
  18. "DeleteOnTermination": true,
  19. "SnapshotId": "snap-085...",
  20. "VolumeSize": 8,
  21. "VolumeType": "gp2",
  22. "Encrypted": false
  23. }
  24. }
  25. ],
  26. "Description": "An Amazon Linux 2 AMI with QuestDB installed.",
  27. "EnaSupport": true,
  28. "Hypervisor": "xen",
  29. "Name": "questdb-amzn2-1618842140",
  30. "RootDeviceName": "/dev/xvda",
  31. "RootDeviceType": "ebs",
  32. "SriovNetSupport": "simple",
  33. "VirtualizationType": "hvm"
  34. }
  35. ]
  36. }

Enable networking and launch an instance

Instances using this AMI with QuestDB installed can be directly launched from the CLI. For convenience, we will first allow networking on instance creation via a security group. For this guide, we will enable port 9000 which allows HTTP access.

  1. aws ec2 create-security-group \
  2. --group-name questdb-sg \
  3. --description "QuestDB security group"

Make a note of the security group ID returned from this command and pass it as the <sec_group_id> variable below to allow ingress on TCP port 9000 for IPV4 and IPV6:

  1. aws ec2 authorize-security-group-ingress \
  2. --group-id <sec_group_id> \
  3. --ip-permissions \
  4. IpProtocol=tcp,FromPort=9000,ToPort=9000,IpRanges='[{CidrIp=0.0.0.0/0}]' \
  5. IpProtocol=tcp,FromPort=9000,ToPort=9000,Ipv6Ranges='[{CidrIpv6=::/0}]'

:::info

  • In this example, ingress on port 9000 is open for requests originating from any IP. This is a security group configuration used for illustrative purposes only. When deploying QuestDB to production, only trusted or required public IP addresses should be allowed.

  • Users may also want to enable networking for other types of traffic. QuestDB listens for PostgreSQL wire protocol by default on port 8812 and InfluxDB Line Protocol on ports 9009 for TCP and UDP.

:::

Launch the AMI with the security group attached:

  1. aws ec2 run-instances --count 1 \
  2. --image-id <ami_id> \
  3. --security-group-ids <sec_group_id>

Verifying the deployment

To find the public IP address of the QuestDB instance, run the following command, replacing the instance ID in the filters parameter (<ami_id>):

  1. aws ec2 describe-instances \
  2. --filters "Name=image-id,Values=<ami_id>" \
  3. --query "Reservations[].Instances[].PublicIpAddress"

The CLI will return the public IP of EC2 instances running the QuestDB AMI created in the preceding section. To verify that the web console of this running instance is active:

  1. Copy the External IP of the instance
  2. Navigate to <external_ip>:9000 in a browser

import Screenshot from “@theme/Screenshot”

Alternatively, a request may be sent against the REST API exposed on port 9000:

  1. curl -G \
  2. --data-urlencode "query=select * from telemetry_config" \
  3. <external_ip>:9000/exec

For more information on using this functionality, see the official documentation for using the QuestDB REST API.

Logging using AWS CloudWatch

The AMI uses Linux logrotate utility to automatically trim and archive logs generated by QuestDB. The AWS CloudWatch agent is also pre-installed and configured to start sending log messages. To make the logs available on your CloudWatch dashboard, an instance profile with the CloudWatchAgentServerPolicy IAM policy must be associated with the EC2 instance running QuestDB.

The following two JSON documents may be copied directly without modifications to assign an IAM role with the correct permissions for logging. Create a file trust_policy.json file with the following contents which allows EC2 instances to assume the role:

  1. {
  2. "Version": "2012-10-17",
  3. "Statement": [
  4. {
  5. "Effect": "Allow",
  6. "Principal": {
  7. "Service": "ec2.amazonaws.com"
  8. },
  9. "Action": "sts:AssumeRole"
  10. }
  11. ]
  12. }

Create a permission policy document permission_policy.json which provides permissions to write to CloudWatch:

  1. {
  2. "Version": "2012-10-17",
  3. "Statement": [
  4. {
  5. "Effect": "Allow",
  6. "Action": [
  7. "cloudwatch:PutMetricData",
  8. "ec2:DescribeVolumes",
  9. "ec2:DescribeTags",
  10. "logs:PutLogEvents",
  11. "logs:DescribeLogStreams",
  12. "logs:DescribeLogGroups",
  13. "logs:CreateLogStream",
  14. "logs:CreateLogGroup"
  15. ],
  16. "Resource": "*"
  17. },
  18. {
  19. "Effect": "Allow",
  20. "Action": ["ssm:GetParameter"],
  21. "Resource": "arn:aws:ssm:*:*:parameter/AmazonCloudWatch-*"
  22. }
  23. ]
  24. }

Create the IAM role using these documents:

  1. # create a role with EC2 trust policy
  2. aws iam create-role --role-name QuestDB-Log-Role \
  3. --assume-role-policy-document file://trust_policy.json
  4. # attach permission policy to allow publishing to cloudwatch
  5. aws iam put-role-policy --role-name QuestDB-Log-Role \
  6. --policy-name QuestDB-CloudWatch-Permissions-Policy \
  7. --policy-document file://permission_policy.json

Create an instance policy, attach the IAM role, and associate the IAM role with the EC2 instance running QuestDB:

  1. # create an instance profile
  2. aws iam create-instance-profile --instance-profile-name QuestDB-CWLogging
  3. # add the logging role
  4. aws iam add-role-to-instance-profile \
  5. --instance-profile-name QuestDB-CWLogging \
  6. --role-name QuestDB-Log-Role
  7. # associate it with the QuestDB instance
  8. aws ec2 associate-iam-instance-profile --instance-id <instance_id> \
  9. --iam-instance-profile Name=QuestDB-CWLogging

Create a CloudWatch log group and log stream:

  1. aws logs create-log-group --log-group-name "/questdb-<instance_id>"
  2. aws logs create-log-stream --log-group-name "/questdb-<instance_id>" \
  3. --log-stream-name "questdb-<instance_id>"

To read the latest log events from this stream from the CLI, use the following command:

  1. aws logs get-log-events --log-group-name "/questdb-<instance_id>"\
  2. --log-stream-name "questdb-systemd-service.log"

A JSON object containing the most recent events will be returned:

  1. {
  2. "events": [
  3. {
  4. "timestamp": 1618907396917,
  5. "message": "...http-server connected [ip=1.2.3.4, fd=19]",
  6. "ingestionTime": 1618907735820
  7. },
  8. {
  9. "timestamp": 1618907396917,
  10. "message": "...I i.q.c.h.p.StaticContentProcessor [19] incoming [url=/]",
  11. "ingestionTime": 1618907735820
  12. },
  13. ...