【问题标题】:Can we declare stage name inside the template.serverless file?我们可以在 template.serverless 文件中声明舞台名称吗?
【发布时间】:2018-07-26 06:32:18
【问题描述】:

我们正在使用 .NET Core 创建 AWS 无服务器 Lambda 函数。当我们部署这个 lambda 函数时,它会在 url 中自动添加“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 serverless lambda。("https://aws.amazon.com/blogs/developer/preview-of-the-aws-toolkit-for-visual-studio-2017")

【问题讨论】:

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


    【解决方案1】:

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

    无服务器模板:

    {
        "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}/" }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多