Coder Social home page Coder Social logo

spacelift-migration-kit's Introduction

Spacelift Migration Kit

This repository contains scripts to help you move from various vendors to Spacelift.

There is no one-size-fits-all for this kind of migration so this kit aims at doing the heavy lifting and getting you 90% through. You will likely need to slightly tweak the generated Terraform code to fit your specific context.

Overview

The migration process is as follows:

  • Export the definition for your resources at your current vendor.
  • Generate the Terraform code to recreate similar resources at Spacelift using the Terraform provider.
  • Review and possibly edit the generated Terraform code.
  • Commit the Terraform code to a repository.
  • Create a manager Spacelift stack that points to the repository with the Terraform code.

Supported Source

Currently, only Terraform Cloud/Enterprise is supported as a source.

Prerequisites

  • Terraform

Instructions

Preparation

Use the terraform login spacelift.io command to ensure that Terraform can interact with your Spacelift account.

Depending on the exporter used, you may need additional steps:

  • Terraform Cloud/Enterprise: Use the terraform login command to ensure that Terraform can interact with your Terraform Cloud/Enterprise account.

Pre-Migration Cleanup

In order to start fresh, clean up files and folders from previous runs.

rm -rf ./out ./{exporters/tfc,generator,manager-stack}/.terraform ./{exporters/tfc,generator,manager-stack}/.terraform.lock.hcl ./{exporters/tfc,generator,manager-stack}/terraform.tfstate ./{exporters/tfc,generator,manager-stack}/terraform.tfstate.backup

Export the resource definitions and Terraform state

  • Choose an exporter and copy the example .tfvars file for it into exporter.tfvars.
  • Edit that file to match your context.
  • Run the following commands:
cd exporters/<EXPORTER>
terraform init
terraform apply -auto-approve -var-file=../../exporter.tfvars

A new out folder should have been created. The data.json files contains the mapping of your vendor resources to the equivalent Spacelift resources, and the state-files folder contains the files for the Terraform state of your stacks, if the state export was enabled.

Please note that once exported the Terraform state files can be imported into Spacelift or to any backend supported by Terraform.

Generate the Terraform code

  • If you want to customize the template that generates the Terraform code, run cp ../../generator/generator.tftpl ../generator.tftpl, and edit the generator.tftpl file at the root of the repository. If present, it will be used automatically.
  • Run the following commands:
cd ../../generator
terraform init
terraform apply -auto-approve -var-file=../out/data.json

Review and edit the generated Terraform code

A main.tf should have been generated in the out folder. It contains all the Terraform code for your Spacelift resources.

Mapping resources from a vendor to Spacelift resources is not an exact science. There are gaps in functionality and caveats in the mapping process.

Please carefully review the generated Terraform code and make sure that it looks fine. If it does not, repeat the process with a different configuration or edit the Terraform code.

Commit the Terraform code

When the Terraform code is ready, commit it to a repository.

Create a manager Spacelift stack

⚠️ Check out the VCS system integation If you were using https://docs.spacelift.io/integrations/source-control/github#setting-up-the-custom-application set a correct namespace variable in manager-stack/terraform.tfvars and uncomment github_enterpise block in manager-stack/main.tf file.

It is now time to create a Spacelift stack that will point to the commited Terraform code that manages your Spacelift resources.

  • Copy the example manager-stack.example.tfvars file into manager-stack.tfvars .
  • Edit that file to match your context.
  • Run the following commands:
cd ../manager-stack
terraform init
terraform apply -auto-approve -var-file=../manager-stack.tfvars

After the stack has been created, a tracked run will be triggered automatically. That run will create the defined Spacelift resources.

Post-Migration Cleanup

Before you can use Spacelift to manage your infrastructure, you may need to make changes to the Terraform code for your infrastructure, depending on the Terraform state is managed.

If the Terraform state is managed by Spacelift,perform the following actions, otherwise you can skip this section:

  • Remove any backend/cloud block from the Terraform code that manages your infrastructure to avoid a conflict with Spacelift's backend.
  • Delete the import_state_file arguments from the Terraform code that manages your Spacelift resources.
  • After the manager stack has successfully run, the mounted Terraform state files are not needed anymore and can be deleted by setting the import_state argument to false in the manager-stack.tfvars file and run terraform apply -auto-approve -var-file=../manager-stack.tfvars in the manager-stack folder.

Known Limitations

State Files Bigger than 2MB

The current implementation uses mounted files to upload the exported state files to Spacelift. This works well for most state files but mounted files have a file size limit of 2MB so state files bigger than that cannot be imported that way.

This limitation will be removed in an upcoming release but until then, the workaround is to:

  • Do not export the state files nor import them into Spacelift with the Migration Kit.
  • Set the TFC_TOKEN environment variable with a TFC token as the value. It can be set on the stacks or as a context attached to multiple stacks.
  • Mount the following script as a mounted file named import-tfc-state.sh:
#!/bin/bash
set -euo pipefail

if [[ -z $TFC_TOKEN ]]; then
  echo "TFC_TOKEN is not set"
  exit 1
fi

TFC_WORKSPACE_ID=$1

STATE_DOWNLOAD_URL=$(curl -sSL --fail \
  --header "Authorization: Bearer $TFC_TOKEN" \
  --header "Content-Type: application/vnd.api+json" \
  --request GET \
  "https://app.terraform.io/api/v2/workspaces/${TFC_WORKSPACE_ID}/current-state-version" \
  | jq -r '.data.attributes."hosted-state-download-url"' )

curl -sSL --fail -o state.tfstate "${STATE_DOWNLOAD_URL}"
terraform state push -force state.tfstate
  • Run the following command as task: /mnt/workspace/import-tfc-state.sh <TFC WORKSPACE ID>

Please note that the spacectl CLI tool can be used to run those tasks which is helpful if you need to import many big state files: spacectl stack task --id <STACK ID> --tail /mnt/workspace/import-tfc-state.sh <TFC WORKSPACE ID>

Terraform Cloud/Enterprise Exporter

  • The variable sets are not exposed so they cannot be listed and exported.
  • The name of the Version Control System (VCS) provider for a stack is not returned so it has to be set in the exporter configuration file.
  • When the branch for the stack is the repository default branch, the value is empty. You can set the value for the default branch in the exporter configuration file, or edit the generated Terraform code.

spacelift-migration-kit's People

Contributors

crayfishx avatar flaviuscdinu avatar jmfontaine avatar nikitabarskov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spacelift-migration-kit's Issues

Error mounting state file due to size.

While using the kit to migrate we encountered the following error on several of our state files.
This was from running the apply on the manager-stack

│ Error: could not create stack mounted file: validation failed: file is too large, max size is 2MB

The state files we have failing to mount range anywhere from 1.2M to 7.4M.
I attempted to mount them in the UI and that also failed, which leads me to believe this is an api constraint with mounted files and not one specific to the migration kit.

In any case, 2MB seems like a very tight constraint considering the potential size of a terraform state file. Can that limit be bumped? Can the migration kit use another method?

import-tfc-state.sh curl authorization

Added --header "Authorization: Bearer $TFC_TOKEN" to the second curl command because the script fails with a 401 while running the second curl command:

image

PR here: #16

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.