How can i set the maximumRetryAttempt for aws lambda in the cloudformation lambda?

Viewed 2811

I have a serverless project created throught visual studio and i'm looking for setting the maximumRetryAttempt of a specific lambda in the cloudformation template. I saw EventInvokeConfig, however the lambda function name is generated automatically and different from each environment. I am wondering if there is an aws specific parameter to get a lambda function name?

  "EventInvokeConfig": {
  "Type" : "AWS::Lambda::EventInvokeConfig",
  "Properties" : {
      "FunctionName" : "???",
      "MaximumRetryAttempts" : 0,
      "Qualifier" : "$LATEST"
    }
}

Here is my serverless template

{
 "AWSTemplateFormatVersion":"2010-09-09",
 "Transform":"AWS::Serverless-2016-10-31",
 "Description":"An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",
 "Resources":{
    "MyFunctionLambda":{
       "Type":"AWS::Serverless::Function",
       "Properties":{
          "Handler":"MyPlatformServerless::MyPlatformServerless.Lambdas.MyFunctionLambda::FunctionHandler",
          "Runtime":"dotnetcore2.1",
          "CodeUri":"",
          "Description":"Default function",
          "MemorySize":512,
          "Timeout":60,
          "Role":null
       }
    }
 }
}
2 Answers

You can make use of the Ref instrinsic function. For the resource of type AWS::Serverless::Function the returned value is the name of the function.

This can be referenced in other resources defined in the template. For EventInvokeConfig, the template would look like

{
    "AWSTemplateFormatVersion":"2010-09-09",
    "Transform":"AWS::Serverless-2016-10-31",
    "Description":"An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",
    "Resources":{
        "MyFunctionLambda":{
            "Type":"AWS::Serverless::Function",
            "Properties":{
                "Handler":"MyPlatformServerless::MyPlatformServerless.Lambdas.MyFunctionLambda::FunctionHandler",
                "Runtime":"dotnetcore2.1",
                "CodeUri":"",
                "Description":"Default function",
                "MemorySize":512,
                "Timeout":60,
                "Role":null
            }
        },
        "EventInvokeConfig": {
            "Type" : "AWS::Lambda::EventInvokeConfig",
            "Properties" : {
                "FunctionName" : { "Ref" : MyFunctionLambda },
                "MaximumRetryAttempts" : 0,
                "Qualifier" : "$LATEST"
            }
        }
    }
}

Your issue might have resolved already but you can improve the code. As you're using Serverless (SAM) you can directly specify the EventInvokeConfig in the lambda resource properties and no need of another resource creation. Please find the below snippet:

{
    "AWSTemplateFormatVersion":"2010-09-09",
    "Transform":"AWS::Serverless-2016-10-31",
    "Description":"An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",
    "Resources":{
        "MyFunctionLambda":{
            "Type":"AWS::Serverless::Function",
            "Properties":{
                "Handler":"MyPlatformServerless::MyPlatformServerless.Lambdas.MyFunctionLambda::FunctionHandler",
                "Runtime":"dotnetcore2.1",
                "CodeUri":"",
                "Description":"Default function",
                "MemorySize":512,
                "Timeout":60,
                "Role":null,
                "EventInvokeConfig": {
                    "MaximumRetryAttempts" : 0
                }
            }
        }
    }
}

You can also specify other attributes like DestinationConfig and MaximumEventAgeInSeconds in the EventInvokeConfig object.

References: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-eventinvokeconfiguration.html

Related