我们可以在template.serverless文件中声明阶段名称吗?

时间:2018-02-15 13:20:56

标签: asp.net-core .net-core aws-lambda

我们正在使用.NET Core创建AWS Serverless Lambda函数。当我们部署这个lambda函数时,它会自动添加" Prod"网址后缀。但我们希望将其改为" dev"。我们可以在serverless.template文件中声明阶段名吗?

这是我的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.",

  "Parameters" : {

  },

  "Conditions" : {

  },

  "Resources" : {

"Get" : {
  "Type" : "AWS::Serverless::Function",
  "Properties": {
    "Handler": "F2C.MAP.API.AWSLambda.PublicAPI::F2C.MAP.API.AWSLambda.PublicAPI.LambdaEntryPoint::FunctionHandlerAsync",
    "Runtime": "dotnetcore2.0",
    "CodeUri": "",
    "MemorySize": 256,
    "Timeout": 30,
    "Role": null,
    "Policies": [ "AWSLambdaFullAccess" ],
    "Environment" : {
      "Variables" : {

      }
    },
    "Events": {
      "PutResource": {
        "Type": "Api",
        "Properties": {
          "Path": "/{proxy+}",
          "Method": "GET"
        }
      }
    }
  }
},
"POST" : {
  "Type" : "AWS::Serverless::Function",
  "Properties": {
    "Handler": "F2C.MAP.API.AWSLambda.PublicAPI::F2C.MAP.API.AWSLambda.PublicAPI.LambdaEntryPoint::FunctionHandlerAsync",
    "Runtime": "dotnetcore2.0",
    "CodeUri": "",
    "MemorySize": 256,
    "Timeout": 30,
    "Role": null,
    "Policies": [ "AWSLambdaFullAccess" ],
    "Environment" : {
      "Variables" : {

      }
    },
    "Events": {
      "PutResource": {
        "Type": "Api",
        "Properties": {
          "Path": "/{proxy+}",
          "Method": "POST"
        }
      }
    }
  }
}
  },
 "Outputs" : {

  }
}

我们正在使用AWS Toolkit for Visual Studio 2017来部署aws无服务器lambda。(" https://aws.amazon.com/blogs/developer/preview-of-the-aws-toolkit-for-visual-studio-2017")

1 个答案:

答案 0 :(得分:2)

我能找到的唯一方法是指定要使用的AWS :: Serverless :: Api。以下示例将StageName和ASPNETCORE_ENVIRONMENT设置为EnvironmentName参数。

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.",
    "Parameters" : {
        "EnvironmentName" : {
            "Type" : "String",
            "Description" : "Sets the ASPNETCORE_ENVIRONMENT variable as well as the API's StageName to this.",
            "MinLength" : "0"
        }
    },
    "Resources" : {

        "ProxyFunction" : {
        "Type" : "AWS::Serverless::Function",
        "Properties": {
            "Handler": "PeopleGateway::PeopleGateway.LambdaEntryPoint::FunctionHandlerAsync",
            "Runtime": "dotnetcore2.0",
            "CodeUri": "",
            "MemorySize": 256,
            "Timeout": 30,
            "Role": null,
            "Policies": [ "AWSLambdaFullAccess", "AWSLambdaVPCAccessExecutionRole" ],
            "Environment" : {
            "Variables" : {
                "ASPNETCORE_ENVIRONMENT": { "Ref" : "EnvironmentName" }
            }
            },
            "Events": {
            "PutResource": {
                "Type": "Api",
                "Properties": {
                "Path": "/{proxy+}",
                "Method": "ANY",
                "RestApiId": { "Ref": "APIGateway" }
                }
            }
            }
        }
        },

        "APIGateway": {
            "Type" : "AWS::Serverless::Api",
            "Properties": {
            "StageName": { "Ref" : "EnvironmentName" },
            "DefinitionBody": {
                "swagger": "2.0",
                "info": {
                "title": {
                    "Ref": "AWS::StackName"
                }
                },
                "paths": {
                "/{proxy+}": {
                    "x-amazon-apigateway-any-method": {
                    "x-amazon-apigateway-integration": {
                        "httpMethod": "POST",
                        "type": "aws_proxy",
                        "uri": {
                        "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ProxyFunction.Arn}/invocations"
                        }
                    },
                    "responses": {}
                    }
                }
                }
            }
            }
        }
    },

    "Outputs" : {
        "ApiURL" : {
            "Description" : "API endpoint URL for the specified environment",
            "Value" : { "Fn::Sub" : "https://${APIGateway}.execute-api.${AWS::Region}.amazonaws.com/${EnvironmentName}/" }
        }
    }
}