0
votes

I have created a serverless .Net core API. Lambda function is created using a console. Now I want to use the same lambda when publishing. But cloud formation is creating a new lambda function when deploying. Is it possible to define the same lambda function in cloud formation template?

Here is the template used for publishing

`{
  "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": {
"testapi": {
  "Type": "AWS::Serverless::Function", 
  "Properties": {
    "FunctionName" : "arn:aws:lambda:ap-south- 
 1:721092682477:function:testapi",
        "Handler": "Syngenta.Global.TPTool.WebApi::Syngenta.Global.TPTool.WebApi.LambdaEntryPoint 
::FunctionHandlerAsync",
        "Runtime": "dotnetcore2.1",
        "CodeUri": "",
        "MemorySize": 512,
        "Timeout": 30,
        "Role": null,
        "Policies": [
          "AWSLambdaFullAccess"
        ],
        "Environment": {
          "Variables": {
          }
        },
        "Events": {
          "ProxyResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/{proxy+}",
              "Method": "ANY"
            }
          }
        }
      }
    }
  }
}`

here testapi is the lambda function which is already created using AWS console. when publishing getting the error "function name already exists". And if name is changed then new lambda function is created.

1
What are you using your lambda for ? Provide some code of what you want to do with the lambda by reusing in a cloudformation stack.iwaduarte
I am trying to publish the code to lambda function using VS studio AWS toolkit.Shahbaz

1 Answers

0
votes

There several approaches when importing an existing resource to a cloudformation stack. You could either delete your function manually and run your cloudformation file again so it will create an entire new function and from there you should not get errors anymore.

Or if that it is not possible. Following the documentation guide you should add the property: "DeletionPolicy": "Retain" to your resource.

Example:

"testapi": {
  "Type": "AWS::Serverless::Function", 
  "DeletionPolicy": "Retain",
... }

There's also other ways of importing existing resources to the a specific cloudformation stack (i.e via console or cli). You could find more information here:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resource-import-existing-stack.html