Skip to main content
⏳ Estimated read time: 4 min read

stackgen import state

The stackgen import state command will parse a Terraform state file and import the infrastructure configuration into an appstack or a new version of an existing appstack.

Usage

stackgen import state [flags]

Flags

FlagDescriptionDefault
--cloud-provider stringCloud provider for the infrastructureaws
-m, --mode stringImport strategy: replace or mergereplace
-h, --helpHelp for import state.n/a

Global flags (with this command)

FlagDescription
-f, --file stringPath to the Terraform state file (required).
--appstack-id stringAppstack ID to import into an existing appstack (creates a new appstack version).
--appstack-name stringAppstack name to create if importing into a new appstack (must be unique).

See Global flags for --project, --output, --interactive, --log, --stackgen-home, and other globals.

Usage Guide

Use stackgen import state to:

Prerequisites

Click to view

Before running this command, ensure you have:

  • StackGen CLI v0.52.0 or higher installed.

  • To install StackGen CLI, run the command:

    brew install stackgenhq/stackgen/stackgen
  • If you are creating a new appStack, you need: A valid Terraform-generated state file (.tfstate).

  • If you are creating an appStack version, you will need:

    • An updated Terraform state file (.tfstate)
    • appStack ID for an existing appStack in StackGen.
  • Configure your StackGen environment by running:

    export STACKGEN_TOKEN=<TOKEN>

    Replace <TOKEN> with your personal access token (PAT). Skip this step if you have already configured your environment.

  • If you are creating an appStack inside a project workspace (not the default personal workspace):

    1. Select the project workspace from the Projects dropdown.

    2. From the browser address bar, copy the Project ID.

    3. Run:

      export STACKGEN_PROJECT=<Project ID>

    You can also pass --project <Project ID> instead of the environment variable.

  • This feature is supported in the following cloud providers:

    • AWS
    • Azure
    • GCP

Create New appStack

Click to view

Use this command to create a new appStack in StackGen by importing an existing Terraform state file. This is especially useful when you want to bring your current infrastructure under StackGen's visibility without manually recreating configurations.

Once imported, StackGen will analyze the resources in the state file, generate the corresponding appStack definition, and make it available for further compliance checks, versioning, and deployment actions.

important

Please read the Prerequisites section before running the command.

Usage

Create a new appStack in StackGen by importing resources from a .tfstate file.

stackgen import state -f <path/to/.tfstate> --cloud-provider=<provider name>

Flags (this flow)

FlagDescriptionRequired
-f, --filePath to the Terraform state fileYes
--cloud-providerCloud provider (for example AWS, Azure, GCP)Yes
-m, --modereplace or merge (default replace)No

Example:

Import resources from the .tfstate file and create a new appStack for an Azure-based infrastructure.

stackgen import state -f /Users/abc/Downloads/terraform/terraform.tfstate --cloud-provider=azure

Create New Version of an Existing appStack

Click to view

Use this command to create a new version of an existing appStack by importing an updated Terraform state file. This helps keep your StackGen environment in sync with infrastructure changes and re-evaluating compliance after each iteration.

Usage

Create a new version of the specified appStack using the updated tfstate file.

stackgen import state --appstack-id=<your-appstack-id> -f <path/to/terraform.tfstate> --cloud-provider=<provider name>

Flags (this flow)

FlagDescriptionRequired
--appstack-idID of the existing appStack to create a new version forYes
-f, --filePath to the Terraform state fileYes
--cloud-providerCloud provider (for example AWS, Azure, GCP)Yes
-m, --modereplace or merge (default replace)No

Example:

Create a new version of the appStack with ID app-1234abcd5678efgh using the latest Terraform state file for an AWS infrastructure.

stackgen import state --appstack-id=app-1234abcd5678efgh -f /Users/abc/Downloads/terraform/terraform.tfstate --cloud-provider=aws