Should you’ve ever adjusted Amazon DynamoDB international secondary index capability (GSI) outdoors Terraform, you understand how Terraform detects drift and forces undesirable reverts. With Terraform’s new aws_dynamodb_global_secondary_index useful resource, you possibly can tackle this downside.
The brand new aws_dynamodb_global_secondary_index useful resource treats every GSI as an unbiased useful resource with its personal lifecycle administration. You should use this function to make capability changes for GSI and tables outdoors of Terraform.
On this put up, I display easy methods to use Terraform’s new aws_dynamodb_global_secondary_index useful resource to handle GSI drift selectively. I stroll you thru the restrictions of present approaches and information you thru implementing the answer.
The issue: Terraform drift and GSI administration
Earlier than diving into the answer, let’s set up what drift means in infrastructure administration. In infrastructure as code (IaC), drift happens when the precise state of your infrastructure differs from what’s outlined in your Terraform configuration. Terraform detects drift by evaluating the specified state (your .tf configuration information), the final identified state (saved in terraform.tfstate), and the precise state (queried from AWS). When these don’t match, Terraform reviews drift and proposes adjustments to reconcile the distinction.
DynamoDB GSIs typically require capability changes for varied operational causes: load testing, capability planning, emergency efficiency necessities, or managing heat throughput. Your DynamoDB capability will also be modified by autoscaling occasions. Everytime you make these adjustments outdoors of Terraform, it creates drift between Terraform’s configuration and AWS actuality.
For instance, let’s assume your analytics crew runs a each day report that queries a GSI closely. The report runs at 2:00 AM and desires 50 learn capability models (RCUs), however throughout regular hours, 5 RCUs is adequate. Your operations crew manually will increase capability earlier than the report runs to deal with the load.
At 1:50 AM, your ops crew will increase capability from 5 to 50 utilizing AWS Command Line Interface (AWS CLI). The report runs from 2:00 AM to three:00 AM with the upper capability. Later that day, if you run terraform plan to deploy an unrelated change, Terraform detects drift as a result of the precise capability (50) doesn’t match your configuration (5). Terraform desires to revert the capability again to five, which might intrude along with your operational capability administration.
The widespread workaround and its limitations
A standard workaround is to make use of ignore_changes = [global_secondary_index] in your desk’s lifecycle block. This prevents Terraform from detecting capability drift. Nevertheless, this method is simply too broad—it ignores all GSI adjustments, not simply capability. As a result of global_secondary_index is a fancy nested sort, ignore_changes solely works on the top-level, not at particular person attributes. If somebody unintentionally deletes a GSI or modifies its key schema, Terraform gained’t detect it. You’ll be able to’t distinguish between intentional capability tuning and unintentional GSI deletions.
The answer: Separate GSI sources
The brand new aws_dynamodb_global_secondary_index useful resource treats every GSI as an unbiased useful resource with its personal lifecycle administration. This offers you granular management over which attributes to disregard for every GSI whereas nonetheless detecting vital adjustments like deletions or schema modifications.
Stipulations
Earlier than you start, confirm you have got:
The aws_dynamodb_global_secondary_index useful resource is at the moment marked as experimental within the Terraform AWS supplier. This implies the schema or habits would possibly change with out discover, and isn’t topic to the backwards compatibility assure of the supplier.
You should set the atmosphere variable TF_AWS_EXPERIMENT_dynamodb_global_secondary_index to allow this experimental useful resource. With out this atmosphere variable, Terraform will return an error when trying to make use of aws_dynamodb_global_secondary_index. Set it earlier than working any Terraform instructions:
Take a look at totally in non-production environments earlier than utilizing in manufacturing. You might be welcome to offer suggestions at GitHub Challenge #45640.
Should you’re upgrading from AWS Supplier v5.x to v6.x, overview the v6.0.0 improve information for breaking adjustments earlier than continuing.
Set up Terraform on Amazon Linux:
Utilizing the brand new useful resource
Create a provisioned capability desk with two GSIs utilizing the brand new separate useful resource methodology. You’ll create major.tf the place the desk and GSIs are outlined as unbiased sources.
Desk and GSI keys:
| Useful resource | Hash key | Vary key | Capability |
|---|---|---|---|
| Desk | id | timestamp | 5/5 |
| StatusUserIndex | standing | user_id | 5/5 |
| TimestampIndex | timestamp | – | 3/3 |
Configuration:
Deploy the sources:
Take a look at selective ignore_changes by manually altering the capability of StatusUserIndex (the one with ignore_changes):
Operating terraform plan reveals No adjustments regardless that StatusUserIndex capability modified to 10/10 in AWS. This happens due to ignore_changes = [provisioned_throughput].
Confirm drift detection nonetheless works by manually altering TimestampIndex (the one with out ignore_changes):
Operating terraform plan detects the drift and proposes to alter TimestampIndex capability from 8 again to three. This demonstrates that:
StatusUserIndexreveals no adjustments (capability ignored as meant)TimestampIndexreveals drift detection (capability adjustments detected)- Every GSI has unbiased lifecycle administration
- You’ll be able to selectively ignore particular attributes per GSI
- Terraform nonetheless detects vital adjustments on GSIs with out
ignore_changes.
The important thing variations from the normal methodology are that the desk defines attributes utilized by the desk itself (id, timestamp), whereas GSI-specific attributes (standing, user_id) are outlined within the separate GSI useful resource’s key_schema blocks with their attribute_type (required in new useful resource). If a GSI reuses a desk attribute, that attribute stays within the desk’s attribute block. The GSI is a separate useful resource with its personal lifecycle.
Advantages of the brand new useful resource
The brand new useful resource mannequin supplies a number of benefits. Now you can ignore particular attributes of a GSI with out affecting different GSIs and automatic scripts can modify capability primarily based on visitors patterns with out creating Terraform drift. You continue to observe vital adjustments like key schema modifications, confirming no unintentional GSI deletions or reconfigurations. Terraform state stays the supply of reality for GSI construction, whereas DynamoDB APIs present precise runtime capability.
Every GSI can have its personal lifecycle guidelines, offering unbiased administration. The brand new useful resource mannequin follows Terraform finest practices the place every useful resource manages one logical infrastructure part, dependencies are specific by means of useful resource references, and state administration is extra easy.
The brand new useful resource totally helps heat throughput configuration for on-demand tables. Heat throughput is a DynamoDB functionality that you should use to specify baseline capability for on-demand tables, serving to you handle efficiency and prices extra predictably. That is how one can check it.
Create ondemand.tf:
Deploy and check:
Terraform reveals No adjustments as a result of heat throughput adjustments are ignored as anticipated.
Earlier than transferring to the following part, destroy the on-demand check sources:terraform destroy
Migration instance
Now that you simply’ve seen how the brand new useful resource works, let’s stroll by means of a whole hands-on migration of present infrastructure. Begin with a desk utilizing the normal nested GSI method, then migrate it to the brand new separate useful resource methodology with none downtime.
Step 1: Create infrastructure with conventional methodology
Create a DynamoDB desk with a GSI utilizing the normal nested block method.
Create a file referred to as migration-old.tf:
Deploy this infrastructure:
Confirm the desk and GSI had been created:
Output:
Step 2: Put together for migration
Earlier than migrating, backup your Terraform state:
Set the required atmosphere variable:
Step 3: Replace your Terraform configuration
Create a brand new file referred to as migration-new.tf with the up to date configuration. Maintain each information for now—you’ll take away the outdated one after import.
Step 4: Take away the outdated configuration
Now take away or rename the outdated file:
At this level, in the event you run terraform plan, you’ll see that Terraform desires to take away the GSI from the desk (as a result of the nested block is gone) and create a brand new separate GSI useful resource.
Don’t apply but. This is able to trigger downtime. As an alternative, import the prevailing GSI.
Step 5: Import the prevailing GSI
Import the prevailing GSI into the brand new useful resource’s state:
Output:
Step 6: Confirm the migration
Run terraform plan to confirm:
Anticipated output:
Should you see No adjustments, the migration was profitable. The GSI is now managed as a separate useful resource.
Migration abstract
To finish a migration, you began with a conventional nested GSI configuration, which you then migrated to separate GSI sources with out downtime utilizing terraform import. You then verified the migration with terraform plan displaying No adjustments, after which you efficiently transitioned to the brand new useful resource mannequin.
Key takeaways:
- Migration makes use of
terraform import - No AWS sources are modified or recreated
- The GSI continues to exist all through the migration with zero downtime
- After migration, you have got granular management over what to disregard with
ignore_changes - The migration course of is protected and reversible
Migration concerns
Don’t mix aws_dynamodb_global_secondary_index sources with global_secondary_index blocks on aws_dynamodb_table. Doing so would possibly trigger conflicts, perpetual variations, and GSIs being overwritten.
When migrating, observe these steps:
- Backup state:
terraform state pull > backup.tfstate - Set atmosphere variable:
export TF_AWS_EXPERIMENT_dynamodb_global_secondary_index=1 - Replace configuration: Take away the GSI block from desk the desk and create a brand new GSI useful resource
- Import present GSI:
terraform import'table_name,index_name' - Confirm: Run
terraform plan, it ought to presentNo adjustments - Take a look at: Manually change capability and confirm that Terraform ignores the change
You gained’t expertise downtime throughout migration if completed appropriately utilizing terraform import. The GSI continues to exist in AWS all through the migration. The terraform import command solely updates Terraform’s state file—it doesn’t modify AWS sources.
In case your desk has a number of GSIs, migrate them one after the other:
- Import the primary GSI and confirm with
terraform plan - Import the second GSI and confirm with
terraform plan - Proceed till all GSIs are migrated
This reduces threat and simplifies troubleshooting.
Comparability: Conventional in comparison with new methodology
The next desk summarizes the important thing variations between the normal nested block method and the brand new separate useful resource methodology:
| Facet | Conventional methodology (nested block) | New methodology (separate useful resource) |
|---|---|---|
| Useful resource enablement | No atmosphere variable wanted | Requires TF_AWS_EXPERIMENT_dynamodb_global_secondary_index=1 |
Granular ignore_changes |
Not supported | Supported |
| Impartial GSI administration | All GSIs managed collectively | Every GSI managed independently |
| Drift detection | All-or-nothing | Selective per GSI |
| Lifecycle guidelines | Applies to all GSIs | Per-GSI lifecycle guidelines |
| State administration | Advanced nested state | Easy flat state |
| Capability configuration | Prime-level attributes (read_capacity, write_capacity) |
Block syntax (provisioned_throughput block) |
| Projection configuration | Prime-level attribute (projection_type) |
Block syntax (projection block) |
| Heat throughput help | Restricted | Full help (attribute syntax: warm_throughput = { }) |
| Migration complexity | N/A | Requires import course of |
| Backward compatibility | Current methodology | Can’t combine with conventional methodology |
| Stability | Steady | Experimental (schema would possibly change) |
Clear up
To keep away from incurring future fees, delete the sources you created on this walkthrough:
Should you created any sources manually throughout testing, ensure to delete these as nicely by means of the AWS Administration Console or AWS CLI to keep away from incurring future prices.
Conclusion
On this put up, I confirmed you ways the brand new aws_dynamodb_global_secondary_index useful resource solves the long-standing problem of managing DynamoDB GSI drift in Terraform. The all-or-nothing nature of ignoring nested global_secondary_index blocks created a niche between operational flexibility and infrastructure governance.
By treating GSIs as first-class sources, you achieve granular management with selective ignore_changes for particular GSI attributes, unbiased administration the place every GSI has its personal lifecycle guidelines, higher drift detection that tracks vital adjustments whereas permitting operational changes, and a extra easy structure with separation of considerations between desk and index configuration.
Keep in mind that the aws_dynamodb_global_secondary_index useful resource is at the moment marked as experimental. Whereas it supplies highly effective capabilities for managing GSI drift, bear in mind that:
- The schema or habits would possibly change in future supplier variations
- You should set the atmosphere variable
TF_AWS_EXPERIMENT_dynamodb_global_secondary_index=1to allow this useful resource - It’s not topic to the backwards compatibility assure of the supplier
- You’ll be able to’t combine this useful resource with conventional
global_secondary_indexblocks on the identical desk
All the time check totally in non-production environments and monitor supplier launch notes for updates. When you’ve got suggestions, present it at GitHub Challenge #45640 to assist form the way forward for this function.
In regards to the authors
