Terraform Assignment– 5
Advanced Terraform Features
Basic Questions
- Initialize workspaces and create three environments named dev, stage, and prod.
- List all workspaces and select the dev workspace.
- Output the active workspace using terraform.workspace in an output block.
- Prefix a resource name with the current workspace (e.g., “${terraform.workspace}-sg”).
- Run plan separately in dev, stage, and prod and save each plan to a file.
- Apply a trivial resource in dev only, then destroy it using the same workspace.
- Create a string variable and print its length using the length() function.
- Use lookup() to safely read a value from a map variable with a default.
- Concatenate two lists using concat() and print the result as an output.
- Join a list of CIDRs into a comma-separated string using join().
- Split a comma-separated string into a list using split() and print index 0.
- Create a simple dynamic block to render multiple tags on a resource from a map.
- Create two similar resources using for_each over a set of names.
- Add a local-exec provisioner that writes “post-apply” to a local file.
- Add a remote-exec provisioner that runs echo hello on a test instance.
- Enable detailed logs by setting TF_LOG=DEBUG and run terraform plan.
- Use terraform console to evaluate an expression using contains() and upper().
- Format and validate the configuration using terraform fmt and terraform validate.
- Add depends_on to enforce creation order between two resources.
- Configure a basic multi-environment variable pattern with dev.tfvars, stage.tfvars, and prod.tfvars.
Intermediate Questions
- Configure a remote backend key that includes the workspace name (e.g., state/${terraform.workspace}/tfstate).
- For each workspace (dev, stage, prod), run plan with the matching -var-file.
- Use for_each on a map of subnets to create multiple subnet resources with tags.
- Use a dynamic block to generate multiple ingress rules from a list of objects.
- Build a map of common tags with merge() and apply it to all resources.
- Use coalesce() to fall back to a default AMI when a variable is empty.
- Use a conditional expression to choose instance size by environment.
- Replace a remote-exec bootstrap step with user data/cloud-init to avoid provisioners.
- Replace a local-exec artifact copy with a native Terraform resource/data source.
- Add variable validation (e.g., regex for environment names dev|stage|prod).
- Add a precondition check to ensure a CIDR block is within 10.0.0.0/8.
- Add a postcondition that asserts an output (e.g., subnet count > 0).
- Create a module call that passes different inputs per workspace using a lookup table.
- Compare count vs for_each by creating resources with both and documenting the differences in code comments.
- Use try() to handle a missing attribute and provide a safe default.
- Use keys() and values() to iterate a map and output a formatted string list.
- Generate a minimal dependency graph using terraform graph and save to a file.
- Simulate an error (invalid AMI) and capture diagnostics using TF_LOG and -json plan output.
- Add lifecycle { prevent_destroy = true } to a critical resource and test a destroy plan.
- Create a makefile or script to automate init → workspace select → plan → apply for dev.
Advanced Questions
- Implement a complete multi-environment stack where all resource names, tags, and backend keys are derived from terraform.workspace.
- Build a reusable module that accepts a map of environments and returns structured outputs; consume it from dev, stage, and prod.
- Create nested dynamic blocks (e.g., listeners → rules) driven by a list-of-maps variable.
- Use for expressions to transform a complex object variable into a flattened list for a resource argument.
- Compose functions (format(), replace(), trimsuffix(), coalescelist()) to compute image IDs and names per environment.
- Add robust input validation with multiple validation blocks and clear error messages for wrong types or ranges.
- Integrate a minimal smoke test: apply in dev, output a URL/IP, and verify reachability via a null_resource with local-exec curl (document why to avoid it in production).
- Introduce error-resilient rollout: use create_before_destroy and ignore_changes where appropriate, and demonstrate with a blue/green variable flag.
- Implement workspace-aware logging: send plan/apply logs to logs/${terraform.workspace}/YYYYMMDD-HHMM.txt.
- Deliver a hands-on multi-environment solution: init backend → create/select workspaces → apply dev with dev.tfvars → promote to stage and prod via the same config using only different workspaces and variable files.