Process Amazon SQS Messages with an Elastic Beanstalk Worker Environment¶
This recipe shows how to run asynchronous processing with Elastic Beanstalk worker environments backed by Amazon SQS. Use this pattern when web requests should hand off slower work such as image processing, notifications, or report generation.
Prerequisites¶
- Existing Elastic Beanstalk application.
- Amazon SQS queue for worker messages.
- IAM permissions for SQS and worker environment creation.
- A Java worker application that can poll or receive work from the queue.
What You'll Build¶
You will build:
- A worker-tier Elastic Beanstalk environment.
- Queue settings that connect the worker to Amazon SQS.
- A simple Spring Boot message processor.
flowchart LR
A[Web Environment] --> B[Amazon SQS Queue]
B --> C[Elastic Beanstalk Worker Environment]
C --> D[Spring Boot Worker Process]
D --> E[Downstream AWS Service or Database] Steps¶
- Create the SQS queue if you do not already have one.
- Initialize a worker environment.
- Configure the worker queue URL and polling behavior.
option_settings:
aws:elasticbeanstalk:sqsd:
HttpPath: /worker/process
MimeType: application/json
WorkerQueueURL: https://sqs.$REGION.amazonaws.com/<account-id>/$APP_NAME-worker
- Add a Spring Boot endpoint to receive work from the daemon.
package com.example.guide.controller;
import java.util.Map;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WorkerController {
@PostMapping("/worker/process")
public ResponseEntity<Map<String, String>> process(@RequestBody String body) {
return ResponseEntity.ok(Map.of("status", "processed", "payload", body));
}
}
- Send a test message.
aws sqs send-message --queue-url "https://sqs.$REGION.amazonaws.com/<account-id>/$APP_NAME-worker" --message-body '{"job":"health-check"}' --region "$REGION"
- Check worker logs and events.
Verification¶
Use these checks after deployment:
eb status "$ENV_NAME-worker"
eb logs --all "$ENV_NAME-worker"
aws sqs get-queue-attributes --queue-url "https://sqs.$REGION.amazonaws.com/<account-id>/$APP_NAME-worker" --attribute-names ApproximateNumberOfMessages ApproximateNumberOfMessagesNotVisible --region "$REGION"
Expected outcomes:
- The worker environment reaches
Readystate. - Messages are delivered from SQS to the worker HTTP endpoint.
- Worker logs confirm successful processing.
- Queue depth returns toward zero after processing.