aws / aws/aws-sam-cli

Add support for `global` in the env position of samconfig.toml files

Open
#3,010 7 comments 9 reactions 0 assignees View on GitHub
area/sam-config maintainer/need-followup stage/pm-review type/feature
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

### Describe your idea/feature/enhancement

I wish SAM CLI would support a `samconfig.toml` file that requires less repetition across env. I believe it could do that leveraging its existing notion of `global` put into the env position of samconfig.toml tables

### Proposal

Today `samconfig.toml` files supports a notion of a `default` env and `global` command context and a section

As mentioned under [the precedence rules documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html#rules-precedence), you can leverage a technique to reduce repetition on a per env basis across different commands for that env using the `global` alias in the command context position.

```toml
[prod.global.parameters]
stack_name = "common-stack"

[prod.deploy.parameters]
stack_name = "my-app-stack"
```

However, you can not leverage this to reduce repetition _across_ envs.

A common case I have is a separate deployment table to support multiple deployment envs

```toml
version = 1

[dev-us-east-1.deploy.params]
stack_name = "xxx-dev"
s3_bucket = "sam-artifacts-xxx-us-east-1"
region = "us-east-1"
fail_on_empty_changeset = false
capabilities = "CAPABILITY_IAM"

[prod-us-east-1.deploy.params]
stack_name = "xxx-prod"
s3_bucket = "sam-artifacts-xxx-us-east-1"
region = "us-east-1"
fail_on_empty_changeset = false
capabilities = "CAPABILITY_IAM"

[dev-us-west-2.deploy.params]
stack_name = "xxx-dev"
s3_bucket = "sam-artifacts-xxx-us-east-1"
region = "us-west-2"
fail_on_empty_changeset = false
capabilities = "CAPABILITY_IAM"

[prod-us-west-2.deploy.params]
stack_name = "xxx-prod"
s3_bucket = "sam-artifacts-xxx-us-east-1"
region = "us-west-2"
fail_on_empty_changeset = false
capabilities = "CAPABILITY_IAM"
```

You'll notice about that I'm repeating `s3_bucket`, `fail_on_empty_changeset`, `capabilities` in each table.

What I'd like instead is to leverage the existing semantics of `global` but in the env context

```toml
version = 1

[global.deploy.params]
s3_bucket = "sam-artifacts-xxx-us-east-1"
fail_on_empty_changeset = false
capabilities = "CAPABILITY_IAM"

[dev-us-east-1.deploy.params]
stack_name = "xxx-dev"
region = "us-east-1"

[prod-us-east-1.deploy.params]
stack_name = "xxx-prod"
region = "us-east-1"

[dev-us-west-2.deploy.params]
stack_name = "xxx-dev"
region = "us-west-2"

[prod-us-west-2.deploy.params]
stack_name = "xxx-prod"
region = "us-west-2"
```

Notice how there is no repeating values that are the same but don't change across envs making it easier to add new deployment envs and fewer places to change should the common parameters change

I believe this would be a simple addition [here](https://github.com/aws/aws-sam-cli/blob/1ec81ce5133acc0ef70482eb96d3d38778b395fa/samcli/lib/config/samconfig.py#L75-L81) in addition to documentation and would be leveraging existing semantics. `global` is an alias for all command contexts in the command context position and sports overrides for specific command context.

`global` in this proposal can mean all envs in the env position and support overrides when a specific config env is defined.

This also harmonizes with the idea of [global in templates themselves](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy-globals.html), a top level set of defaults

Things to consider:
1. Will this require any updates to the [SAM Spec](https://github.com/awslabs/serverless-application-model) I don't believe so.

### Additional Details

Thank you for making sam cli the great product that it is today. This change would make my life and hopefully the lives of others with more than one deployment target even better.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.