Terraform Apply erroring due to RUNNING state

Hello guys!

We are trying to run a custom preview-environment setup, we have been following the examples from github (here) but we have been having problems running terraform apply. It seems like the provider or the API is not taking into account that resources might have multiple states, I’m not sure what the state on the Terraform provider means exactly but it is definitely causing problems to us, any of the applications/database that we add with the state = "RUNNING" fail with the following error:

│   with qovery_database.preview_environment_redis_database,
│   on qovery.tf line 51, in resource "qovery_database" "preview_environment_redis_database":
│   51: resource "qovery_database" "preview_environment_redis_database" {
│
│ Could not deploy database '479b76a7-437b-44e8-a009-d0938f9cb175',
│ unexpected error: 400 Bad Request - Specified action is not possible in
│ this state: Start Cannot start deployment while environment is in
│ WAITING_RUNNING state

If we make the resources with state = "STOPPED" we dont run into these issues. As we need these apps in the environment to be automatically RUNNING and even keep an order of dependencies like deploy dbdeploy core-app. Is there anything we can do here, or do we need to run Terraform with state = "STOPPED" and then add a custom script on the github-action that triggers the Deploy action of each

So it seems theres currently no way to start directly after Terraform deployment. We ended up using the following script on our github-action

#!/usr/bin/env sh
# use like:
#          trigger_qovery_deployment.sh <environment>
BASE_ENVIRONMENT_ID=$1

echo "Triggering deployment for environment $BASE_ENVIRONMENT_ID  "

set -e

# deploy env
result=$(curl -X POST -H 'Content-type: application/json' -H "Authorization: Token $QOVERY_API_TOKEN" \
    "https://api.qovery.com/environment/$BASE_ENVIRONMENT_ID/deploy")
echo "Result: $result"
timeoutInSeconds=3600
endTime=$(($(date +%s) + timeoutInSeconds))

## wait for successful deployment
while [ "$(date +%s)" -lt $endTime ]; do
    # check deployment status
    # Doc: https://api-doc.qovery.com/#operation/getProjectEnvironmentStatus
    current_state=$(curl -sb -X GET -H 'Content-type: application/json' -H "Authorization: Token $QOVERY_API_TOKEN" \
      "https://api.qovery.com/environment/$BASE_ENVIRONMENT_ID/status" | jq -r .state)

    if [ "RUNNING" = "$current_state" ]; then
      break
    fi

    # shellcheck disable=SC2039
    if [[ "$current_state" =~ ^.*_ERROR$ ]]; then
      echo "deployment error with current state: $(current_state) - connect to https://console.qovery.com" > /dev/stderr
      exit 1
    fi

    printf "environment state: $current_state\n"

    sleep 5 # wait to check again
done

## keep going
exit 0

we borrowed from GitHub Actions | Docs | Qovery

We just left the part that triggers the deployment of the environment. These are the relevants steps on the github-actions

      - name: Build and deploy preview environment
        working-directory: ${{ env.working-directory }}
        if: github.event_name == 'pull_request' && github.event.action != 'closed'
        id: build-deploy-preview-environment
        run: |
          terraform apply -auto-approve -var "branch_name=${{ env.BRANCH_NAME }}" -var "is_production=false"

      - name: Terraform Output
        id: output
        if: github.event_name == 'pull_request' && github.event.action != 'closed'
        working-directory: ${{ env.working-directory }}
        run: |
          terraform output -no-color -json > output.json
        continue-on-error: true

      - name: Trigger Qovery deployment
        working-directory: ${{ env.working-directory }}
        if: github.event_name == 'pull_request' && github.event.action != 'closed'
        id: trigger-backend-deployment
        env:
          QOVERY_API_TOKEN: ${{ secrets.QOVERY_API_TOKEN }}
        run: |
          ./scripts/trigger_qovery_deployment.sh $(cat output.json | jq -r '.qovery_preview_environment_id.value')
        shell: bash