我正在使用适用于 VS 2019 的 AWS Toolkit,并且正在尝试部署一个新的无服务器应用程序。我使用 CLI 和 serverless.template 文件部署新数据库,如下所示:
"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"
}
}
}
}
使用 CLI 命令:aws cloudformation deploy --template-file serverless.template --stack-name LBTServerlessApp
数据库实例创建良好,然后我可以在 MSSMS 中访问以向其部署数据和 SP。
然后,我尝试使用以下 serverless.template 文件从我的第二个项目部署 Lambda:
{
"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/"
}
}
}
}
和 aws-lambda-tools-defaults.json 文件:
{
"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"
}
使用 CLI 命令:dotnet lambda deploy-serverless -cfg aws-lambda-tools-defaults.json -pcfg true(也可以通过右键单击 + 发布到 AWS Lambda 获得相同的结果)
两者都部署到相同的 Cloud Formation 堆栈和相同的存储桶。Lambda 和 API 端点都在创建中,但是,第二次部署删除了数据库和存储桶!所以我只剩下 API Gateway 端点和 Lamdba,但没有数据库来运行调用!?我已经尝试了 IO 能想到的所有方法,包括存储桶版本控制,但数据库总是被删除。任何人都可以解释为什么会这样吗?