Skip to content

Commit af32e56

Browse files
EarthmanTalexmolev
andauthored
Terraform Migrate State Docs (#2534)
Co-authored-by: alexmolev <amolev@gmail.com>
1 parent 955471f commit af32e56

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

  • content/working_with/official_plugins/Orchestration

content/working_with/official_plugins/Orchestration/terraform.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,29 @@ The following example can be used as a parameter file to the execute operation c
407407
decision: "terraform/deny"
408408
```
409409

410+
411+
* `terraform.migrate_state`: Migrate Terraform state storage from a local statefile to a Cloud storage option, such as S3 or Azure Storage Account. The operation accepts two parameters: `backend` and `backend_config`. The `backend` parameter is a dict with two static keys, `name` and `options`. The `name` value is the name of the supported backend provider, such as `s3` or `azurerm`. The `options` value is a dict that contains the backend configuration. The `backend_config` is a dict of key:value's that will be used in the `terraform init -migrate-state` command as appended `-backend-confg` flags. This is useful for injecting credentials into the runtime, for example, the below example will result in the command: `terraform init -no-color -backend-config="bucket=tfstates" -backend-config="key=test" -backend-config="region=us-east-2" -backend-config="access_key=......" -backend-config="secret_key=....." -migrate-state`. Both the `backend` parameter and the `backend_config` parameters are necessary.
412+
413+
```yaml
414+
operation: terraform.migrate_state
415+
operation_kwargs:
416+
backend:
417+
name: s3
418+
options:
419+
bucket: tfstates
420+
key: test
421+
region: var.aws_region
422+
access_key: var.access_key
423+
secret_key: var.secret_key
424+
backend_config:
425+
bucket: tfstates
426+
key: test
427+
region: us-east-2
428+
access_key: { get_secret: aws_access_key_id }
429+
secret_key: { get_secret: aws_secret_access_key }
430+
allow_kwargs_override: true
431+
```
432+
410433
### **Runtime Properties**
411434

412435
* `state`: Saves the state of the resources created in the format { "resource_name" : <resource state> },
@@ -620,6 +643,46 @@ Executing workflow `run_infracost` on deployment `tf` [timeout=900 seconds]
620643

621644
Workflow outputs are saved in `plain_text_infracost` and `infracost` runtime properties.
622645

646+
647+
## migrate_state
648+
649+
You can migrate from local Terraform state file to a hosted storage state, such as S3 or Azure storage account, with the `migrate_state` workflow. This command exposes the functionality of `terraform init -migrate-state`. This workflow wraps the `terraform.migrate` interface for the `cloudify.nodes.terraform.Module` node type.
650+
651+
That operation accepts two parameters:
652+
- `backend`: A dict that contains `name` and `options` keys. More information follows the `backend_config` definition.
653+
- `backend_config`: a dict of key:value's that will be used in the `terraform init -migrate-state` command as appended `-backend-confg` flags. This is useful for injecting credentials into the runtime, for example, the below example will result in the command: `terraform init -no-color -backend-config="bucket=foo" -backend-config="key=bar" -backend-config="region=us-east-2" -backend-config="access_key=......" -backend-config="secret_key=....." -migrate-state`. Both the `backend` parameter and the `backend_config` parameters are necessary.
654+
655+
The `backend` parameter is a dict with two static keys:
656+
- `name`: the name of the supported backend provider, such as `s3` or `azurerm`.
657+
- `options`: a dict that contains the backend configuration.
658+
659+
You can invoke the `migrate_state` workflow from the CLI like this, using a YAML file describing the required parameters:
660+
661+
```bash
662+
cfy executions start migrate_state -d [DEPLOYMENT_ID] -p migrate-state-params.yaml
663+
```
664+
665+
Example `migrate-state-params.yaml` file:
666+
667+
```yaml
668+
node_ids:
669+
- cloud_resources
670+
backend:
671+
name: s3
672+
options:
673+
bucket: foo
674+
key: bar
675+
region: var.aws_region
676+
access_key: var.access_key
677+
secret_key: var.secret_key
678+
backend_config:
679+
bucket: foo
680+
key: bar
681+
region: us-east-2
682+
access_key: { get_secret: aws_access_key_id }
683+
secret_key: { get_secret: aws_secret_access_key }
684+
```
685+
623686
# Terraform Outputs
624687
625688
You can expose outputs from your Terraform template to the node instance runtime properties.
@@ -677,3 +740,4 @@ The plugin executes `terraform plan` to gather the list of resources of the curr
677740
It then calls `terraform refresh` in order to pull the remote state.
678741
Finally, it executes `terraform show state` for each resource.
679742
An "OK" return value indicates that all resources exist. A "not OK" value indicates that the resource does not exist.
743+

0 commit comments

Comments
 (0)