|
| 1 | +# Terraform AWS ARC SQS Module Usage Guide |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +### Purpose of the Document |
| 6 | + |
| 7 | +This document guides implementation of the Terraform AWS ARC SQS module for managing Amazon SQS queues in AWS infrastructure. |
| 8 | + |
| 9 | +### Module Overview |
| 10 | + |
| 11 | +The Terraform AWS ARC SQS module provides a secure and modular foundation for deploying SQS queues on AWS. This module supports both Standard and FIFO queues, with comprehensive features including: |
| 12 | + |
| 13 | +- Standard and FIFO queue configurations |
| 14 | +- Dead Letter Queue (DLQ) support for message failure handling |
| 15 | +- Flexible encryption options (SQS-managed SSE or customer-managed KMS keys) |
| 16 | +- Configurable message retention, visibility timeouts, and polling settings |
| 17 | +- Queue policy management for fine-grained access control |
| 18 | + |
| 19 | +### Prerequisites |
| 20 | + |
| 21 | +Before using this module, ensure you have the following: |
| 22 | + |
| 23 | +- AWS credentials configured |
| 24 | +- Terraform version 1.3 or higher installed |
| 25 | +- AWS Provider version 5.0 or higher |
| 26 | +- A working knowledge of AWS SQS, IAM policies, KMS (optional), and Terraform concepts |
| 27 | + |
| 28 | +## Getting Started |
| 29 | + |
| 30 | +### Module Source |
| 31 | + |
| 32 | +To use the module in your Terraform configuration, include the following source block: |
| 33 | + |
| 34 | +```hcl |
| 35 | +module "arc-sqs" { |
| 36 | + source = "sourcefuse/arc-sqs/aws" |
| 37 | + version = "0.0.1" # Use the latest version from Terraform Registry |
| 38 | +
|
| 39 | + name = "my-application-queue" |
| 40 | +
|
| 41 | + tags = module.tags.tags |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Refer to the [Terraform Registry](https://registry.terraform.io/modules/sourcefuse/arc-sqs/aws/latest) for the latest version. |
| 46 | + |
| 47 | +### Integration with Existing Terraform Configurations |
| 48 | + |
| 49 | +To integrate with an existing Terraform mono repo configuration: |
| 50 | + |
| 51 | +- Create a new folder in `terraform/` named `sqs` or a service-specific name (e.g., `order-processing-queue`) |
| 52 | +- Create the required files (see the [examples](https://github.com/sourcefuse/terraform-aws-arc-sqs/tree/main/examples) folder for reference): |
| 53 | + - `main.tf` - Main module configuration |
| 54 | + - `variables.tf` - Variable declarations |
| 55 | + - `outputs.tf` - Output values |
| 56 | + - `versions.tf` - Terraform and provider version constraints |
| 57 | +- Configure your Terraform backend: |
| 58 | + - Create the environment backend configuration file: `config.<environment>.hcl` |
| 59 | + - `region`: Where the backend resides |
| 60 | + - `key`: `<working_directory>/terraform.tfstate` |
| 61 | + - `bucket`: Bucket name where the terraform state will reside |
| 62 | + - `dynamodb_table`: Lock table to prevent concurrent terraform operations |
| 63 | + - `encrypt`: Enable encryption for all traffic to and from the backend |
| 64 | + |
| 65 | +### Required AWS Permissions |
| 66 | + |
| 67 | +AWS credentials require permissions to create, list, and modify: |
| 68 | + |
| 69 | +- SQS queues (`sqs:CreateQueue`, `sqs:DeleteQueue`, `sqs:GetQueueAttributes`, `sqs:SetQueueAttributes`, `sqs:TagQueue`, `sqs:UntagQueue`) |
| 70 | +- SQS queue policies (`sqs:GetQueueUrl`, `sqs:AddPermission`, `sqs:RemovePermission`) |
| 71 | +- KMS keys (if using customer-managed encryption): `kms:CreateKey`, `kms:CreateAlias`, `kms:DescribeKey`, `kms:EnableKeyRotation`, `kms:GetKeyPolicy`, `kms:PutKeyPolicy`, `kms:TagResource` |
| 72 | +- IAM policy documents: `iam:GetPolicyDocument` (for data source evaluation) |
| 73 | + |
| 74 | +## Module Configuration |
| 75 | + |
| 76 | +### Input Variables |
| 77 | + |
| 78 | +For all input variables, see the README [Inputs](https://github.com/sourcefuse/terraform-aws-arc-sqs#inputs) section. |
| 79 | + |
| 80 | +Key variables: |
| 81 | + |
| 82 | +- **name**: Name of the SQS queue (required) |
| 83 | +- **fifo_config**: Configuration for FIFO queues with message ordering |
| 84 | +- **message_config**: Message handling settings (retention, visibility timeout, polling) |
| 85 | +- **kms_config**: KMS encryption configuration (key_arn for existing key, create_key for new key) |
| 86 | +- **dlq_config**: Dead Letter Queue configuration for handling failed messages |
| 87 | +- **policy_config**: Queue access policy configuration |
| 88 | +- **tags**: Resource tagging for organization and cost tracking |
| 89 | + |
| 90 | +### Output Values |
| 91 | + |
| 92 | +For all outputs, see the README [Outputs](https://github.com/sourcefuse/terraform-aws-arc-sqs#outputs) section. |
| 93 | + |
| 94 | +Key outputs: |
| 95 | + |
| 96 | +- **queue_id/queue_url**: The URL for accessing the created SQS queue |
| 97 | +- **queue_arn**: The ARN of the SQS queue for IAM policies and cross-service integration |
| 98 | +- **dlq_id/dlq_arn**: Dead Letter Queue identifiers (if enabled) |
| 99 | +- **kms_key_arn**: KMS key ARN (if customer-managed encryption is enabled) |
| 100 | + |
| 101 | +## Module Usage |
| 102 | + |
| 103 | +The module supports multiple use cases: |
| 104 | + |
| 105 | +#### 1. Basic Standard Queue |
| 106 | + |
| 107 | +Simplest configuration for a standard SQS queue. See [examples/basic-standard-queue](https://github.com/sourcefuse/terraform-aws-arc-sqs/tree/main/examples/basic-standard-queue). |
| 108 | + |
| 109 | +```hcl |
| 110 | +module "sqs" { |
| 111 | + source = "sourcefuse/arc-sqs/aws" |
| 112 | +
|
| 113 | + name = "my-basic-queue" |
| 114 | +
|
| 115 | + tags = module.tags.tags |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +Creates: |
| 120 | +- Standard SQS queue with default message retention (4 days) |
| 121 | +- SQS-managed encryption (SSE-SQS) |
| 122 | +- Default visibility timeout (30 seconds) |
| 123 | + |
| 124 | +#### 2. Standard Queue with Dead Letter Queue |
| 125 | + |
| 126 | +Reliable message processing with failure handling. See [examples/standard-queue-with-dlq](https://github.com/sourcefuse/terraform-aws-arc-sqs/tree/main/examples/standard-queue-with-dlq). |
| 127 | + |
| 128 | +```hcl |
| 129 | +module "sqs" { |
| 130 | + source = "sourcefuse/arc-sqs/aws" |
| 131 | +
|
| 132 | + name = "my-queue-with-dlq" |
| 133 | +
|
| 134 | + message_config = { |
| 135 | + retention_seconds = 345600 # 4 days |
| 136 | + visibility_timeout = 300 # 5 minutes |
| 137 | + receive_wait_time_seconds = 20 # Enable long polling |
| 138 | + } |
| 139 | +
|
| 140 | + dlq_config = { |
| 141 | + enabled = true |
| 142 | + max_receive_count = 5 |
| 143 | + message_retention_seconds = 1209600 # 14 days for troubleshooting |
| 144 | + } |
| 145 | +
|
| 146 | + tags = module.tags.tags |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +Creates: |
| 151 | +- Standard SQS queue with long polling enabled |
| 152 | +- Automatically configured Dead Letter Queue |
| 153 | +- Extended retention for failed messages (14 days) |
| 154 | + |
| 155 | +#### 3. FIFO Queue |
| 156 | + |
| 157 | +Strict message ordering and exactly-once processing. See [examples/fifo-queue](https://github.com/sourcefuse/terraform-aws-arc-sqs/tree/main/examples/fifo-queue). |
| 158 | + |
| 159 | +```hcl |
| 160 | +module "sqs" { |
| 161 | + source = "sourcefuse/arc-sqs/aws" |
| 162 | +
|
| 163 | + name = "my-fifo-queue.fifo" # Must end with .fifo |
| 164 | +
|
| 165 | + fifo_config = { |
| 166 | + enabled = true |
| 167 | + content_based_deduplication = true |
| 168 | + deduplication_scope = "messageGroup" |
| 169 | + throughput_limit = "perMessageGroupId" |
| 170 | + } |
| 171 | +
|
| 172 | + tags = module.tags.tags |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +Creates: |
| 177 | +- FIFO queue with message ordering guarantees |
| 178 | +- Content-based deduplication preventing duplicate processing |
| 179 | +- High throughput mode with per-message-group limits |
| 180 | + |
| 181 | +#### 4. Encrypted Queue with KMS |
| 182 | + |
| 183 | +Sensitive data with customer-managed encryption. See [examples/encrypted-queue](https://github.com/sourcefuse/terraform-aws-arc-sqs/tree/main/examples/encrypted-queue). |
| 184 | + |
| 185 | +```hcl |
| 186 | +module "sqs" { |
| 187 | + source = "sourcefuse/arc-sqs/aws" |
| 188 | +
|
| 189 | + name = "my-encrypted-queue" |
| 190 | +
|
| 191 | + kms_config = { |
| 192 | + create_key = true |
| 193 | + deletion_window_days = 30 |
| 194 | + rotation_enabled = true |
| 195 | + } |
| 196 | +
|
| 197 | + tags = module.tags.tags |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +Creates: |
| 202 | +- SQS queue with customer-managed KMS encryption |
| 203 | +- Dedicated KMS key with automatic rotation |
| 204 | +- Enhanced security for sensitive workloads |
| 205 | + |
| 206 | +**Alternative - Use existing KMS key:** |
| 207 | +```hcl |
| 208 | +module "sqs" { |
| 209 | + source = "sourcefuse/arc-sqs/aws" |
| 210 | +
|
| 211 | + name = "my-encrypted-queue" |
| 212 | +
|
| 213 | + kms_config = { |
| 214 | + key_arn = "arn:aws:kms:us-east-1:123456789012:key/abc..." |
| 215 | + } |
| 216 | +
|
| 217 | + tags = module.tags.tags |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +#### 5. SNS Fanout Queue |
| 222 | + |
| 223 | +Pub/sub pattern with SNS topic distribution to multiple queues. See [examples/sns-fanout-queue](https://github.com/sourcefuse/terraform-aws-arc-sqs/tree/main/examples/sns-fanout-queue). |
| 224 | + |
| 225 | +```hcl |
| 226 | +module "sqs" { |
| 227 | + source = "sourcefuse/arc-sqs/aws" |
| 228 | +
|
| 229 | + name = "my-fanout-queue" |
| 230 | +
|
| 231 | + policy_config = { |
| 232 | + create = true |
| 233 | + source_policy_documents = [ |
| 234 | + data.aws_iam_policy_document.sns_publish.json |
| 235 | + ] |
| 236 | + } |
| 237 | +
|
| 238 | + tags = module.tags.tags |
| 239 | +} |
| 240 | +``` |
| 241 | + |
| 242 | +Creates: |
| 243 | +- SQS queue configured to receive messages from SNS |
| 244 | +- Queue policy granting SNS publish permissions |
| 245 | +- Fanout messaging pattern |
| 246 | + |
| 247 | +### Tips and Recommendations |
| 248 | + |
| 249 | +- **Use Dead Letter Queues**: Enable DLQs in production to capture and troubleshoot failed messages instead of losing them after max retries |
| 250 | +- **Enable Long Polling**: Set `receive_wait_time_seconds = 20` to reduce empty receives and lower costs |
| 251 | +- **Choose the Right Queue Type**: Use FIFO queues only when strict ordering is required, as they have lower throughput limits compared to Standard queues |
| 252 | +- **Implement Proper Visibility Timeouts**: Set `visibility_timeout` longer than your processing time to prevent duplicate processing |
| 253 | +- **Use KMS for Sensitive Data**: Enable customer-managed encryption for queues handling sensitive information |
| 254 | +- **Tag Your Resources**: Use consistent tagging for cost allocation and resource management |
| 255 | +- **Monitor Queue Metrics**: Set up CloudWatch alarms for metrics like `ApproximateAgeOfOldestMessage` and `ApproximateNumberOfMessagesVisible` |
| 256 | +- **Configure Appropriate Retention**: Balance between storage costs and message recovery needs (default: 4 days, max: 14 days) |
| 257 | + |
| 258 | +## Troubleshooting |
| 259 | + |
| 260 | +### Common Issues |
| 261 | + |
| 262 | +**Issue: FIFO queue name validation error** |
| 263 | +- **Solution**: Ensure FIFO queue names end with `.fifo` suffix |
| 264 | + |
| 265 | +**Issue: Messages not appearing in DLQ** |
| 266 | +- **Solution**: Verify `max_receive_count` is set appropriately and check the DLQ redrive policy |
| 267 | + |
| 268 | +**Issue: KMS encryption errors** |
| 269 | +- **Solution**: Ensure the KMS key policy grants appropriate permissions to SQS service principal and consuming services |
| 270 | + |
| 271 | +**Issue: Access denied when consuming messages** |
| 272 | +- **Solution**: Verify IAM policies grant necessary SQS permissions (`sqs:ReceiveMessage`, `sqs:DeleteMessage`, etc.) |
| 273 | + |
| 274 | +### Reporting Issues |
| 275 | + |
| 276 | +Report bugs and issues on the [GitHub repository](https://github.com/sourcefuse/terraform-aws-arc-sqs/issues). |
| 277 | + |
| 278 | +## Security Considerations |
| 279 | + |
| 280 | +### Encryption at Rest |
| 281 | + |
| 282 | +This module supports two encryption methods: |
| 283 | + |
| 284 | +- **SQS-Managed SSE (SSE-SQS)**: Default encryption using AWS-managed keys |
| 285 | +- **Customer-Managed KMS (SSE-KMS)**: Enhanced security with customer-controlled key rotation and access policies |
| 286 | + |
| 287 | +For sensitive workloads, use customer-managed KMS encryption with automatic key rotation enabled. |
| 288 | + |
| 289 | +### Queue Access Policies |
| 290 | + |
| 291 | +Implement least-privilege access using queue policies: |
| 292 | + |
| 293 | +- Restrict access to specific AWS accounts or services |
| 294 | +- Use condition keys to enforce encryption in transit (`aws:SecureTransport`) |
| 295 | +- Limit actions to only what's necessary for each principal |
| 296 | + |
| 297 | +### Best Practices for AWS SQS Security |
| 298 | + |
| 299 | +Follow AWS security best practices when using this module: |
| 300 | + |
| 301 | +- [AWS SQS Security Best Practices](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-security-best-practices.html) |
| 302 | +- [AWS KMS Best Practices](https://docs.aws.amazon.com/kms/latest/developerguide/best-practices.html) |
| 303 | +- Enable encryption in transit by using HTTPS endpoints |
| 304 | +- Regularly rotate KMS keys (automatically handled when `kms_config.create_key = true` with `kms_config.rotation_enabled = true`) |
| 305 | +- Monitor queue access using AWS CloudTrail |
| 306 | +- Implement proper IAM policies for queue producers and consumers |
| 307 | + |
| 308 | +### Data Privacy |
| 309 | + |
| 310 | +- Enable Dead Letter Queues to prevent data loss |
| 311 | +- Set appropriate message retention periods based on compliance requirements |
| 312 | +- Use KMS encryption for data subject to regulatory requirements (HIPAA, PCI-DSS, etc.) |
| 313 | + |
| 314 | +## Contributing and Community Support |
| 315 | + |
| 316 | +### Contributing Guidelines |
| 317 | + |
| 318 | +Follow the guidelines in [CONTRIBUTING.md](https://github.com/sourcefuse/terraform-aws-arc-sqs/blob/main/CONTRIBUTING.md). |
| 319 | + |
| 320 | +Contributions welcome: |
| 321 | +- Bug fixes and issue reports |
| 322 | +- Feature enhancements |
| 323 | +- Documentation improvements |
| 324 | +- Example implementations |
| 325 | + |
| 326 | +### Reporting Bugs and Issues |
| 327 | + |
| 328 | +Report bugs on the [GitHub repository](https://github.com/sourcefuse/terraform-aws-arc-sqs/issues). |
| 329 | + |
| 330 | +Include: |
| 331 | +- Terraform version |
| 332 | +- Module version |
| 333 | +- Minimal reproduction code |
| 334 | +- Expected vs actual behavior |
| 335 | +- Relevant error messages or logs |
| 336 | + |
| 337 | +## License |
| 338 | + |
| 339 | +This module is licensed under the Apache 2.0 license. See [LICENSE](https://github.com/sourcefuse/terraform-aws-arc-sqs/blob/main/LICENSE) for details. |
0 commit comments