AWS Cloud Installation
To use Query Commander with API Gateway + Lambda you must attach the Lambda to a VPC subnet. Doing so may require additional configuration depending on your environment.
Intial Setup
For optimal performance two APIs are required. The REST API serves the pages and simple data objects while the Websocket enables longer running queries to be possible due to AWS timeout limitations in the API Gateway service. Without both APIs in place the maximum runtime for a query is 29 seconds.
Important
For the package creation to work properly you should execute all commands on an Amazon Linux x86_64 server as this is most similar to the Lambda runtime environment of the same architecture.
Step 1: Create the Lambda Layer
It is recommending to use pip to set up the libraries on which query commander depends. This is fairly easy to achieve using the following:
# Create a directory for the installation
mkdir ./package
# Install all the dependencies along with the core query commander
pip install \
--platform manylinux2014_x86_64 \
--target=./package \
--implementation cp \
--python-version 3.12 \
--only-binary=:all: \
--upgrade \
querycommander[lambda]
cd ./package
zip -r ../package.zip *
Now go into AWS and upload the zip file just created as a new layer. If you already have a layer you can upload the zip file as a new version on the existing layer. Make sure to select the proper runtime (Python 3.11 and/or Python 3.12 are the recommended runtimes with x86_64 architecture)
Note
Releases since 0.6.2 have included a pre-built Python layer for AWS Lambda. The pre-built layer requires Python 3.12 runtime. You can download the zip by visiting the release page.
Step 2: Create the Lambda Function
- Create a new function as "Author from Scratch".
- Enter a function name of your choice
- Select a runtime (Python 3.12 is recommended)
- Select an architecture (x86_64 is recommended)
- Under "Permissions / change default execution rule" select (or create) an execution role:
- CloudWatch Required Permissions:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- EC2 Network Permissions (required to connect Lambda to a VPC)
- ec2:CreateNetworkInterface
- ec2:DescribeNetworkInterfaces
- ec2:DescribeSubnets
- ec2:DeleteNetworkInterface
- ec2:AssignPrivateIpAddresses
- ec2:UnassignPrivateIpAddresses
- Add other permissions you plan to use like DynamoDB or S3
- CloudWatch Required Permissions:
- Under "Advanced" check the box for "Enable VPC"
- Select the desired VPC
- Select the subnets to send traffic through (you need at least 2)
- Select the security group(s)
- Click Create Function
Step 3: Create Env Variables in Function
- Open the newly created function and choose Configuration
- Select Environment variables
- Click Edit on the Environment Variables list
- Click Add environment variable
- Enter the values:
- Key: QRYCOMM_CONFIG_PATH
- Value: /var/task
- Click Save
Step 4: Add Layer to Function
- Open the function and choose Code
- Scroll to the bottom and find the Layers section and click Add Layer
- Select the Custom Layer option and then pick the layer created in Step 1
- Click Add to add the layer to the function
Step 5: Add a settings.yml file to Function
- Open the function and choose Code
- In the editor add a new file called settings.yml alongside the existing lambda_function.py file
- In the settings.yml file specify the configuration as desired.
Note
You can update the settings.yml at any time, but each time you change it you'll need to Deploy your function for the changes to take effect.
Step 6: Create the Python Handler code
In the lambda_function.py file delete all the code and add the following:
import sys
sys.path.insert(0, '/opt')
from querycommander import start
def lambda_handler(event, context):
#logging.error(str(event))
return start.as_lambda(event, context)
Once complete, save and click Deploy to publish the latest version of your function.
Step 7: Set up API Gateway REST API
- Navigate to the Lambda function in the AWS Console
- Click "Add Trigger" in the chart at the top (above the Code window)
- Select API Gateway in the drop down list
- Choose to Create a New API
- Select REST API
- Choose Open from the security box
- Click "Additional Details" to show more options
- In "Binary Media Types" choose Add
- Enter */* in the box to indicate all types
- Choose Add at the bottom to create the API
Step 8: Set up API Gateway Websocket API
- Navigate to the API Gateway service in the AWS Console
- Click Create API and select Build under a Websocket API
- Enter a API Name of your choice
- For Route selection expression enter request.body.command
- Click Next
- Click Add $default route
- Attach the Lambda integration and select the function from Step #2.
- Click Next
- Enter production in the stage name and click Next
- Click Create and deploy
Step 9: Adjust Lambda Runtime Configuration
Query Commander recommends adjusting the following:
- Set the timeout of the Lambda function to something reasonable for your use case.
- Allocate at least 512MB of Memory to the function for consistent operation.
Note
While adjusting the timeout of the Lambda function it is strongly recommended to start low and increment in stages as setting long timeouts up front may create hurdles for troubleshooting.
Making Lambda work in your VPC
Lambda can run inside a VPC, but when doing so it doesn't actually execute within the VPC itself. It executes via a gateway that AWS sets up when you select Lambda to connect to the VPC. This creates a problem because Lambda cannot send traffic over the VPC endpoint into your network and it traverse back out to the internet through a standard Internet Gateway. So, to make that work you have to use a NAT gateway. Here's the basic configuration.
- Make sure your VPC has at least two fully private subnets that do not include an Internet Gateway in their routing table
- Attach Lambda to the two fully private subnets
- Place your NAT gateway in a public subnet (a subnet with a route table that has an Internet Gateway in it)
- Set the default route of your private subnet to route traffic over to the NAT gateway in the public subnet.
Doing this allows Lambda to connect to devices inside your network while still being able to connect to AWS services like DynamoDB and S3.

Note
Any VPC Service Endpoints you create for AWS services should be in your public subnet and the NAT gateway will honor those and not send your internal AWS traffic over the public Internet.