RousselTMLEARNING

TP · Terraform: from discovery to expertise

Lab: Remote state, locking, import and refactoring

What you will learn in this TP
  • Configure an S3 backend with locking
  • Import an existing resource
  • Refactor with moved
Mode :

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

A locked remote state and existing resources cleanly taken over.
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 .env file read with ${PASSWORD:?} in the Compose file, or a Docker secret read through a *_FILE variable (for example GF_SECURITY_ADMIN_PASSWORD__FILE for Grafana).

Hands-on #

Guided steps, with their solution.

  1. 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. 2. Declare the backend

    Why? use_lockfile creates a lock file next to the state during each operation.

    Add backend.tf pointing to MinIO and re-initialise, migrating local state.
  3. 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. 4. Rename without recreating

    Why? Without a moved block, renaming a resource would destroy and recreate it.

    Rename aws_s3_bucket.documents to aws_s3_bucket.archives with 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

  • ⏱ Estimated time : ≈ 50 min

Sign in to track your progress and take notes. Sign in

⭐ Rate this lesson

4.0 initial rating from the teaching team

Sign in to rate this lesson →

💬 Questions about this lesson

No questions yet: be the first to ask for help or share your configuration.

Ask a question about this lesson →

Recommended Articles

Infrastructure as Code and GitOps: Terraform, Ansible and Argo CD

Manage infrastructure as code: declarative or imperative, Terraform (providers, state, plan, modu...

CaaS: state of the art and best practices

Container as a Service (CaaS) is a cloud computing model that lets companies manage and deploy ap...

Terraform in CI/CD: pipelines, plan review, OIDC and GitOps

Automate Terraform: plan pipeline on merge requests and apply after approval, posting the plan as...

Fleet Management in Grafana Cloud

Fleet Management in Grafana Cloud: remotely managing a fleet of Grafana Alloy and OpenTelemetry c...

OVHcloud storage: Block Storage, S3-compatible Object Storage and Cold Archive

Store data on OVHcloud: Block Storage volumes (classic, high performance), S3-compatible Object S...

Storage on AWS: S3, EBS, EFS and backup

Store data on AWS: Amazon S3 (buckets, storage classes, versioning, lifecycle, encryption, polici...

AWS best practices: security, reliability, costs and operations

The essential best practices for running AWS in production: multi-account organization, identitie...

Infrastructure as Code on Azure: Bicep, Terraform, Cost Management and Well-Architected

Describe and deploy Azure as code with Bicep (modules, parameters, what-if, pipeline deployments)...

Azure storage: storage accounts, Blob, Files, disks and redundancy

Store data on Azure: storage accounts, Blob (hot, cool, archive tiers, lifecycle), Files, Queue a...

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!