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 you want to refer to a manually created AWS resource
If the Git repository of the Terraform template is split or using Module
In other Git repositories, use it to refer to AWS resources defined.
For example, the KMS key is managed by a git repository for KMS administrators to manage, and the system side refers to the KMS key using the Data Sroce in the encryption setting of AWS resources.
If you manage all AWS resources in one Git repository, you may want to separate them later, so we recommend that you decide the rules and divide them.
Also, if you use a module to define resources, use Data Sroce when referring to AWS resources generated in Module.
If you want to deploy AWS resources dynamically according to the AWS settings and configuration
If you want to get accounts and region information
If you want to get AMI and image information
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
Since CodePipeline’s Data Source is Incompatic, we will use Shell Script to get information.
First, I will explain the shell script.
# !/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.