Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+301 -356
View File
@@ -1,367 +1,312 @@
---
description: This rule file provides comprehensive best practices, coding standards, and security guidelines for developing applications using Amazon S3. It aims to ensure secure, performant, and maintainable S3 integrations.
globs: *S3*.js,*S3*.ts,*S3*.jsx,*S3*.tsx,*S3*.py,*S3*.java,*S3*.go,*S3*.csharp
description: This guide provides definitive best practices for interacting with Amazon S3, focusing on security, performance, cost optimization, and maintainable code in modern AWS applications.
globs: **/*
---
- Always disable public access to S3 buckets unless explicitly needed. Use AWS Identity and Access Management (IAM) policies and bucket policies for access control instead of Access Control Lists (ACLs), which are now generally deprecated.
- Implement encryption for data at rest using Server-Side Encryption (SSE), preferably with AWS Key Management Service (KMS) for enhanced security.
- Use S3 Transfer Acceleration for faster uploads over long distances and enable versioning to protect against accidental deletions. Monitor performance using Amazon CloudWatch and enable logging for auditing. Additionally, consider using S3 Storage Lens for insights into storage usage and activity trends.
- Leverage S3's lifecycle policies to transition objects to cheaper storage classes based on access patterns, and regularly review your storage usage to optimize costs. Utilize S3 Intelligent-Tiering for automatic cost savings based on changing access patterns.
# amazon-s3 Best Practices
## Amazon S3 Best Practices and Coding Standards
Amazon S3 is the backbone of object storage in AWS. Adhering to these guidelines ensures your S3 implementations are secure, performant, cost-effective, and maintainable.
This document provides comprehensive best practices, coding standards, and security guidelines for developing applications using Amazon S3. Following these guidelines will help ensure that your S3 integrations are secure, performant, maintainable, and cost-effective.
## 1. Security First
### 1. Code Organization and Structure
Security is paramount. Always assume your S3 buckets contain sensitive data and configure them defensively.
#### 1.1. Directory Structure Best Practices
### 1.1. Encryption at Rest
Organize your code related to Amazon S3 into logical directories based on functionality.
Always encrypt objects at rest. Default to AWS-managed keys (SSE-S3) or KMS (SSE-KMS) for stronger control.
project/
├── src/
│ ├── s3/
│ │ ├── utils.js # Utility functions for S3 operations
│ │ ├── uploader.js # Handles uploading files to S3
│ │ ├── downloader.js # Handles downloading files from S3
│ │ ├── config.js # Configuration for S3 (bucket name, region, etc.)
│ │ ├── errors.js # Custom error handling for S3 operations
│ │ └── index.js # Entry point for S3 module
│ ├── ...
│ └── tests/
│ ├── s3/
│ │ ├── uploader.test.js # Unit tests for uploader.js
│ │ └── ...
│ └── ...
├── ...
#### 1.2. File Naming Conventions
Use descriptive and consistent file names.
* `uploader.js`: Module for uploading files to S3.
* `downloader.js`: Module for downloading files from S3.
* `s3_service.py`: (Python Example) Defines S3 related services.
* `S3Manager.java`: (Java Example) Manages S3 client and configurations.
#### 1.3. Module Organization
* **Single Responsibility Principle:** Each module should have a clear and specific purpose (e.g., uploading, downloading, managing bucket lifecycle).
* **Abstraction:** Hide complex S3 operations behind simpler interfaces.
* **Configuration:** Store S3 configuration details (bucket name, region, credentials) in a separate configuration file.
Example (JavaScript):
javascript
// s3/uploader.js
import AWS from 'aws-sdk';
import config from './config';
const s3 = new AWS.S3(config.s3);
export const uploadFile = async (file, key) => {
const params = {
Bucket: config.s3.bucketName,
Key: key,
Body: file
};
try {
await s3.upload(params).promise();
console.log(`File uploaded successfully: ${key}`);
} catch (error) {
console.error('Error uploading file:', error);
throw error; // Re-throw for handling in the caller.
}
};
#### 1.4. Component Architecture
For larger applications, consider a component-based architecture. This can involve creating distinct components for different S3-related tasks. For example:
* **Upload Component:** Handles file uploads, progress tracking, and error handling.
* **Download Component:** Handles file downloads, progress tracking, and caching.
* **Management Component:** Manages bucket creation, deletion, and configuration.
#### 1.5. Code Splitting
If you have a large application using S3, consider using code splitting to reduce the initial load time. This involves breaking your code into smaller chunks that can be loaded on demand. This is especially relevant for front-end applications using S3 for asset storage.
* **Dynamic Imports:** Use dynamic imports to load S3-related modules only when needed.
* **Webpack/Rollup:** Configure your bundler to create separate chunks for S3 code.
### 2. Common Patterns and Anti-patterns
#### 2.1. Design Patterns
* **Strategy Pattern:** Use a strategy pattern to handle different storage classes or encryption methods.
* **Factory Pattern:** Use a factory pattern to create S3 clients with different configurations.
* **Singleton Pattern:** Use a singleton pattern if you want to use one s3 instance for all the s3 interactions.
#### 2.2. Recommended Approaches for Common Tasks
* **Uploading large files:** Use multipart upload for files larger than 5 MB. This allows you to upload files in parallel and resume interrupted uploads.
* **Downloading large files:** Use byte-range fetches to download files in chunks. This is useful for resuming interrupted downloads and for accessing specific portions of a file.
* **Deleting multiple objects:** Use the `deleteObjects` API to delete multiple objects in a single request. This is more efficient than deleting objects one by one.
#### 2.3. Anti-patterns and Code Smells
* **Hardcoding credentials:** Never hardcode AWS credentials in your code. Use IAM roles or environment variables.
* **Insufficient error handling:** Always handle errors from S3 operations gracefully. Provide informative error messages and retry failed operations.
* **Ignoring bucket access control:** Properly configure bucket policies and IAM roles to restrict access to your S3 buckets.
* **Overly permissive bucket policies:** Avoid granting overly broad permissions in your bucket policies. Follow the principle of least privilege.
* **Not using versioning:** Failing to enable versioning can lead to data loss if objects are accidentally deleted or overwritten.
* **Assuming immediate consistency:** S3 provides eventual consistency for some operations. Be aware of this and design your application accordingly.
* **Polling for object existence:** Instead of polling, use S3 events to trigger actions when objects are created or modified.
* **Inefficient data retrieval:** Avoid retrieving entire objects when only a portion of the data is needed. Use byte-range fetches or S3 Select to retrieve only the necessary data.
#### 2.4. State Management
* **Stateless operations:** Design your S3 operations to be stateless whenever possible. This makes your application more scalable and resilient.
* **Caching:** Use caching to reduce the number of requests to S3. Consider using a CDN (Content Delivery Network) to cache frequently accessed objects.
* **Session management:** If you need to maintain state, store session data in a separate database or cache, not in S3.
#### 2.5. Error Handling
* **Retry mechanism:** Implement retry logic with exponential backoff for transient errors.
* **Specific error handling:** Handle different S3 errors differently (e.g., retry 503 errors, log 403 errors).
* **Centralized error logging:** Log all S3 errors to a centralized logging system for monitoring and analysis.
Example (Python):
python
❌ **BAD: Unencrypted objects or relying on client-side encryption without server-side enforcement.**
```python
import boto3
from botocore.exceptions import ClientError
import time
s3 = boto3.client('s3')
def upload_file(file_name, bucket, object_name=None):
"""Upload a file to an S3 bucket"""
if object_name is None:
object_name = file_name
for attempt in range(3): # Retry up to 3 times
try:
response = s3.upload_file(file_name, bucket, object_name)
return True
except ClientError as e:
if e.response['Error']['Code'] == 'NoSuchBucket':
print(f"The bucket {bucket} does not exist.")
return False
elif e.response['Error']['Code'] == 'AccessDenied':
print("Access denied. Check your credentials and permissions.")
return False
else:
print(f"An error occurred: {e}")
if attempt < 2: # Wait and retry
time.sleep(2 ** attempt)
else:
return False
except Exception as e:
print(f"An unexpected error occurred: {e}")
return False
return False # Reached max retries and failed
### 3. Performance Considerations
#### 3.1. Optimization Techniques
* **Use S3 Transfer Acceleration:** If you are uploading or downloading files from a geographically distant location, use S3 Transfer Acceleration to improve performance. S3 Transfer Acceleration utilizes Amazon CloudFront's globally distributed edge locations.
* **Use multipart upload:** For large files, use multipart upload to upload files in parallel. The documentation states the best practice is to use this for files larger than 5MB.
* **Enable gzip compression:** Compress objects before uploading them to S3 to reduce storage costs and improve download times. Set the `Content-Encoding` header to `gzip` when uploading compressed objects.
* **Use HTTP/2:** Enable HTTP/2 on your S3 bucket to improve performance.
* **Optimize object sizes:** Store related data in a single object to reduce the number of requests to S3.
#### 3.2. Memory Management
* **Stream data:** Avoid loading entire files into memory. Use streams to process data in chunks.
* **Release resources:** Release S3 client objects when they are no longer needed.
#### 3.3. Bundle Size Optimization
* **Tree shaking:** Use a bundler that supports tree shaking to remove unused code from your bundle.
* **Code splitting:** Split your code into smaller chunks that can be loaded on demand.
#### 3.4. Lazy Loading
* **Load images on demand:** Load images from S3 only when they are visible on the screen.
* **Lazy load data:** Load data from S3 only when it is needed.
### 4. Security Best Practices
#### 4.1. Common Vulnerabilities
* **Publicly accessible buckets:** Ensure that your S3 buckets are not publicly accessible.
* **Insufficient access control:** Properly configure bucket policies and IAM roles to restrict access to your S3 buckets.
* **Cross-site scripting (XSS):** Sanitize user input to prevent XSS attacks if you are serving content directly from S3.
* **Data injection:** Validate all data before storing it in S3 to prevent data injection attacks.
#### 4.2. Input Validation
* **Validate file types:** Validate the file types of uploaded objects to prevent malicious files from being stored in S3.
* **Validate file sizes:** Limit the file sizes of uploaded objects to prevent denial-of-service attacks.
* **Sanitize file names:** Sanitize file names to prevent directory traversal attacks.
#### 4.3. Authentication and Authorization
* **Use IAM roles:** Use IAM roles to grant permissions to applications running on EC2 instances or other AWS services.
* **Use temporary credentials:** Use temporary credentials for applications that need to access S3 from outside of AWS. You can use AWS STS (Security Token Service) to generate temporary credentials.
* **Principle of least privilege:** Grant only the minimum permissions required for each user or application.
#### 4.4. Data Protection
* **Encrypt data at rest:** Use server-side encryption (SSE) or client-side encryption to encrypt data at rest in S3.
* **Encrypt data in transit:** Use HTTPS to encrypt data in transit between your application and S3.
* **Enable versioning:** Enable versioning to protect against accidental data loss.
* **Enable MFA Delete:** Require multi-factor authentication to delete objects from S3.
* **Object locking:** Use S3 Object Lock to prevent objects from being deleted or overwritten for a specified period of time.
#### 4.5. Secure API Communication
* **Use HTTPS:** Always use HTTPS to communicate with the S3 API.
* **Validate certificates:** Validate the SSL/TLS certificates of the S3 endpoints.
* **Restrict access:** Restrict access to the S3 API using IAM policies and bucket policies.
### 5. Testing Approaches
#### 5.1. Unit Testing
* **Mock S3 client:** Mock the S3 client to isolate your unit tests.
* **Test individual functions:** Test individual functions that interact with S3.
* **Verify error handling:** Verify that your code handles S3 errors correctly.
Example (JavaScript with Jest):
javascript
// s3/uploader.test.js
import { uploadFile } from './uploader';
import AWS from 'aws-sdk';
jest.mock('aws-sdk', () => {
const mS3 = {
upload: jest.fn().mockReturnThis(),
promise: jest.fn(),
};
return {
S3: jest.fn(() => mS3),
};
});
describe('uploadFile', () => {
it('should upload file successfully', async () => {
const mockS3 = new AWS.S3();
mockS3.promise.mockResolvedValue({});
const file = 'test file content';
const key = 'test.txt';
await uploadFile(file, key);
expect(AWS.S3).toHaveBeenCalledTimes(1);
expect(mockS3.upload).toHaveBeenCalledWith({
Bucket: 'your-bucket-name',
Key: key,
Body: file
});
});
it('should handle upload error', async () => {
const mockS3 = new AWS.S3();
mockS3.promise.mockRejectedValue(new Error('Upload failed'));
const file = 'test file content';
const key = 'test.txt';
await expect(uploadFile(file, key)).rejects.toThrow('Upload failed');
});
});
#### 5.2. Integration Testing
* **Test with real S3 buckets:** Create a dedicated S3 bucket for integration tests.
* **Test end-to-end flows:** Test complete workflows that involve S3 operations.
* **Verify data integrity:** Verify that data is correctly stored and retrieved from S3.
#### 5.3. End-to-End Testing
* **Simulate user scenarios:** Simulate real user scenarios to test your application's S3 integration.
* **Monitor performance:** Monitor the performance of your S3 integration under load.
#### 5.4. Test Organization
* **Separate test directories:** Create separate test directories for unit tests, integration tests, and end-to-end tests.
* **Descriptive test names:** Use descriptive test names that clearly indicate what is being tested.
#### 5.5. Mocking and Stubbing
* **Mock S3 client:** Use a mocking library (e.g., Jest, Mockito) to mock the S3 client.
* **Stub S3 responses:** Stub S3 API responses to simulate different scenarios.
* **Use dependency injection:** Use dependency injection to inject mocked S3 clients into your components.
### 6. Common Pitfalls and Gotchas
#### 6.1. Frequent Mistakes
* **Forgetting to handle errors:** Failing to handle S3 errors can lead to unexpected behavior and data loss.
* **Using incorrect region:** Using the wrong region can result in connection errors and data transfer costs.
* **Exposing sensitive data:** Storing sensitive data in S3 without proper encryption can lead to security breaches.
* **Not cleaning up temporary files:** Failing to delete temporary files after uploading them to S3 can lead to storage waste.
* **Overusing public read access:** Granting public read access to S3 buckets can expose sensitive data to unauthorized users.
#### 6.2. Edge Cases
* **Eventual consistency:** S3 provides eventual consistency for some operations. Be aware of this and design your application accordingly.
* **Object size limits:** Be aware of the object size limits for S3.
* **Request rate limits:** Be aware of the request rate limits for S3.
* **Special characters in object keys:** Handle special characters in object keys correctly.
#### 6.3. Version-Specific Issues
* **SDK compatibility:** Ensure that your AWS SDK is compatible with the S3 API version.
* **API changes:** Be aware of any API changes that may affect your application.
#### 6.4. Compatibility Concerns
* **Browser compatibility:** Ensure that your application is compatible with different browsers if you are using S3 directly from the browser.
* **Serverless environments:** Be aware of any limitations when using S3 in serverless environments (e.g., Lambda).
#### 6.5. Debugging Strategies
* **Enable logging:** Enable logging to track S3 API calls and errors.
* **Use S3 monitoring tools:** Use S3 monitoring tools to monitor the performance and health of your S3 buckets.
* **Check S3 access logs:** Analyze S3 access logs to identify potential security issues.
* **Use AWS CloudTrail:** Use AWS CloudTrail to track API calls to S3.
* **Use AWS X-Ray:** Use AWS X-Ray to trace requests through your application and identify performance bottlenecks.
### 7. Tooling and Environment
#### 7.1. Recommended Development Tools
* **AWS CLI:** The AWS Command Line Interface (CLI) is a powerful tool for managing S3 resources.
* **AWS SDK:** The AWS SDK provides libraries for interacting with S3 from various programming languages.
* **S3 Browser:** S3 Browser is a Windows client for managing S3 buckets and objects.
* **Cyberduck:** Cyberduck is a cross-platform client for managing S3 buckets and objects.
* **Cloudberry Explorer:** Cloudberry Explorer is a Windows client for managing S3 buckets and objects.
#### 7.2. Build Configuration
* **Use environment variables:** Store S3 configuration details (bucket name, region, credentials) in environment variables.
* **Use a build tool:** Use a build tool (e.g., Maven, Gradle, Webpack) to manage your project dependencies and build your application.
#### 7.3. Linting and Formatting
* **Use a linter:** Use a linter (e.g., ESLint, PyLint) to enforce code style and best practices.
* **Use a formatter:** Use a code formatter (e.g., Prettier, Black) to automatically format your code.
#### 7.4. Deployment
* **Use infrastructure as code:** Use infrastructure as code (e.g., CloudFormation, Terraform) to automate the deployment of your S3 resources.
* **Use a deployment pipeline:** Use a deployment pipeline to automate the deployment of your application.
* **Use blue/green deployments:** Use blue/green deployments to minimize downtime during deployments.
#### 7.5. CI/CD Integration
* **Integrate with CI/CD tools:** Integrate your S3 deployment process with CI/CD tools (e.g., Jenkins, CircleCI, Travis CI).
* **Automate testing:** Automate your unit tests, integration tests, and end-to-end tests as part of your CI/CD pipeline.
* **Automate deployments:** Automate the deployment of your application to S3 as part of your CI/CD pipeline.
By following these best practices and coding standards, you can ensure that your Amazon S3 integrations are secure, performant, maintainable, and cost-effective.
s3.put_object(Bucket='my-unencrypted-bucket', Key='data.txt', Body=b'sensitive data')
# Data is stored unencrypted by default if bucket policy doesn't enforce it.
```
✅ **GOOD: Enforce server-side encryption (SSE-S3 or SSE-KMS).**
```python
import boto3
s3 = boto3.client('s3')
# Option 1: SSE-S3 (AWS-managed keys)
s3.put_object(
Bucket='my-secure-bucket',
Key='data.txt',
Body=b'sensitive data',
ServerSideEncryption='AES256' # SSE-S3
)
# Option 2: SSE-KMS (Customer Master Key)
# Ensure 'my-kms-key-id' exists and bucket has permissions.
s3.put_object(
Bucket='my-secure-bucket',
Key='more-data.txt',
Body=b'more sensitive data',
ServerSideEncryption='aws:kms',
SSEKMSKeyId='arn:aws:kms:us-east-1:123456789012:key/my-kms-key-id'
)
```
> **Note on SSE-C:** As of April 2026, SSE-C is disabled for new buckets by default. Only enable it via `PutBucketEncryption` API *after* bucket creation if explicitly required for legacy or specific compliance needs. Prefer SSE-S3 or SSE-KMS.
### 1.2. Access Control: Policies over ACLs
Manage access exclusively through IAM and Bucket Policies. Disable ACLs for simplified, auditable permissions.
❌ **BAD: Relying on ACLs for granular object access.**
```python
# Avoid this pattern. ACLs are disabled by default for new buckets.
s3.put_object_acl(
Bucket='my-bucket',
Key='object.txt',
GrantRead='id="some-aws-account-id"'
)
```
✅ **GOOD: Disable ACLs and use IAM/Bucket Policies.**
```python
# Ensure S3 Object Ownership is set to "Bucket owner enforced" to disable ACLs.
# This is the default for new buckets.
# Example Bucket Policy (IaC recommended):
# This policy grants read-only access to a specific IAM role for a specific prefix.
bucket_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789012:role/MyReadOnlyRole"
},
"Action": ["s3:GetObject"],
"Resource": ["arn:aws:s3:::my-secure-bucket/data/*"]
}
]
}
# Apply this via IaC (e.g., Terraform, CloudFormation) or boto3.put_bucket_policy
```
### 1.3. Block Public Access
Always enable S3 Block Public Access at the account or organization level. Buckets should never be public unless explicitly required for static website hosting (and even then, use CloudFront with OAC).
✅ **GOOD: Enable S3 Block Public Access.**
```python
# This is typically configured via AWS Console, AWS CLI, or IaC at the account level.
# Example for a specific bucket (via boto3, but IaC is preferred):
s3.put_public_access_block(
Bucket='my-secure-bucket',
PublicAccessBlockConfiguration={
'BlockPublicAcls': True,
'IgnorePublicAcls': True,
'BlockPublicPolicy': True,
'RestrictPublicBuckets': True
}
)
```
## 2. Code Organization & Data Governance
Structured naming, versioning, and lifecycle rules are crucial for manageability and cost control.
### 2.1. Bucket & Object Naming
Adopt clear, hierarchical naming conventions, especially for data lakes. This simplifies governance and policy application.
❌ **BAD: Generic or flat naming.**
```
my-app-bucket/customerdata_20230101.csv
my-app-bucket/logs_server1_access.log
```
✅ **GOOD: Hierarchical, environment-aware naming.**
```
# Bucket name (globally unique, often includes org/env/purpose)
# e.g., myorg-prod-datalake-raw-us-east-1
# Object keys (within the bucket)
# For data lakes:
# <environment>/<layer>/<source>/<dataset>/<year>/<month>/<day>/<file>
# e.g., prod/raw/ecommerce/orders/2025/01/15/order_batch_1.json
# e.g., dev/curated/analytics/user_sessions/2025/01/15/session_summary.parquet
# For application assets:
# <environment>/<application>/<asset_type>/<id>/<file>
# e.g., prod/webapp/images/user-123/profile.jpg
# e.g., dev/backend/configs/service-a/config.yaml
```
### 2.2. Versioning & Tagging
Enable versioning on all critical buckets. Use object tags for cost allocation, lifecycle management, and audit trails.
❌ **BAD: No versioning, untagged objects.**
```python
# Accidental deletion or overwrite means data loss.
s3.delete_object(Bucket='my-bucket', Key='important-report.csv')
# Oops, gone forever.
```
✅ **GOOD: Enable versioning and tag objects.**
```python
# Terraform example for bucket versioning:
# resource "aws_s3_bucket" "my_bucket" {
# bucket = "myorg-prod-data"
# versioning {
# enabled = true
# }
# # ... other configurations
# }
# Python example for tagging an object:
s3.put_object(
Bucket='myorg-prod-data',
Key='reports/monthly/sales-2025-01.csv',
Body=b'sales data',
Tagging='department=finance&project=sales-reporting'
)
```
### 2.3. Lifecycle Management
Automate object transitions to cheaper storage classes (e.g., Glacier, Intelligent-Tiering) or deletion based on access patterns. Use S3 Inventory to gain insights.
✅ **GOOD: Define lifecycle rules.**
```python
# Terraform example for a lifecycle rule:
# resource "aws_s3_bucket_lifecycle_configuration" "my_bucket_lifecycle" {
# bucket = aws_s3_bucket.my_bucket.id
#
# rule {
# id = "archive_old_logs"
# status = "Enabled"
#
# filter {
# prefix = "logs/"
# }
#
# transition {
# days = 30
# storage_class = "STANDARD_IA" # Infrequent Access
# }
# transition {
# days = 90
# storage_class = "GLACIER_FLEXIBLE_RETRIEVAL"
# }
# expiration {
# days = 365
# }
# }
# # ... other rules
# }
```
## 3. Performance Optimization
Design your S3 interactions for maximum throughput and minimal latency.
### 3.1. Multipart Uploads
For files larger than 100MB, always use multipart uploads. This improves upload speed, resilience, and allows for parallelization.
❌ **BAD: Uploading large files in a single PUT operation.**
```python
# This can be slow and prone to failure for large files.
s3.put_object(Bucket='my-bucket', Key='large-video.mp4', Body=open('large-video.mp4', 'rb'))
```
✅ **GOOD: Use `transfer_manager` or `upload_fileobj` for multipart uploads.**
```python
import boto3
from boto3.s3.transfer import TransferConfig
s3 = boto3.client('s3')
config = TransferConfig(multipart_threshold=1024 * 25, max_concurrency=10, multipart_chunksize=1024 * 25) # 25MB parts
with open('large-video.mp4', 'rb') as data:
s3.upload_fileobj(data, 'my-bucket', 'large-video.mp4', Config=config)
```
### 3.2. Request Rate & Prefixes
Distribute high-throughput workloads across multiple S3 prefixes. S3 scales performance based on prefixes. Aim for at least 3-4 x 10^4 requests per second per prefix.
❌ **BAD: Concentrating all high-volume reads/writes on a single prefix.**
```
my-bucket/logs/all_access_logs_YYYY-MM-DD.log # Single hot prefix
```
✅ **GOOD: Spread workloads across multiple prefixes.**
```
my-bucket/logs/webserver1/access_logs_YYYY-MM-DD.log
my-bucket/logs/webserver2/access_logs_YYYY-MM-DD.log
my-bucket/logs/appserver1/access_logs_YYYY-MM-DD.log
# Or use hash-based prefixes for even distribution:
# my-bucket/data/hash_prefix_A/object_id.json
# my-bucket/data/hash_prefix_B/object_id.json
```
## 4. Infrastructure as Code (IaC) with Terraform
When using S3 as a Terraform backend, follow these critical best practices.
❌ **BAD: Using local state or deprecated DynamoDB locking.**
```terraform
# backend "s3" {
# bucket = "myorg-tf-states"
# key = "myapp/prod.tfstate"
# region = "us-east-1"
# # Missing versioning, encryption, and using deprecated DynamoDB for locking
# dynamodb_table = "terraform-locks" # DEPRECATED!
# }
```
✅ **GOOD: Enable versioning, encryption, and S3 native locking.**
```terraform
terraform {
backend "s3" {
bucket = "myorg-tf-states"
key = "myapp/prod.tfstate"
region = "us-east-1"
encrypt = true # Always encrypt state file
versioning_enabled = true # Critical for state recovery
use_lockfile = true # RECOMMENDED: S3 native locking (Terraform 1.10+)
}
}
```
## 5. Testing Approaches
Thoroughly test your S3 interactions, especially security and data integrity.
### 5.1. Local Integration Testing
Use tools like LocalStack to simulate S3 locally, allowing for rapid integration testing without incurring AWS costs or affecting production resources.
✅ **GOOD: Use LocalStack for local S3 emulation.**
```python
# Example with boto3 and LocalStack (ensure LocalStack is running)
s3_local = boto3.client(
's3',
endpoint_url='http://localhost:4566',
aws_access_key_id='test',
aws_secret_access_key='test',
region_name='us-east-1'
)
s3_local.create_bucket(Bucket='test-bucket')
s3_local.put_object(Bucket='test-bucket', Key='test-object.txt', Body=b'hello')
```
### 5.2. Policy Validation
Programmatically validate IAM and bucket policies to ensure least privilege and prevent unintended access.
✅ **GOOD: Use AWS IAM Policy Simulator or `boto3` for validation.**
```python
# Example: Check if an IAM role can perform a specific S3 action
iam_client = boto3.client('iam')
response = iam_client.simulate_principal_policy(
PolicySourceArn='arn:aws:iam::123456789012:role/MyReadOnlyRole',
ActionNames=['s3:GetObject'],
ResourceArns=['arn:aws:s3:::my-secure-bucket/data/report.csv']
)
# Assert that 'Effect' is 'Allow' for the action.
```