What you will learn in this TP
- Configure an S3 backend with locking
- Import an existing resource
- Refactor with moved
All steps and instructions are shown. Recommended for this lab: semi-guided
Introduction #
Context. The team adopts Terraform while resources already exist. You set up shared state and take over the existing resources without breaking anything.
Prerequisites: Terraform 1.10 or later, Docker, LocalStack and terraform-local (pip install localstack terraform-local); no cloud account or charges · Estimated time: 50 min
Disposable environment: passwords are written in clear text to keep the lab simple. Outside a lab, keep image versions pinned and move secrets out of the file: an unversioned.envfile read with${PASSWORD:?}in the Compose file, or a Docker secret read through a*_FILEvariable (for exampleGF_SECURITY_ADMIN_PASSWORD__FILEfor Grafana).
Hands-on #
Guided steps, with their solution.
1. Prepare the backend
Why? The state bucket is versioned so a previous version can be restored.
Start MinIO and create a versioned bucket for state.2. Declare the backend
Why? use_lockfile creates a lock file next to the state during each operation.
Addbackend.tfpointing to MinIO and re-initialise, migrating local state.3. Import existing resources
Why? The import block is reviewed in merge requests like the rest of the code.
Create a bucket "by hand", generate its code with an import block then apply.4. Rename without recreating
Why? Without a moved block, renaming a resource would destroy and recreate it.
Renameaws_s3_bucket.documentstoaws_s3_bucket.archiveswith a moved block and check the plan destroys nothing.
Validate your work
Check that each goal above is reached and that you get the result described in the introduction.
Difficulty level : ●●●○○ (3/5)
My progress
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
Terraform state: remote backends, locking, import and refactoring
Manage Terraform state: the role of the state file, sensitive data, remote backends (S3, azurerm,...
Terraform best practices: structure, state, security, review and maintenance
Terraform best practices for teams: repository structure, naming, pinned versions, encrypted and ...
Terraform multi-environment, workspaces and repository organization
Organize Terraform for several environments: one folder per environment, tfvars files, CLI worksp...
Terraform: principles, architecture and workflow
Understand Terraform: declarative infrastructure as code, architecture (core, providers, state), ...
The HCL language: resources, variables, outputs, data and expressions
Writing Terraform: terraform, provider, resource and data blocks, typed variables with validation...
Testing and securing Terraform: validate, terraform test, tflint, Checkov and policies
Ensure the quality of Terraform code: fmt and validate, native terraform test tests with mocked p...
Terraform modules: designing, versioning and reusing
Structure Terraform with modules: root module and child modules, inputs and outputs, sources (loc...
Preparing for the HashiCorp Certified: Terraform Associate certification
Preparation guide for the Terraform Associate: format, official objectives (IaC concepts, workflo...
Terraform across multiple clouds: AWS, Azure, Google Cloud, OVHcloud and Kubernetes
Use Terraform with the main providers: aws, azurerm, google, ovh and openstack, kubernetes and he...