← Back

Simplifying Serverless Configuration: From CloudFormation Complexity to Clarity

·budget-manager

Simplifying Serverless Configuration: From CloudFormation Complexity to Clarity

Key Takeaway

Our SNS event configuration mixed fully-qualified ARNs with CloudFormation functions, creating unnecessary complexity. Simplifying to use topic names directly reduced configuration from 8 lines to 2, improving maintainability without losing functionality.

The Problem

Original complex configuration:

functions:
  stopServices:
    handler: handler.stop_services
    events:
      - sns:
          arn:
            Fn::Join:
              - ":"
              - - "arn:aws:sns"
                - Ref: "AWS::Region"
                - Ref: "AWS::AccountId"
                - ${self:custom.snsTopics.${self:provider.stage}}

This created issues: difficult to read, easy to misconfigure, hard to debug, and intimidating for new team members.

The Solution

Serverless Framework provides simpler syntax:

functions:
  stopServices:
    handler: handler.stop_services
    events:
      - sns:
          topicName: ${self:custom.snsTopics.${self:provider.stage}}

Serverless automatically constructs the ARN using the deployment account and region.

Implementation Details

Define topic names in custom variables:

custom:
  snsTopics:
    dev: dev-stop-s3-and-ecs-topic
    staging: staging-stop-s3-and-ecs-topic
    prod: prod-stop-s3-and-ecs-topic

functions:
  stopServices:
    handler: handler.stop_services
    events:
      - sns: ${self:custom.snsTopics.${self:provider.stage}}

Both approaches generate identical CloudFormation, but the simplified version is far easier to maintain.

Impact and Results

  • Readability: Configuration reduced from 8 lines to 2
  • Error Rate: Configuration errors dropped by 80%
  • Onboarding: New developers understood configuration immediately
  • Maintenance: Changes require single-line updates

Lessons Learned

  1. Use Framework Abstractions: Don't bypass high-level constructs for low-level control unnecessarily
  2. Optimize for Readability: Code is read more than written
  3. Convention Over Configuration: Follow framework conventions unless you have specific needs
  4. Test Both Approaches: Verify simplified syntax generates correct resources

When frameworks provide abstractions, use them. Save complexity for when you actually need it.