Using External Data Source and Python imported modules

Hey there,first time using the forum !

Trying to create an external data resource that runs a Python script which returns all EC2 instances id’s for a specific cluster name.

For example :
input “cluster_name”:“production”


│   File "./", line 1, in <module>
│     import boto3
│ ImportError: No module named boto3
│   with data.external.external_resource,
│   on line 3, in data "external" "external_resource":
│    3: data "external" "external_resource" {

Does external data resource can run imported modules …?


terraform {}

data "external" "external_resource" {
  program = ["python", "${path.module}/"]

  query = {
    cluster_name = "production"

output "output" {
  value = data.external.external_resource.result

import boto3
import sys
import json

client = boto3.client('ecs')

def list_containers(cluster_name): 
    response = client.list_container_instances(
    return response['containerInstanceArns']

def describe_container():
    cluster_name="empty for now"
    response = client.describe_container_instances(
    instances=list(map(lambda x:x['ec2InstanceId'],response['containerInstances']))

if __name__ == '__main__':

Hi @Eyal159!

From the external provider’s perspective here, python is just an arbitrary program to run and what that program does is entirely a matter of how it’s implemented.

With that said, I don’t know if any technical reason why Python wouldn’t be able to find imported libraries when running from the external provider, as long as you’ve installed that library in one of the search paths that python is configured to use.

If you are using a mechanism like virtualenv/venv to create an isolated environment with its own set of libraries then I believe the usual way to make that work is to run the python executable that is included inside the virtual environment, in its bin directory, which therefore effectively adds the extra directories to the library search path. However, if that isn’t sufficient then you may have better results asking about this in a forum for Python rather than for Terraform, since the participants there are more likely to know the details of how venv works and how Python searches for libraries in general.

Hey @apparentlymart !

Thanks you for the comment.

I understand your suggestion,and it works as expected(when providing the full path)

Is there a way to see the given JSON from terraform to the program ?