Terraform output usage and not being detected

When I do terraform apply using bitbucket pipeline.

This is the result:

Apply complete! Resources: 12 added, 0 changed, 1 destroyed.
Outputs:
ecr_repo_uri = "12345678.dkr.ecr.ap-southeast-2.amazonaws.com/ecr-repository"
+ printf '\n'
+ printf '\x1d+ terraform output\n'
+ terraform output

so meaning this was a success?

But the next line of my pipeline will call this command terraform output

but the result is Warning: No outputs found. I am missing something?

+ terraform output
╷
│ Warning: No outputs found
│ 
│ 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.
╵
+ printf '\n'
+ printf '\x1d+ echo \x22ECR_REGISTRY_URI=\x24(terraform output -raw ecr_repo_uri)\x22 >> \x24{BITBUCKET_CLONE_DIR}/env-vars.env\n'
+ terraform output -raw ecr_repo_uri

outputs.tf

output "ecr_repo_uri" {
  value = aws_ecr_repository.ecr-repository.repository_url
}

bitbucket pipeline

        step:
          name: 'Create ECR repository'
          script:
            - set -x
            - . shared.sh
            - load_aws_key "$Environment"
            - install_awscli
            - aws --version
            - touch state_file.txt
            - aws s3 cp s3://terraform-statefile-"$Environment"/"$BITBUCKET_REPO_SLUG"/state_file.txt state_file.txt
            - terraform --version
            - terraform init -backend-config="access_key=$AWS_ACCESS_KEY_ID" -backend-config="secret_key=$AWS_SECRET_ACCESS_KEY"
            - terraform validate
            - terraform plan
            - terraform init
            - terraform refresh
            - terraform apply -input=false -auto-approve -state=state_file.txt
            - TF_VAR=$(terraform output -raw ecr_repo_uri)
            - echo "export ECR_REGISTRY_URI=$TF_VAR" >> ${BITBUCKET_CLONE_DIR}/env-vars.env
            - cat state_file.txt
            - aws s3 cp state_file.txt s3://terraform-statefile-"$Environment"/"$BITBUCKET_REPO_SLUG"/state_file.txt
          artifacts:
            - env-vars.env

I don’t think anyone is going to be able to usefully assist without being able to see your pipeline definition.

thanks. i added my pipeline definition

Ah, I see the problem. You’re doing some weird stuff in your pipeline definition.

When you configure Terraform to use the s3 backend, Terraform itself takes care of talking to S3.

You’re causing problems for yourself by also separately using aws s3 cp and having some terraform commands use the state it manages itself, whilst overriding others to use your manually managed state.

You should delete all the aws s3 cp commands, the -state=state_file.txt option, and all your extra commands mentioning state_file.txt.