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
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...