Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Add region config to default.global.parameters #7841

Open
niklas-palm opened this issue Jan 22, 2025 · 5 comments
Open

Feature request: Add region config to default.global.parameters #7841

niklas-palm opened this issue Jan 22, 2025 · 5 comments
Labels
stage/needs-feedback Needs feedback from the community (are you also interested in/experiencing this?) type/feature Feature request

Comments

@niklas-palm
Copy link

Describe your idea/feature/enhancement

I wish SAM CLI would add the region configuration to default.global.parameters in the samconfig.toml.

Currently, when using sam init´ the samconfig is generated based on the user input, and the only property in default.global.parametersis the stack_name. The region parameter is added underdefault.deploy.parameterswhich means that allsam deploycommands will use the specified region. However,sam sync` uses the default region in the local installation of the AWS CLI configuration, which causes a lot of misstakes and deployments to the wrong region.

Proposal

Add region to default.global.parameters to ensure all deployment using sam sync use the same configured region.

Things to consider:

  1. Will this require any updates to the SAM Spec
    No.

Additional Details

@niklas-palm niklas-palm added stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. type/feature Feature request labels Jan 22, 2025
@roger-zhangg
Copy link
Member

Thanks for reaching out. Currently the region is read from the ~/.aws/config by default if not provided in parameter. I do think adding region in samconfig.toml could improve UX in few cases and we always welcome contribution. If you already have few thoughts in mind, feel free to draft a PR for this feature. Thank you

@niklas-palm
Copy link
Author

My pleasure.
The region is read from ~/.aws/config unless it's specified in the samconfig, which it is if the user uses --guidedwith the first deploy and stores the parameters.

During sam deploy --guided, the user is prompted with setting defaults:

	Setting default arguments for 'sam deploy'
	=========================================
	Stack Name [test]: 
	AWS Region [eu-west-1]: 
	#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
	Confirm changes before deploy [Y/n]: 
	#SAM needs permission to be able to create roles to connect to the resources in your template
	Allow SAM CLI IAM role creation [Y/n]: 
	#Preserves the state of previously provisioned resources when an operation fails
	Disable rollback [y/N]: 
	HelloWorldFunction has no authentication. Is this okay? [y/N]: y
	Save arguments to configuration file [Y/n]: 
	SAM configuration file [samconfig.toml]: 
	SAM configuration environment [default]: 

This produces the below config:

# More information about the configuration file can be found here:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
version = 0.1

[default.global.parameters]
stack_name = "test"

[default.build.parameters]
cached = true
parallel = true

[default.validate.parameters]
lint = true

[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
resolve_s3 = true
s3_prefix = "test"
region = "eu-west-1"
image_repositories = []

[default.package.parameters]
resolve_s3 = true

[default.sync.parameters]
watch = true

[default.local_start_api.parameters]
warm_containers = "EAGER"

[default.local_start_lambda.parameters]
warm_containers = "EAGER"

The region is specified in default.deploy.parameters which means all future sam deploy will use that region.
sam sync, however, will use the region specified in ~/.aws/config.

This is not a desired behaviour.

Given me limited exposure to building CLIs with Click, I can't really identify where in the source code the samconfig is generated, and where a deploy with the --guided flag would write to that it. If you point me in the right direction I can have a look.

@roger-zhangg
Copy link
Member

Thank you for explaining that distinction. I understand now that SAM CLI handles region settings differently between sync and deploy commands. For sync operations, it defaults to the region specified under [default.sync.parameters] in the configuration, like:

[default.sync.parameters]
watch = true
region = "us-east-2"

This raises an interesting design question: When a region is specified in either sync or deploy commands, should SAM CLI automatically apply that region setting to both operations? We could approach this in two ways:

  • Make it automatic: Always propagate the region setting across both sync and deploy configurations
  • Make it optional: Add a new flag like --save-param-global that would explicitly tell SAM CLI to apply the region setting globally

For automatic option, It would represent a significant change to SAM CLI's existing behavior. Currently, users can maintain different region settings for different purposes - for example:

  • Using region from samconfig.toml for deploy
  • Using region from ~/.aws/config for sync

If we automatically propagated region settings, it could lead to unexpected behavior. A user might suddenly find their sync targeting a different region than intended, simply because they performed a deploy to another region.

Therefore, I lean towards the optional approach using a flag like --save-param-global to enable this behavior.

@roger-zhangg roger-zhangg added stage/needs-feedback Needs feedback from the community (are you also interested in/experiencing this?) and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Jan 24, 2025
@niklas-palm
Copy link
Author

niklas-palm commented Jan 24, 2025

I understand what you mean but I believe there's still an avenue for the automatic approach. We don't have to change how the SAM CLI determines what region to use - i.e if region only is specified under deployment parameters, then continue using the default in the ~/.aws/config for sync commands makes sense. However, an update to the "wizard" when using sam deploy --guided could solve this by either

  1. adding the region to the sync parameters:
[default.sync.parameters]
watch = true
region = "us-east-2" <---

Or
2. moving the region parameter to the global parameters (from the deployment parameters)

[default.global.parameters]
stack_name = "test"
region = "us-east-2" <---

Neither option would impact existing deployment flows if I'm not mistaken, since we're not updating the CLIs behaviour, only the --guided "wizard" and how it persists parameters? There's no change in how those parameters are interpreted.

In general, my opinion is that choosing to persist the parameters you've specified through the "wizard" should apply equally to all parameters. As part of the "wizard" we specify stack name, which is added as a global parameter. Why should the same logic not be applied to region? The automatic approach would not impact any existing deployments as far as I understand.

@roger-zhangg
Copy link
Member

Understood, if this is the behavior coming from sam deploy --guided then we can add a notice in that step saying this region will apply to both sync and deploy, then update the region in [default.sync.parameters] if not exist. I think this flow sound good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage/needs-feedback Needs feedback from the community (are you also interested in/experiencing this?) type/feature Feature request
Projects
None yet
Development

No branches or pull requests

2 participants