Run PEX as Data Source
This module runs the provided PEX binary in a portable manner that works with multiple platforms and python versions, to be used as an external data source in Terraform.
This module uses prepare-pex-environment
under the hood. See What is
PEX? for more details on what is a PEX file and how to construct one
for use with this module.
Data Source vs Resource
Terraform provides two escape hatches where a first-class Terraform provider is not more appropriate. The escape hatches allow you to call out to arbitrary binaries available on the operator machine. These are:
- External Data Source, where you can run the binary as a data source.
- local-exec Provisioners, where you can run the binary to provision a resource.
This module uses the data source approach (you can see the run-pex-as-resource module for running it as a data source). Which approach to use depends on your needs:
- Data sources are calculated every time a terraform state needs to be refreshed. This includes all
plan
andapply
calls, even if the data source isn't explicitly changed. - Data sources are useful if the logic can be used to determine if a resource needs to be changed.
- Data sources can output values that can be used in other parts of the Terraform code. You cannot do this with the provisioner approach.
- There are limitations with Data Sources and dependencies. See this terraform issue comment for example.
- Provisioners with a
null_resource
implements the standard resource life cycle (create, destroy, etc). - Provisioners with a
null_resource
have explicit controls on when to trigger.
Sample Usage
- Terraform
- Terragrunt
# ------------------------------------------------------------------------------------------------------
# DEPLOY GRUNTWORK'S RUN-PEX-AS-DATA-SOURCE MODULE
# ------------------------------------------------------------------------------------------------------
module "run_pex_as_data_source" {
source = "git::git@github.com:gruntwork-io/terraform-aws-utilities.git//modules/run-pex-as-data-source?ref=v0.10.5"
# ----------------------------------------------------------------------------------------------------
# REQUIRED VARIABLES
# ----------------------------------------------------------------------------------------------------
# Parts of the path (folders and file names) to the python package directory
# housing the pex file.
pex_module_path_parts = <list(string)>
# Parts of the path (folders and files names) to the PEX executable for python
# as a list of strings.
python_pex_path_parts = <list(string)>
# Main function of the script, encoded as SCRIPT_MODULE:FUNCTION. So for
# example, if the main function of the script is in a file named
# `entrypoint.py` which houses the function `main`, then this should be
# `entrypoint:main`.
script_main_function = <string>
# ----------------------------------------------------------------------------------------------------
# OPTIONAL VARIABLES
# ----------------------------------------------------------------------------------------------------
# The arguments to pass to the command as a string
command_args = ""
# The query for the command run as a data source.
command_query = {}
# If you set this variable to false, this module will not run the PEX script.
# This is used as a workaround because Terraform does not allow you to use the
# 'count' parameter on modules. By using this parameter, you can optionally
# enable the data source within this module. Note that when false, the
# 'result' output will be null.
enabled = true
}
# ------------------------------------------------------------------------------------------------------
# DEPLOY GRUNTWORK'S RUN-PEX-AS-DATA-SOURCE MODULE
# ------------------------------------------------------------------------------------------------------
terraform {
source = "git::git@github.com:gruntwork-io/terraform-aws-utilities.git//modules/run-pex-as-data-source?ref=v0.10.5"
}
inputs = {
# ----------------------------------------------------------------------------------------------------
# REQUIRED VARIABLES
# ----------------------------------------------------------------------------------------------------
# Parts of the path (folders and file names) to the python package directory
# housing the pex file.
pex_module_path_parts = <list(string)>
# Parts of the path (folders and files names) to the PEX executable for python
# as a list of strings.
python_pex_path_parts = <list(string)>
# Main function of the script, encoded as SCRIPT_MODULE:FUNCTION. So for
# example, if the main function of the script is in a file named
# `entrypoint.py` which houses the function `main`, then this should be
# `entrypoint:main`.
script_main_function = <string>
# ----------------------------------------------------------------------------------------------------
# OPTIONAL VARIABLES
# ----------------------------------------------------------------------------------------------------
# The arguments to pass to the command as a string
command_args = ""
# The query for the command run as a data source.
command_query = {}
# If you set this variable to false, this module will not run the PEX script.
# This is used as a workaround because Terraform does not allow you to use the
# 'count' parameter on modules. By using this parameter, you can optionally
# enable the data source within this module. Note that when false, the
# 'result' output will be null.
enabled = true
}
Reference
- Inputs
- Outputs
Required
pex_module_path_parts
list(string)Parts of the path (folders and file names) to the python package directory housing the pex file.
python_pex_path_parts
list(string)Parts of the path (folders and files names) to the PEX executable for python as a list of strings.
script_main_function
stringMain function of the script, encoded as SCRIPT_MODULE:FUNCTION. So for example, if the main function of the script is in a file named entrypoint.py
which houses the function main
, then this should be entrypoint:main
.
Optional
command_args
stringThe arguments to pass to the command as a string
""
Details
We don't use null here because this is interpolated into the python script.
command_query
map(string)The query for the command run as a data source.
{}
enabled
boolIf you set this variable to false, this module will not run the PEX script. This is used as a workaround because Terraform does not allow you to use the 'count' parameter on modules. By using this parameter, you can optionally enable the data source within this module. Note that when false, the 'result' output will be null.
true
Data source result of executing the PEX binary.