'How to deploy to multiple envorionments (develop, release, production) with AWS SAM?

I have an AWS SAM template where I have a Lambda with an API Gateway and I want to be able to deploy that lambda to different environments such as develop, release, and production as any other project so that everything can be tested well before reaching the final end-user.

So far I have tried creating a parameter in my template which is used to change the environment on deploy and the first time lambda got deployed with its name-develop but when I tried to deploy to release it just substituted name-develop with name-release instead of having both lambdas coexisting.

Here is my template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  ocr-app

  Sample SAM Template for ocr-app
  
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 15

Parameters:
  Env:
    Type: String
    AllowedValues:
      - develop
      - release
      - product
    # Default: develop
    Description: Environment in which the application will be deployed. Allowed values [develop, release, product]


Resources:
  OCRFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      Environment:
          Variables:
            ENV: !Ref Env
      CodeUri: ocr/
      Handler: app.lambdaHandler
      FunctionName: !Sub OCRFunction_${Env}
      Runtime: nodejs14.x
      Policies: 
        - S3ReadPolicy: # Managed Policy
            BucketName: "*"
        - AWSLambdaExecute # Managed Policy
      Architectures:
        - x86_64
      Events:
        OCR:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /ocr
            Method: get

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  OCRApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  OCRFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt OCRFunction.Arn
  OCRFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt OCRFunctionRole.Arn

How do I deploy to multiple environments?



Solution 1:[1]

One way I found out so far is creating another stack - so a stack per environment. I am not sure if this is the only/best way so I am open for other opinions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Angel Hadzhiev