Kanji
・ Cloud engineer / freelance ・ Born in 1993 ・ Born in Ehime Prefecture / Lives in Shibuya-ku, Tokyo ・ AWS history 5 years Profile details
Table of Contents
If the Git repository of the Terraform template is split or using Module
If you want to define a custom Data Source, use External Data Source.
external_external | Data Sources | hashicorp/external | Terraform Registry
In order to obtain information on existing resources as Data Source, Program calls scripts such as bash and python, and output JSON format strings as execution results.
Program
# !/bin/bash eval "$(jq -r '@sh "codepipeline_name_prefix=\(.codepipeline_name_prefix)"')" pipelines=$( aws codepipeline \ list-pipelines \ --query "pipelines[?starts_with(name, '"${codepipeline_name_prefix}"')].name" \ --region ap-northeast-1 \ --output json ) # pointt result=$(cat << EOS { "names": $(echo "${pipelines}" | jq '@json') } EOS ) jq -n "$result"
# point
Names
| jq '@json'
# Before the change of points result=$(cat << EOS { "names": $(echo "${pipelines}") } EOS ) # Result variable { "names": [ "test-pipeline01", "test-pipeline02", "test-pipeline03", "test-pipeline04", "test-pipeline05" ] }
% terraform plan data.external.aws_codepipelines: Reading... ╷ │ Error: Unexpected External Program Results │ │ with data.external.aws_codepipelines, │ on test.tf line 2, in data "external" "aws_codepipelines": │ 2: program = ["bash", "${path.module}/ListCodePipelineNames.sh"] │ │ The data source received unexpected results after executing the program. │ │ Program output must be a JSON encoded map of string keys and string values. │ │ If the error is unclear, the output can be viewed by enabling Terraform's logging at TRACE level. │ Terraform documentation on logging: https://www.terraform.io/internals/debugging │ │ Program: /bin/bash │ Result Error: json: cannot unmarshal array into Go value of type string
#Point
# Before the change of points result=$(cat << EOS { "names": $(echo "${pipelines}" | jq '@json') } EOS ) # Result variable { "names": "[\"test-pipeline01\",\"test-pipeline02\",\"test-pipeline03\",\"test-pipeline04\",\"test-pipeline05\"]" }
data "external" "aws_codepipelines" { program = ["bash", "${path.module}/scripts/ListCodePipelineNames.sh"] query = { codepipeline_name_prefix = "test-pipeline0" } } # point output aws_codepipelines_names { value = jsondecode(data.external.aws_codepipelines.result["names"]) }
I created a scripts directory in the same directory as the Terraform template file and refer to the script called listCodepipelinenames.sh .
scripts
listCodepipelinenames.sh
In query , codepipeline_name_prefix can be specified as an argument. If you specify codepipeline_name_prefix`, refer to only the pipeline name starting with the specified character string.
query
codepipeline_name_prefix
If you specify
The point is the output part of # point .Terraform’s function jsondecode () is decoded to the array by referring to the names` key of the output result of External Data Source.
jsondecode
is decoded to the array by referring to the
In practice, when referring to other resources, etc., the same definition is performed, and after converting to an array, repeated processing is implemented.