terraform_backend

package
v1.185.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

README

Extending Support for a New Terraform Backend in the !terraform.state YAML Function

To enable a new Terraform backend type for use with the !terraform.state YAML function, follow these steps:

1. Implement the Backend Reader Function

Define a new function following the naming convention ReadTerraformBackend<BackendType>. This function is responsible for retrieving the raw Terraform state from the specified backend.

Function signature:

  func ReadTerraformBackend<BackendType>(atmosConfig *schema.AtmosConfiguration, componentSections *map[string]any) ([]byte, error)

Function behavior:

  • If the state file exists, return its contents as a []byte.
  • If the state file does not exist (e.g., in the case when the component has not been provisioned yet), return nil and no error.

2. Register the Backend Reader

In terraform_backend_registry.go, register your backend reader implementation by mapping it to the corresponding backend type in the terraformBackends registry:

  func RegisterTerraformBackends() {
    terraformBackends[cfg.BackendTypeLocal] = ReadTerraformBackendLocal
    terraformBackends[cfg.BackendTypeS3] = ReadTerraformBackendS3
    
    // Register your new backend implementation here
    terraformBackends[cfg.BackendType<BackendType>] = ReadTerraformBackend<BackendType>
  }

3. Update Documentation

Update the corresponding documentation at:

website/docs/core-concepts/stacks/yaml-functions/terraform.state.mdx

Include usage details and any backend-specific requirements or limitations.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBackendAttribute

func GetBackendAttribute(section *map[string]any, attribute string) string

GetBackendAttribute returns an attribute from a section in the backend.

func GetComponentBackend

func GetComponentBackend(sections *map[string]any) map[string]any

GetComponentBackend returns the `backend` section for a component in a stack.

func GetComponentBackendType

func GetComponentBackendType(sections *map[string]any) string

GetComponentBackendType returns the `backend_type` section for a component in a stack.

func GetS3BackendAssumeRoleArn

func GetS3BackendAssumeRoleArn(backend *map[string]any) string

GetS3BackendAssumeRoleArn returns the s3 backend role ARN from the S3 backend config. https://developer.hashicorp.com/terraform/language/backend/s3#assume-role-configuration

func GetTerraformBackend

func GetTerraformBackend(
	atmosConfig *schema.AtmosConfiguration,
	componentSections *map[string]any,
) (map[string]any, error)

GetTerraformBackend reads and processes the Terraform state file from the configured backend.

func GetTerraformBackendReadFunc

func GetTerraformBackendReadFunc(backendType string) func(*schema.AtmosConfiguration, *map[string]any) ([]byte, error)

GetTerraformBackendReadFunc accepts a backend type and returns a function to read the state file from the backend.

func GetTerraformBackendVariable

func GetTerraformBackendVariable(
	atmosConfig *schema.AtmosConfiguration,
	values map[string]any,
	variable string,
) (any, error)

GetTerraformBackendVariable returns the output from the configured backend.

func GetTerraformComponent

func GetTerraformComponent(sections *map[string]any) string

GetTerraformComponent returns the `component` section for a component in a stack.

func GetTerraformWorkspace

func GetTerraformWorkspace(sections *map[string]any) string

GetTerraformWorkspace returns the `workspace` section for a component in a stack.

func ProcessTerraformStateFile

func ProcessTerraformStateFile(data []byte) (map[string]any, error)

ProcessTerraformStateFile processes a Terraform state file.

func ReadTerraformBackendLocal

func ReadTerraformBackendLocal(
	atmosConfig *schema.AtmosConfiguration,
	componentSections *map[string]any,
) ([]byte, error)

ReadTerraformBackendLocal reads the Terraform state file from the local backend. If the state file does not exist, the function returns `nil`.

func ReadTerraformBackendS3

func ReadTerraformBackendS3(
	_ *schema.AtmosConfiguration,
	componentSections *map[string]any,
) ([]byte, error)

ReadTerraformBackendS3 reads the Terraform state file from the configured S3 backend. If the state file does not exist in the bucket, the function returns `nil`.

func ReadTerraformBackendS3Internal

func ReadTerraformBackendS3Internal(
	s3Client S3API,
	componentSections *map[string]any,
	backend *map[string]any,
) ([]byte, error)

ReadTerraformBackendS3Internal accepts an S3 client and reads the Terraform state file from the configured S3 backend.

func RegisterTerraformBackends

func RegisterTerraformBackends()

RegisterTerraformBackends registers Terraform backends.

Types

type RawTerraformState

type RawTerraformState struct {
	Version          int    `json:"version"`           // Internal format version
	TerraformVersion string `json:"terraform_version"` // CLI version used
	Outputs          map[string]struct {
		Value any `json:"value"` // Can be any JSON type
		Type  any `json:"type"`  // HCL type representation
	} `json:"outputs"`
	Resources interface{} `json:"resources,omitempty"`
}

RawTerraformState represents a raw Terraform state file.

type ReadTerraformBackendFunc

type ReadTerraformBackendFunc func(*schema.AtmosConfiguration, *map[string]any) ([]byte, error)

ReadTerraformBackendFunc defines a function type to read Terraform state from a backend.

type S3API

type S3API interface {
	GetObject(ctx context.Context, input *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
}

S3API defines an interface for interacting with S3, including retrieving objects with context and configuration options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL