AWS S3 Bucket Management with Terraform
Objective
The goal is to automatically create a remote development environment, including AWS S3 buckets for storing files and templates, without requiring manual intervention.
Implementation
To achieve this, aws_s3_bucket resources were introduced in the aws Terraform module configuration. A random suffix is appended to the bucket names to facilitate easy destruction and recreation when needed.
Challenges
A key issue with this approach arises when a terraform plan includes bucket destruction. If the bucket contains any files, the plan application fails.
Solution
To mitigate this, the prevent_destroy lifecycle property was set to true. This ensures that if a plan includes bucket destruction, it fails immediately during the planning stage rather than at the application stage.
To avoid the failure, plan targeting can be used. This approach:
- Enables quick environment creation without manual intervention.
- Maintains the bucket as a managed resource in Terraform, allowing continued management of its properties and configurations.
However, if the buckets contain files, manual cleanup is required before deletion.
Alternative Approach Considered
An alternative option was to define the buckets as data sources instead of Terraform-managed resources. However, this approach was not chosen because:
- Environment creation would require manual steps
- Terraform would no longer manage bucket properties
- Configuration drift detection would be lost
- There would be no consistency between environments
By keeping the buckets as Terraform-managed resources, we ensure better automation and maintainability, despite the need for manual cleanup in some cases.