Skip to content

Run (plan & apply)

Valter Silva edited this page Feb 16, 2021 · 1 revision

Run

How-To

Plan

First you need to find the workspace ID of the workspace you want to execute a plan:

tecli  workspace list --organization=foo-org
{
  "ID": "ws-fo3aNuvd8xVXpX7T",
  "Actions": {
    "is-destroyable": false
  },
  "AgentPoolID": "",
  "AllowDestroyPlan": false,
  "AutoApply": false,
  "CanQueueDestroyPlan": false,
  "CreatedAt": "2021-02-01T01:02:28.137Z",
  "Environment": "default",
  "ExecutionMode": "remote",
  "FileTriggersEnabled": false,
  "Locked": false,
  "MigrationEnvironment": "",
  "Name": "terraform-dummy",
  "Operations": true,
  "Permissions": {
    "can-destroy": true,
    "can-force-unlock": true,
    "can-lock": true,
    "can-queue-apply": true,
    "can-queue-destroy": true,
    "can-queue-run": true,
    "can-read-settings": true,
    "can-unlock": true,
    "can-update": true,
    "can-update-variable": true
  },
  "QueueAllRuns": false,
  "SpeculativeEnabled": true,
  "TerraformVersion": "0.14.5",
  "TriggerPrefixes": [],
  "VCSRepo": {
    "branch": "",
    "display-identifier": "valter-silva-au/terraform-dummy",
    "identifier": "valter-silva-au/terraform-dummy",
    "ingress-submodules": false,
    "oauth-token-id": "ot-m4nuCHotgiG4JqgZ"
  },
  "WorkingDirectory": "",
  "AgentPool": null,
  "CurrentRun": null,
  "Organization": {
    "Name": "foo-org",
    "CollaboratorAuthPolicy": "",
    "CostEstimationEnabled": false,
    "CreatedAt": "0001-01-01T00:00:00Z",
    "Email": "",
    "EnterprisePlan": "",
    "ExternalID": "",
    "OwnersTeamSAMLRoleID": "",
    "Permissions": null,
    "SAMLEnabled": false,
    "SessionRemember": 0,
    "SessionTimeout": 0,
    "TrialExpiresAt": "0001-01-01T00:00:00Z",
    "TwoFactorConformant": false
  },
  "SSHKey": null
},

Now you can create a run, which will trigger a plan, copy the "ID" to be used later.

tecli run create --workspace-id=ws-fo3aNuvd8xVXpX7T
{
  "ID": "run-Xd2NgHjNtkpeMf8T",
  "Actions": {
    "is-cancelable": true,
    "is-confirmable": false,
    "is-discardable": false,
    "is-force-cancelable": false
  },
  "CreatedAt": "2021-02-01T01:07:40.172Z",
  "ForceCancelAvailableAt": "0001-01-01T00:00:00Z",
  "HasChanges": false,
  "IsDestroy": false,
  "Message": "Queued manually via the Terraform Cloud API",
  "Permissions": {
    "can-apply": true,
    "can-cancel": true,
    "can-discard": true,
    "can-force-cancel": true,
    "can-force-execute": true
  },
  "PositionInQueue": 0,
  "Source": "tfe-api",
  "Status": "pending",
  "StatusTimestamps": {
    "errored-at": "0001-01-01T00:00:00Z",
    "finished-at": "0001-01-01T00:00:00Z",
    "queued-at": "0001-01-01T00:00:00Z",
    "started-at": "0001-01-01T00:00:00Z",
    "applying-at": "0001-01-01T00:00:00Z",
    "applied-at": "0001-01-01T00:00:00Z",
    "planning-at": "0001-01-01T00:00:00Z",
    "planned-at": "0001-01-01T00:00:00Z",
    "planned-and-finished-at": "0001-01-01T00:00:00Z",
    "plan-queueable-at": "2021-02-01T01:07:40Z"
  },
  "TargetAddrs": null,
  "Apply": {
    "ID": "apply-tozNkRY3o9co4jjd",
    "LogReadURL": "",
    "ResourceAdditions": 0,
    "ResourceChanges": 0,
    "ResourceDestructions": 0,
    "Status": "",
    "StatusTimestamps": null
  },
  "ConfigurationVersion": {
    "ID": "cv-N277fBvfCEPQTuoA",
    "AutoQueueRuns": false,
    "Error": "",
    "ErrorMessage": "",
    "Source": "",
    "Speculative": false,
    "Status": "",
    "StatusTimestamps": null,
    "UploadURL": ""
  },
  "CostEstimate": null,
  "CreatedBy": null,
  "Plan": {
    "ID": "plan-Mu2jHgM6vPRDb1se",
    "HasChanges": false,
    "LogReadURL": "",
    "ResourceAdditions": 0,
    "ResourceChanges": 0,
    "ResourceDestructions": 0,
    "Status": "",
    "StatusTimestamps": null,
    "Exports": null
  },
  "PolicyChecks": null,
  "Workspace": {
    "ID": "ws-fo3aNuvd8xVXpX7T",
    "Actions": null,
    "AgentPoolID": "",
    "AllowDestroyPlan": false,
    "AutoApply": false,
    "CanQueueDestroyPlan": false,
    "CreatedAt": "0001-01-01T00:00:00Z",
    "Environment": "",
    "ExecutionMode": "",
    "FileTriggersEnabled": false,
    "Locked": false,
    "MigrationEnvironment": "",
    "Name": "",
    "Operations": false,
    "Permissions": null,
    "QueueAllRuns": false,
    "SpeculativeEnabled": false,
    "TerraformVersion": "",
    "TriggerPrefixes": null,
    "VCSRepo": null,
    "WorkingDirectory": "",
    "AgentPool": null,
    "CurrentRun": null,
    "Organization": null,
    "SSHKey": null
  }
}

You plan will now be executed.

Read a plan output

From the ID copied from the run mentioned in the step above, you can collect many information, one of them is the Plan.ID.

If you have jq installed, you can get it like this:

tecli run read --id=run-Xd2NgHjNtkpeMf8T | jq .Plan.ID
"plan-Mu2jHgM6vPRDb1se"

If you want, you can use pure grep and awk, instead:

tecli run read --id=run-Xd2NgHjNtkpeMf8T | grep "plan-" | tail -n 1 | awk '{ print $2 }'
"plan-Mu2jHgM6vPRDb1se",

Now, with the plan ID, you can get its logs:

tecli plan logs --id=plan-Mu2jHgM6vPRDb1se
Terraform v0.14.5
Configuring remote state backend...
Initializing Terraform configuration...

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:

Terraform will perform the following actions:

Plan: 0 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + aws_region = "us-east-1"

Apply a plan

Just need to pass the argument apply to the command run and provide the Run.ID:

tecli run apply --id=run-Xd2NgHjNtkpeMf8T
run applied successfully

Read the apply output

Same as before, but now you need the Apply.ID:

tecli run read --id=run-Xd2NgHjNtkpeMf8T | jq .Apply.ID
"apply-tozNkRY3o9co4jjd"

Or:

tecli run read --id=run-Xd2NgHjNtkpeMf8T | grep "apply-" | tail -n 1 | awk '{ print $2 }'
"apply-tozNkRY3o9co4jjd",

Now, to read the plan's logs:

tecli apply logs --id=apply-tozNkRY3o9co4jjd
Terraform v0.14.5
Initializing plugins and modules...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

aws_region = "us-east-1"

Clone this wiki locally