20

I have two questions about AWS SAM and deployments.

I’m reading through the docs and checking through examples like this and I’m still not quite sure how to deploy to a staging and production environment separately with my SAM template. Is it as simple as deploying a new stack with a new name like sam deploy —stack-name my-app-staging and sam deploy —stack-name my-app-production?

In the following example, I have one question. If my SAM template contains a Parameters with the name MyEnvironment that has three possible values, how does the deploy know which value of the three to use when deploying the stack? Like how would I say to use the value staging or production? Is that something that will automatically be asked of me when I deploy or is it something I have to provide on the CLI?

enter image description here

Petesta
  • 1,623
  • 3
  • 19
  • 29
  • It looks like for my second question, calling`—guided` would cause the user to be prompted to provide values for each parameter declared in the `Parameters` section. See [here](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy.html) under `Parameters`. – Petesta Aug 18 '21 at 03:30

1 Answers1

37

You can use the samconfig.toml file in order to determine how the stack should be deployed to different environments.

For example:

version = 0.1

[qa.deploy.parameters]
stack_name = "my-qa-stack"
s3_bucket = "XXXXX-qa"
s3_prefix = "XXXXX/qa"
region = "eu-west-1"
capabilities = "CAPABILITY_IAM"
parameter_overrides = "Environment=qa"

[prod.deploy.parameters]
stack_name = "my-prod-stack"
s3_bucket = "XXXXX-prod"
s3_prefix = "XXXXX/prod"
region = "eu-west-1"
capabilities = "CAPABILITY_IAM"
parameter_overrides = "Environment=prod"

You can then pass the required config environment to the command:

sam deploy --config-env <qa|prod>
stijndepestel
  • 3,076
  • 2
  • 18
  • 22
  • 2
    is there a way to use this variable in the template.yaml file? I want to use it in the function name; like this => `Resources: TestFunction: Type: AWS::Serverless::Function Properties: FunctionName: -function-name` – user3807691 Jan 12 '22 at 12:11
  • 7
    The `parameter_overrides` property sets the parameter values for the deployment of the stack. In the example above there is an `Environment` parameter. So if you define that parameter in your yaml stack, you can use `!Sub ${Environment}-function-name`. – stijndepestel Jan 12 '22 at 14:25
  • amazing! worked pretty good – michaelitoh Mar 01 '23 at 18:05