Skip to main content

Langkah-langkah membuat Zero Spend Budget Alert (Automation)

Di artikel sebelumnya yaitu tentang langkah-langkah membuat Zero Spend Budget Alert secara manual disini, mari kita lanjut ke bagian automatisasi dengan CloudFormation (IaC). Aku akan buatkan template-nya + jelaskan cara menjalankannya di AWS Console. 😎

📄 CloudFormation Template (YAML) – Budget + SNS + Lambda

AWSTemplateFormatVersion: '2010-09-09'
Description: Zero Spend Budget Notification using SNS and Lambda

Parameters:
  NotificationEmail:
    Type: String
    Description: Email address to receive budget notifications

Resources:

  ZeroSpendSNSTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: ZeroSpendBudgetTopic

  SNSEmailSubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol: email
      Endpoint: !Ref NotificationEmail
      TopicArn: !Ref ZeroSpendSNSTopic

  BudgetNotificationLambdaRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: BudgetNotificationLambdaRole
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

  BudgetNotificationFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: BudgetNotificationHandler
      Runtime: python3.12
      Handler: index.lambda_handler
      Role: !GetAtt BudgetNotificationLambdaRole.Arn
      Code:
        ZipFile: |
          def lambda_handler(event, context):
              print("Received budget alert event:")
              print(event)
              return {
                  'statusCode': 200,
                  'body': 'Budget notification processed.'
              }

  BudgetNotificationPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !Ref BudgetNotificationFunction
      Principal: sns.amazonaws.com
      SourceArn: !Ref ZeroSpendSNSTopic

  BudgetNotificationSNSSubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol: lambda
      Endpoint: !GetAtt BudgetNotificationFunction.Arn
      TopicArn: !Ref ZeroSpendSNSTopic

  ZeroSpendBudget:
    Type: AWS::Budgets::Budget
    Properties:
      Budget:
        BudgetName: ZeroSpendBudget
        BudgetLimit:
          Amount: 0.01
          Unit: USD
        BudgetType: COST
        TimeUnit: MONTHLY
      NotificationsWithSubscribers:
        - Notification:
            NotificationType: ACTUAL
            ComparisonOperator: GREATER_THAN
            Threshold: 100
            ThresholdType: PERCENTAGE
          Subscribers:
            - SubscriptionType: SNS
              Address: !Ref ZeroSpendSNSTopic

🛠️ Cara Menjalankan CloudFormation Template (di AWS Console)

1. Simpan template ke file

Misalnya simpan dengan nama: zero-spend-budget.yaml

2. Buka AWS Console:

🔗 CloudFormation Console

3. Klik “Create stack”“With new resources (standard)”

4. Pilih template:

  • Upload file zero-spend-budget.yaml

5. Isi parameter:

  • NotificationEmail: aku@widianto.org

6. Klik Next → Next → Centang “I acknowledge” → Create stack


⏱️ Setelah Stack Berhasil Dibuat:

  1. Cek email masuk dan konfirmasi subscription SNS.

  2. Budget akan aktif dan memantau limit $0.01 setiap bulan.

  3. Jika melampaui, SNS akan:

    • Kirim email

    • Trigger Lambda (saat ini hanya log event, tapi bisa kamu kembangkan nanti)


💡 Tips Pengembangan Lanjutan:

  • Tambahkan pengiriman ke Telegram, Slack, atau webhook dari Lambda

  • Gunakan CloudWatch log insights untuk monitor event Lambda

  • Tambahkan Tag atau Metadata untuk budget lebih kompleks (misal: "Dev Environment Budget")