Rakesh Kumar
Rakesh Kumar

Reputation: 3149

Can we declare stage name inside the template.serverless file?

We are creating AWS Serverless Lambda function using .NET Core. When we deploy this lambda function it added automatically "Prod" suffix in the url. But we want change it to "dev". Can we declare stage name inside the serverless.template file?

Here is my serverless.template file:

{
  "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" : {

  }
}

We are using AWS Toolkit for visual studio 2017 to deploy aws serverless lambda.("https://aws.amazon.com/blogs/developer/preview-of-the-aws-toolkit-for-visual-studio-2017")

Upvotes: 2

Views: 1337

Answers (1)

Daniel Leach
Daniel Leach

Reputation: 7375

The only way I could find to make this work is the specify a AWS::Serverless::Api to use. The following example sets both the StageName and the ASPNETCORE_ENVIRONMENT to the EnvironmentName parameter.

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

Upvotes: 3

Related Questions