runway.module.terraform module

Terraform module.

runway.module.terraform.gen_workspace_tfvars_files(environment, region)[source]

Generate possible Terraform workspace tfvars filenames.

runway.module.terraform.update_env_vars_with_tf_var_values(os_env_vars, tf_vars)[source]

Return os_env_vars with TF_VAR_ values for each tf_var.

class runway.module.terraform.Terraform(context, path, options=None)[source]

Bases: runway.module.RunwayModule

Terraform Runway Module.

Instantiate class.

Parameters
  • context (Context) – Runway context object.

  • path (Union[str, Path]) – Path to the module.

  • options (Dict[str, Dict[str, Any]]) – Everything in the module definition merged with applicable values from the deployment definition.

auto_tfvars

Decorator for creating cached properties.

A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property. Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76

current_workspace

Decorator for creating cached properties.

A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property. Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76

env_file

Decorator for creating cached properties.

A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property. Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76

property skip

Determine if the module should be skipped.

Returns

To skip, or not to skip, that is the question.

Return type

bool

tfenv

Decorator for creating cached properties.

A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property. Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76

tf_bin

Decorator for creating cached properties.

A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property. Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76

cleanup_dot_terraform()[source]

Remove .terraform excluding the plugins directly.

This step is crucial for allowing Runway to deploy to multiple regions or deploy environments without promping the user for input.

The plugins directory is retained to improve performance when they are used by subsequent runs.

gen_command(command, args_list=None)[source]

Generate Terraform command.

handle_backend()[source]

Handle backend configuration.

This needs to be run before “skip” is assessed or env_file/auto_tfvars is used in case their behavior needs to be altered.

handle_parameters()[source]

Handle parameters.

Either updating environment variables or writing to a file.

terraform_apply()[source]

Execute terraform apply command.

https://www.terraform.io/docs/commands/apply.html

terraform_destroy()[source]

Execute terraform destroy command.

https://www.terraform.io/docs/commands/destroy.html

terraform_get()[source]

Execute terraform get command.

https://www.terraform.io/docs/commands/get.html

terraform_init()[source]

Execute terraform init command.

https://www.terraform.io/docs/commands/init.html

terraform_plan()[source]

Execute terraform plan command.

https://www.terraform.io/docs/commands/plan.html

terraform_workspace_list()[source]

Execute terraform workspace list command.

https://www.terraform.io/docs/commands/workspace/list.html

Returns

The available Terraform workspaces.

Return type

str

terraform_workspace_new(workspace)[source]

Execute terraform workspace new command.

https://www.terraform.io/docs/commands/workspace/new.html

Parameters

workspace (str) – Terraform workspace to create.

terraform_workspace_select(workspace)[source]

Execute terraform workspace select command.

https://www.terraform.io/docs/commands/workspace/select.html

Parameters

workspace (str) – Terraform workspace to select.

terraform_workspace_show()[source]

Execute terraform workspace show command.

https://www.terraform.io/docs/commands/workspace/show.html

Returns

The current Terraform workspace.

Return type

str

run(action)[source]

Run module.

plan()[source]

Run Terraform plan.

deploy()[source]

Run Terraform apply.

destroy()[source]

Run Terraform destroy.

class runway.module.terraform.TerraformOptions(args, backend, workspace, version=None, write_auto_tfvars=False)[source]

Bases: runway.module.ModuleOptions

Module options for Terraform.

Instantiate class.

Parameters
  • args (Union[Dict[str, List[str]], List[str]]) – Arguments to append to Terraform CLI commands. If providing a list, all arguments will be passed to terraform apply only. Can also be provided as a mapping to pass arguments to terraform apply, terraform init, and/or terraform plan.

  • backend (TerraformBackendConfig) – Backend configuration.

  • workspace (str) – Name of the Terraform workspace to use. While it is recommended to let Runway manage this automatically, it has been exposed as an option for cases when a static workspace needs to be used (e.g. remote backend).

  • version (Optional[str]) – Terraform version.

  • write_auto_tfvars (bool) – Optionally write parameters to a tfvars file instead of updating environment variables.

static resolve_version(context, terraform_version=None, **_)[source]

Resolve terraform_version option.

classmethod parse(context, path=None, **kwargs)[source]

Parse the options definition and return an options object.

Parameters
  • context (Context) – Runway context object.

  • path (Optional[Path]) – Path to the module.

Keyword Arguments
  • args (Union[Dict[str, List[str]], List[str]]) – Arguments to append to Terraform CLI commands. If providing a list, all arguments will be passed to terraform apply only. Can also be provided as a mapping to pass arguments to terraform apply, terraform init, and/or terraform plan.

  • terraform_backend_config (Optional[Dict[str, str]]) – Mapping of Terraform backend configuration options.

  • terraform_backend_cfn_outputs (Optional[Dict[str, str]]) – Mapping of Terraform backend configuration options whose values are stored in Cloudformation outputs.

  • terraform_backend_ssm_params (Optional[Dict[str, str]]) – Mapping of Terraform backend configuration options whose values are stored in SSM parameters.

  • terraform_version (Optional[Union[Dict[str, str], str]]) – Version of Terraform to use when processing a module.

  • terraform_workspace (str) – Name of the Terraform workspace to use. While it is recommended to let Runway manage this automatically, it has been exposed as an option for cases when a static workspace may be needed.

  • terraform_write_auto_tfvars (bool) – Optionally write parameters to a tfvars file instead of updating environment variables.

Returns

TerraformOptions

class runway.module.terraform.TerraformBackendConfig(context, config_file=None, **kwargs)[source]

Bases: runway.module.ModuleOptions

Terraform backend configuration module options.

OPTIONS

A list of option names that are parsed by this class.

Type

List[str]

Instantiate class.

See Terraform documentation for the keyword arguments needed for the desired backend.

https://www.terraform.io/docs/backends/types/index.html

OPTIONS = ['terraform_backend_config', 'terraform_backend_cfn_outputs', 'terraform_backend_ssm_params']
init_args

Decorator for creating cached properties.

A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property. Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76

get_full_configuration()[source]

Get full backend configuration.

static resolve_cfn_outputs(client, **kwargs)[source]

Resolve CloudFormation output values.

Parameters

client (CloudformationClient) – Boto3 Cloudformation client.

Keyword Arguments
  • bucket (Optional[str]) – Cloudformation output containing an S3 bucket name.

  • dynamodb_table (Optional[str]) – Cloudformation output containing a DynamoDB table name.

Returns

Resolved values from Cloudformation.

Return type

Dict[str, str]

static resolve_ssm_params(client, **kwargs)[source]

Resolve SSM parameters.

Parameters

client (SSMClient) – Boto3 SSM client.

Keyword Arguments
  • bucket (Optional[str]) – SSM parameter containing an S3 bucket name.

  • dynamodb_table (Optional[str]) – SSM parameter containing a DynamoDB table name.

Returns

Resolved values from SSM.

Return type

Dict[str, str]

static gen_backend_filenames(environment, region)[source]

Generate possible Terraform backend filenames.

Parameters
  • environment (str) – Current deploy environment.

  • region (str) – Current AWS region.

Returns

List of possible file names.

Return type

List[str]

classmethod get_backend_file(path, environment, region)[source]

Determine Terraform backend file.

Parameters
  • path (Path) – Path to the module.

  • environment (str) – Current deploy environment.

  • region (str) – Current AWS region.

Returns

Path to a hcl/tfvars file.

Return type

Optional[Path]

classmethod parse(context, path=None, **kwargs)[source]

Parse backend options and return an options object.

Parameters
  • context (Context) – Runway context object.

  • path (Optional[Path]) – Path to the module.

Keyword Arguments
  • terraform_backend_config (Optional[Dict[str, str]]) – Mapping of Terraform backend configuration options.

  • terraform_backend_cfn_outputs (Optional[Dict[str, str]]) – Mapping of Terraform backend configuration options whose values are stored in Cloudformation outputs.

  • terraform_backend_ssm_params (Optional[Dict[str, str]]) – Mapping of Terraform backend configuration options whose values are stored in SSM parameters.

Returns

TerraformBackendConfig