How to deploy to different environments with AWS SAM
Asked Answered
V

1

25

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

Virgenvirgie answered 18/8, 2021 at 3:11 Comment(1)
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 under Parameters.Virgenvirgie
R
49

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>
Richert answered 18/8, 2021 at 6:45 Comment(3)
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: <qa|prod>-function-nameOdin
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.Richert
amazing! worked pretty goodParesthesia

© 2022 - 2024 — McMap. All rights reserved.