Log Metric DependsOn - Lambda creation

Hi,

Somehow I cannot figure out how to use the DependsOn-Property correctly. I want to deploy a lambda function and also add a metric on the logs in one deploy. This fails because the logs are not created yet when serverless would like to create the Metric. I found out that there is a property for my serverless.yml called DependsOnbut this throws the following error.

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

I also tried

DependsOn: 
  - Ref: myFunction

but this throws:

The CloudFormation template is invalid: Template format error: Every DependsOn value must be a string.

these are the section from my serverless.yml

  myFunction:
    handler: handler.index
    vpc: ${self:custom.vpc}
    environment: ${self:custom.env.${self:custom.defaultStage}}
resources:
  Resources:
    myMetricFilter:
      Type: "AWS::Logs::MetricFilter"
      DependsOn:
        - myFunction
      Properties:
        LogGroupName: ${self:custom.log}${self:service}-${self:custom.defaultStage}-myFunction
        FilterPattern: "[...]"
        MetricTransformations:
          -
            MetricValue: "1"
            MetricNamespace: "LogMetrics"
            MetricName: "MyMetricName"

Any tips, tricks and pointers into the right direction would be highly appreciated.

The function name (that you need to pass to DependsOn) is not the name of your function in your serverless.yml definition - it is the Logical Resource ID of the resource in CloudFormation (see the docs for details), which is generated by Serverless for you when it compiles your CloudFormation template.

Here’s the relevant section of the docs about naming resources. Once you’ve worked out your function’s name, use that in the DependsOn field and you should be right).

Another way to work it out is to compile your template (using the “sls package” command) and inspect the file created in the .serverless directory of your service.

1 Like

Thanks! Worked like a charm.

Can you guys please share an example

this my yml

functions:
get:
handler: src/handlers/get.get
events:
- schedule:
rate: rate(10 minutes)
enabled: false
esources:
Resources:
usersTable:
Type: AWS::S3::Bucket
Properties:
BucketName: table-${opt:stage}
exceptionGetMetricsFilter:
Type: "AWS::Logs::MetricFilter"
DependsOn:
- getLambdaFunction
Properties:
LogGroupName:
Ref: /aws/lambda/dc-${opt:stage}-get
FilterPattern: "“Exception ->”"
MetricTransformations:
-
MetricValue: "1"
MetricNamespace: "dc/get-exception"
MetricName: “dc-get-exception”

i am getting this error
CloudFormation template is invalid: Template format error: Unresolved resource dependencies [/aws/lambda/dc-dev-get, getLambdaFunction] in the Resources block of the template

Ok, resolved. I didn’t realize that the logical ids were case sensitive

1 Like