v0.8.0: Variables, Tagging mods & Syntax highlighting →

Writing Queries

Steampipe queries can provide valuable insight into your cloud configuration, and the interactive client is a powerful tool for ad hoc queries and exploration. Often, however, you will write a query that you will want to re-run in the future, either manually or perhaps as a cron job. Steampipe allows you to save your query to a file, and pass the file into the steampipe query command.

For example, lets create a query to find S3 buckets where versioning is not enabled. Paste the following snippet into a file named s3_versioning_disabled.sql:

select
name,
region,
account_id,
versioning_enabled
from
aws_s3_bucket
where
not versioning_enabled;

We can now run the query by passing the file name to steampipe query

steampipe query s3_versioning_disabled.sql

You can even run multiple sql files by passing a glob or a space separated list of file names to the command:

steampipe query *.sql

Named Queries

Steampipe also allows you to use named queries. Steampipe will create a query object for every .sql file in the root of your workspace directory. By default, the workspace directory is the working directory when you launched steampipe, though you can specify a different directory with the --workspace argument. You can run these queries in an interactive session, or from from command line, by name.

If you created the s3_versioning_disabled.sql file in a directory other than the current directory, change to that directory. Start the interactive query shell:

steampipe query

You can run your query by name:

query.s3_versioning_disabled

Your saved queries even show up in the auto-complete list, making them easier to find and recall!

You can even run the named query in batch mode:

steampipe query "query.s3_versioning_disabled"

Steampipe makes it easy to build a library of custom queries that you can effortlessly recall and re-use!