Search code examples
amazon-web-servicesamazon-s3visual-studio-2019aws-cliaws-toolkit

AWS Toolkit for VS 2019 deleting database instance when run?


I am using the AWS Toolkit for VS 2019 and I'm trying to deploy a new serverless app. I deploy the new database using the CLI and a serverless.template file as below:

  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Description": "Testing AWS setup in Visual Studio.",
  "Resources": {
        "ConfigBucket" : {
            "Type" : "AWS::S3::Bucket",
            "Properties" : {
                "BucketName" : "a-test-bucket"
            }
        }, 
        "Database" : {
            "Type" : "AWS::RDS::DBInstance",
            "Properties" : {
                "DBInstanceClass" : "db.t2.micro", 
                "EngineVersion": "14.00.3294.2.v1",
                "AllocatedStorage" : "20",
                "Engine" : "sqlserver-ex",
                "PubliclyAccessible" : true,
                "MasterUsername" : "myUsername",
                "MasterUserPassword" : "myPassword"
            }
        }
  }

}

Using CLI command: aws cloudformation deploy --template-file serverless.template --stack-name LBTServerlessApp

The database instance creates fine and I can then access in MSSMS to deploy data and SPs to it.

I then try to deploy the Lambda from my second project using the following serverless.template file:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Description": "An AWS Serverless Application.",
  "Resources": {
    "Get": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "LBTServerlessAppTest::LBTServerlessAppTest.GetUsersFunction::GetUser",
        "Runtime": "dotnetcore3.1",
        "CodeUri": "",
        "MemorySize": 256,
        "Timeout": 30,
        "Role": null,
        "Policies": [
          "AWSLambdaBasicExecutionRole"
        ],
        "Events": {
          "RootGet": {
            "Type": "Api",
            "Properties": {
              "Path": "/GetUser",
              "Method": "GET"
            }
          }
        }
      }
    }
  },
  "Outputs": {
    "ApiURL": {
      "Description": "API endpoint URL for DEV environment",
      "Value": {
        "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/DEV/"
      }
    }
  }
}

And aws-lambda-tools-defaults.json file:


{
    "Information" : [
        "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
        "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
        "dotnet lambda help",
        "All the command line options for the Lambda command can be specified in this file."
    ],
    "profile"     : "default",
    "region"      : "eu-west-2",
    "configuration" : "Release",
    "framework"     : "netcoreapp3.1",
    "s3-prefix"     : "LBTServerlessApp/",
    "template"      : "serverless.template",
    "template-parameters" : "",
    "s3-bucket"           : "a-test-bucket",
    "stack-name"          : "LBTServerlessApp"
}

using the CLI command: dotnet lambda deploy-serverless -cfg aws-lambda-tools-defaults.json -pcfg true (Also get the same result by right clicking + publish to AWS Lambda)

Both are deploying to the same Cloud Formation stack and same bucket. Both Lambda and API Endpoints are being created, however, the second deploy deletes the database and the bucket! So I am left with the API Gateway endpoint and Lamdba but no database to run the calls against!? I've tried everything IO can think of including bucket versioning but the database is always being deleted. Can anyone shed any light on why this is happening?


Solution

  • Schoolboy error! I was naming the stack-name the same in both serverless.template files. In order for this to work you need 2 separate Cloud Formation stacks.