unable to parse JSON using terraform external data

2019-07-29 12:28发布

问题:

USE CASE

I wanted to provision an EFS in a given region with an EC2 instance and then mount that instance with EFSand if EFS doesn't exist in that region then I need to provision an alternate of EFS.

PROBLEMS

  1. Amazon is not offering EFS service in every region.
  2. There is no way to identify the availability of service using aws-cli. Already asked but didn't get an answer yet link

WHAT I TRIED

This is the case where amazon provide the service, If I run this command curl -ls https://elasticfilesystem.us-east-1.amazonaws.com system returns the following output.

<MissingAuthenticationTokenException>
  <Message>Missing Authentication Token</Message>
</MissingAuthenticationTokenException>

And if I change the region to eu-west-3 (paris) and run the command system returns nothing.

So this is the WORKAROUND that I've in my mind to check the availability of service in any particular region. But If i write a bash script for this and run the same command using terraform datasource external it shows an error message that unable to parse a JSON error pasring '<'. I've no clue why bash-script considering this returned XML, though I'm checking the exit code instead.

bash script

function check_efs() {
curl -ls https://elasticfilesystem.us-east-1.amazonsaws.com
if [ $? -eq 0 ]; then
        output=1
else:
        output=0
}

function produce_output() {
value=$(output)

jq -n \
    --arg is_efs_exist "$value" \
    '{"is_efs_exist":$is_efs_exist}'
}

check_efs
produce_output

main.tf

provider external {}

data "external" "this" {
program = ["bash", "${path.module}/scripts/efschecker.sh"]
}