Example AWS CloudFormation Templates for Queue-Driven Systems
ID: SUS_SUS3_aws_cloudformation_templates
Code: SUS3_1
**AWS CloudFormation Template Example for a Queue-Driven System**:
“`yaml
AWSTemplateFormatVersion: ‘2010-09-09’
Description: Sample CloudFormation template to create a queue-driven system for asynchronous job management.
Resources:
MyQueue:
Type: ‘AWS::SQS::Queue’
Properties:
QueueName: ‘MyQueue’
MyFunction:
Type: ‘AWS::Lambda::Function’
Properties:
Handler: index.handler
Role: !GetAtt MyLambdaExecutionRole.Arn
Code:
ZipFile: |
def handler(event, context):
# Logic to process the message from the queue
return ‘Processed’
Runtime: ‘python3.8’
Timeout: 30
MyQueueEventSourceMapping:
Type: ‘AWS::Lambda::EventSourceMapping’
Properties:
BatchSize: 10
EventSourceArn: !GetAtt MyQueue.Arn
FunctionName: !GetAtt MyFunction.Arn
Enabled: true
MyLambdaExecutionRole:
Type: ‘AWS::IAM::Role’
Properties:
RoleName: ‘MyLambdaExecutionRole’
AssumeRolePolicyDocument:
Version: ‘2012-10-17’
Statement:
– Effect: ‘Allow’
Principal:
Service: ‘lambda.amazonaws.com’
Action: ‘sts:AssumeRole’
Policies:
– PolicyName: ‘SQSSendMessagePolicy’
PolicyDocument:
Version: ‘2012-10-17’
Statement:
– Effect: ‘Allow’
Action:
– ‘sqs:SendMessage’
Resource: !GetAtt MyQueue.Arn
“`
**Key Benefits of this Architecture:**
1. **Resource Efficiency**: By utilizing AWS Lambda, resources are only consumed during execution, reducing the idle compute resources when handling jobs.
2. **Scalability**: SQS and Lambda can automatically scale based on the number of messages in the queue, ensuring that no resources are wasted during low-traffic periods.
3. **Asynchronous Processing**: This approach allows for the decoupling of request and processing, which can lead to increased system resilience.
4. **Lower Carbon Footprint**: The serverless nature of this design contributes to sustainability goals by ensuring that computing resources are utilized efficiently, thereby minimizing energy consumption.
By employing efficient design patterns like the queue-driven system illustrated in this example, organizations can optimize their resource utilization and contribute significantly to their sustainability targets.