Hi
I see the difference in terraform output command in terraform 0.14.5 vs 0.12.26 .Below is the code
Version.tf
terraform {
required_version = "= 0.14.5"
required_providers {
aws = {
source = "repo.dtcc.com/hashicorp/aws"
version = "4.13.0"
}
null = {
source = "repo.dtcc.com/hashicorp/null"
version = "3.1.1"
}
template = {
source = "repo.dtcc.com/hashicorp/template"
version = "2.2.0"
}
}
}
Bellow is the output.tf
output "id" {
value = module.aurora.id
}
output "cluster_arn" {
value = module.aurora.cluster_arn
}
output "endpoint" {
value = module.aurora.db_endpoint
}
when i run
+ terraform output id
The output variable requested could not be found in the state
file. If you recently added this to your configuration, be
sure to run `terraform apply`, since the state won't be updated
with new output variables until that command is run.
and my pipeline gets failure with this error
when i try to run terraform refresh i see below output
Outputs:
database_name = "asptstd"
master_username = "db_mstr_operator"
port = 5480
replica_identifiers = [
[
[],
],
]
Terraform 0.12.26
In terraform 0.12.26 i see below output but pipeline never gets failed .
+ terraform output id
The state file either has no outputs defined, or all the defined
outputs are empty. Please define an output in your configuration
with the `output` keyword and run `terraform refresh` for it to
become available. If you are using interpolation, please verify
the interpolated value is not empty. You can use the
`terraform console` command to assist.
what was the difference between those two versions of terraform while running terraform output command.
Thanks
Hi @dreamsofravi51,
If I’m understanding you correctly, I think you are asking why terraform output id
produces a success status in this case on v0.12.26 but produces an error status in v0.14.5.
Both of those versions are quite old now so I must admit I don’t recall exactly what changed here but it seems like a bug that terraform output id
in v0.12.16 was returning an error but exiting with a successful status, so I would guess that somebody reported that bug and we fixed it in one of the intermediate versions, so that newer versions of Terraform now correctly return an error status when presenting this error message.
If your goal is to just ignore this error as before then I think you could change your pipeline script to get that effect. You didn’t mention which shell scripting language the pipeline is using but if it’s a Unix-style shell then I think one way to do it would be to write the command in parentheses, causing command to run in a “subshell”:
(terraform output id)
Thanks for the reply . this is the snippet i used to check for the terraform output id.This is on linux.
db_identier_check = sh script: "terraform output id", returnStdout: true
sh "echo $db_identier_check"
if (db_identier_check != "" && db_identier_check != null) {
db_identifier = sh(script: "terraform output id", returnStdout: true).trim()
sh "echo $db_identifier"
yes we want to ignore the error for terraform 14.5 version.
I see the same error in terraform 15.4 version also.
$ terraform output id
╷
│ Error: Output "id" not found
│
│ The output variable requested could not be found in the state file. If you
│ recently added this to your configuration, be sure to run `terraform
│ apply`, since the state won't be updated with new output variables until
│ that command is run.
Hi @dreamsofravi51,
The language your logic is written in here is not familiar to me. Is that Jenkins Pipeline Syntax?
I’ve never written Jenkins Pipeline Syntax and so my ability to help here is limited, but from referring to that documentation I see a section Flow Control which includes the following example of handling a sh
directive whose command returns a non-successful status code:
node {
stage('Example') {
try {
sh 'exit 1'
}
catch (exc) {
echo 'Something failed, I should sound the klaxons!'
throw
}
}
}
I don’t know this language well enough to adapt this example to fit into what you shared, but hopefully you do. I think the general idea would be to wrap that first sh
which runs terraform output id
in a similar try
/catch
construct and then you could either echo
a warning or do nothing at all inside the catch
block in order to ignore the error.
If you’re not sure how to incorporate this, I think it may be best to take this question to a Jenkins forum instead where hopefully the participants will be able to help answer this general question about how to ignore the failure of a sh
directive, which I expect would be a common answer no matter which command the sh
directive is running. (That is, this is not a Terraform-specific problem, but rather a general problem of handling shell command errors in Jenkins.)
syntax is written in groovy.
1.while i run the terraform module does it checks the output id in older versions?
2. i did try to to catch the exception . i see the same error and build fails.
The output variable requested could not be found in the state
file. If you recently added this to your configuration, be
sure to run terraform apply
, since the state won’t be updated
with new output variables until that command is run.