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”
output:“i-213sadas,i-123asdaf…”
ERROR IM HAVING :
│ File "./list_instances.py", line 1, in <module>
│ import boto3
│ ImportError: No module named boto3
│
│
│ with data.external.external_resource,
│ on main.tf line 3, in data "external" "external_resource":
│ 3: data "external" "external_resource" {
Does external data resource can run imported modules …?
CODE :
main.tf
terraform {}
data "external" "external_resource" {
program = ["python", "${path.module}/list_instances.py"]
query = {
cluster_name = "production"
}
}
output "output" {
value = data.external.external_resource.result
}
list_instances.py
import boto3
import sys
import json
client = boto3.client('ecs')
def list_containers(cluster_name):
response = client.list_container_instances(
cluster=cluster_name
)
return response['containerInstanceArns']
def describe_container():
query=sys.stdin.readlines()
cluster_name="empty for now"
response = client.describe_container_instances(
cluster=cluster_name,
containerInstances=list_containers(cluster_name)
)
instances=list(map(lambda x:x['ec2InstanceId'],response['containerInstances']))
sys.stdout.write(json.dumps(result))
if __name__ == '__main__':
describe_container()