serverless-aws-alias icon indicating copy to clipboard operation
serverless-aws-alias copied to clipboard

SNS Topic disappears from Cloudformation stack

Open Tehnix opened this issue 5 years ago • 0 comments

We're trying to deploy a serverless stack, which has an RDS resource, and has auto scaling via CloudWatch and SNS::Topic.

I understand there's some special cases around lambda SNS topics, but this one is for RDS.

Deploying it without serverless-aws-alias works, but after adding alias, it seems like the SNS topic disappears completely, and we get,

Serverless: Validating template...

  Error --------------------------------------------------

  The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [StackAlarmTopic] in the Resources block of the template

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information -----------------------------
     OS:                     darwin
     Node Version:           8.10.0
     Serverless Version:     1.28.0

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I took a look at the compiled-cloudformation-template.json in S3, and only the references were there, but no mention of the SNS::Topic.

Our Resources look like the following, with the StackAlarmTopic in the top, and it getting referred to with - Ref: "StackAlarmTopic" later on (near the end, 4 times):

Resources:
  StackAlarmTopic:
    Type: AWS::SNS::Topic
    Properties:
      DisplayName: Stack Alarm Topic

  # Set up the RDS resource.
  RDSCluster:
    Type: AWS::RDS::DBCluster
    Properties:
      DatabaseName: ${self:custom.dbName}
      DBClusterParameterGroupName:
        Ref: RDSClusterParameterGroup
      DBSubnetGroupName: ${cf:vpc-${self:provider.stage}.rdsSubnetGroupName}
      Engine: aurora
      MasterUserPassword: ${self:custom.dbPassword}
      MasterUsername: ${self:custom.dbUsername}
      VpcSecurityGroupIds:
        - ${cf:vpc-${self:provider.stage}.bitbucketPipelineAccessSecurityGroup}
        - ${cf:vpc-${self:provider.stage}.homeAccessSecurityGroup}
        - ${cf:vpc-${self:provider.stage}.intrinsicTrafficSecurityGroup}
        - ${cf:vpc-${self:provider.stage}.officeAccessSecurityGroup}
      BackupRetentionPeriod: 30

  RDSPrimaryInstance:
    Type: AWS::RDS::DBInstance
    Properties:
      DBClusterIdentifier:
        Ref: RDSCluster
      DBParameterGroupName:
        Ref: RDSInstanceParameterGroup
      DBInstanceClass: ${self:custom.dbSize}
      DBInstanceIdentifier: ${self:provider.stage}-${self:custom.dbIdentifier}-primary
      Engine: aurora
      PubliclyAccessible: true

  RDSClusterParameterGroup:
    Type: AWS::RDS::DBClusterParameterGroup
    Properties:
      Description: '${self:custom.dbName} cluster parameters'
      Family: aurora5.6
      Parameters:
        character_set_client: 'utf8mb4'
        character_set_connection: 'utf8mb4'
        character_set_database: 'utf8mb4'
        character_set_results: 'utf8mb4'
        character_set_server: 'utf8mb4'
        collation_connection: 'utf8mb4_unicode_ci'
        collation_server: 'utf8mb4_unicode_ci'

  RDSInstanceParameterGroup:
    Type: AWS::RDS::DBParameterGroup
    Properties:
      Description: '${self:custom.dbName} instance parameters'
      Family: aurora5.6
      Parameters:
        event_scheduler: ON

  ReadReplicaScalableTarget:
    Type: "AWS::ApplicationAutoScaling::ScalableTarget"
    Properties:
      MaxCapacity: 5
      MinCapacity: 0
      ResourceId:
        Fn::Join:
          - ":"
          - - cluster
            - Ref: RDSCluster
      RoleARN:
        Fn::GetAtt: [ScalingRole, Arn]
      ScalableDimension: rds:cluster:ReadReplicaCount
      ServiceNamespace: rds
    DependsOn: [ScalingRole, RDSCluster]

  ScalingRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          -
            Effect: "Allow"
            Principal:
              Service:
                - application-autoscaling.amazonaws.com
            Action:
              - "sts:AssumeRole"
      Path: "/"
      Policies:
        -
          PolicyName: "root"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              -
                Effect: "Allow"
                Action:
                  - "rds:AddTagsToResource"
                  - "rds:CreateDBInstance"
                  - "rds:DeleteDBInstance"
                  - "rds:DescribeDBClusters"
                  - "rds:DescribeDBInstances"
                  - "cloudwatch:PutMetricAlarm"
                  - "cloudwatch:DescribeAlarms"
                  - "cloudwatch:GetMetricStatistics"
                  - "cloudwatch:SetAlarmState"
                  - "cloudwatch:DeleteAlarms"
                Resource: "*"

  ReadReplicaScalingPolicy:
    Type: "AWS::ApplicationAutoScaling::ScalingPolicy"
    Properties:
      PolicyName: ReadReplicaAutoScalingPolicy
      PolicyType: TargetTrackingScaling
      ScalingTargetId:
        Ref: ReadReplicaScalableTarget
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: 50.0
        ScaleInCooldown: 600
        ScaleOutCooldown: 300
        PredefinedMetricSpecification:
          PredefinedMetricType: RDSReaderAverageCPUUtilization
    DependsOn: ReadReplicaScalableTarget


  DatabasePrimaryCPUAlarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmDescription: Primary database CPU utilization is over 80%.
      Namespace: AWS/RDS
      MetricName: CPUUtilization
      Unit: Percent
      Statistic: Average
      Period: 300
      EvaluationPeriods: 2
      Threshold: 80
      ComparisonOperator: GreaterThanOrEqualToThreshold
      Dimensions:
        - Name: DBInstanceIdentifier
          Value:
            Ref: "RDSPrimaryInstance"
      AlarmActions:
        - Ref: "StackAlarmTopic"
      InsufficientDataActions:
        - Ref: "StackAlarmTopic"

  DatabasePrimaryMemoryAlarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmDescription: Primary database freeable memory is under 100MB.
      Namespace: AWS/RDS
      MetricName: FreeableMemory
      Unit: Bytes
      Statistic: Average
      Period: 300
      EvaluationPeriods: 2
      Threshold: 100000000
      ComparisonOperator: LessThanOrEqualToThreshold
      Dimensions:
        - Name: DBInstanceIdentifier
          Value:
            Ref: "RDSPrimaryInstance"
      AlarmActions:
        - Ref: "StackAlarmTopic"
      InsufficientDataActions:
        - Ref: "StackAlarmTopic"

It's unclear to me whether this is a bug here, or if we just need to adjust our SNS topic usage to this plugin.

Any help appreciated!

Tehnix avatar Jul 25 '18 20:07 Tehnix