aws / aws/aws-sam-cli

Default parameter value ignored when parameter has previously been overridden

Open
#4,180 6 comments 2 reactions 0 assignees View on GitHub
area/deploy area/docs maintainer/need-followup type/documentation
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

### Description:

If I were to deploy a SAM template to AWS while overriding a default parameter value, and then re-deploy the same template without overriding that parameter value, the default parameter value specified in the template is ignored and the previous override value is still used. This results in an error because the changeset says there are no changes to deploy.

It is possible to explicitly override the parameter value again, but it's not obvious that this is required, and if the change was part of a larger change this unexpected behaviour would likely be missed.

### Steps to reproduce:

Using the template and function below, build and deploy the SAM application.

In this case, a `schedule` event on a function creates an EventBridge rule, and the value of the `State` property is set by referencing the `ScheduleState` parameter. The default for the parameter value is `DISABLED`.

Using the steps below you can see that after the `ScheduleState` parameter is overridden when deploying the SAM application, the same value is unexpectedly used when re-deploying without overriding the `ScheduleState` parameter, rather than the default parameter value.

```shell
foo@bar:~$ sam build -u
foo@bar:~$ sam deploy --parameter-overrides ScheduleState=ENABLED
# EventBridge rule is enabled.
foo@bar:~$ sam deploy
# EventBridge rule is still enabled, default value of ScheduleState is not used.
foo@bar:~$ sam deploy --parameter-overrides ScheduleState=DISABLED
# EventBridge rule is disabled.
```

template.yml

```yml
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Parameters:
Schedule:
Description: The schedule on which the Function runs.
Type: String
Default: cron(0 3 1 * ? *)
ScheduleState:
Description: Whether the OrchestratorFunction is enabled.
Type: String
AllowedValues: [ ENABLED, DISABLED ]
Default: DISABLED

Globals:
Function:
Architectures:
- x86_64
Handler: app.lambda_handler
Runtime: python3.8
Timeout: 1

Resources:
Function:
Type: AWS::Serverless::Function
Properties:
CodeUri: code/.
Events:
ScheduleEvent:
Type: Schedule
Properties:
State: !Ref ScheduleState
Schedule: !Ref Schedule
RetryPolicy:
MaximumEventAgeInSeconds: 100
MaximumRetryAttempts: 3

```

samconfig.toml

```python
version = 0.1

[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
```

code/app.py

```python
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': 'OK'
}
```

### Observed result:

Full logs for sam build -u --debug

```console
2022-09-01 11:02:00,647 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-09-01 11:02:00,648 | Using config file: samconfig.toml, config environment: default
2022-09-01 11:02:00,648 | Expand command line arguments to:
2022-09-01 11:02:00,648 | --template_file=/home/me/sam-test/template.yaml --use_container --build_dir=.aws-sam/build --cache_dir=.aws-sam/cache
2022-09-01 11:02:00,763 | 'build' command is called
2022-09-01 11:02:00,763 | Starting Build inside a container
2022-09-01 11:02:00,767 | Collected default values for parameters: {'Schedule': 'cron(0 3 1 * ? *)', 'ScheduleState': 'DISABLED'}
2022-09-01 11:02:00,781 | There is no customer defined id or cdk path defined for resource Function, so we will use the resource logical id as the resource id
2022-09-01 11:02:00,781 | 0 stacks found in the template
2022-09-01 11:02:00,781 | Collected default values for parameters: {'Schedule': 'cron(0 3 1 * ? *)', 'ScheduleState': 'DISABLED'}
2022-09-01 11:02:00,796 | There is no customer defined id or cdk path defined for resource Function, so we will use the resource logical id as the resource id
2022-09-01 11:02:00,797 | 1 resources found in the stack
2022-09-01 11:02:00,797 | Found Serverless function with name='Function' and CodeUri='code/.'
2022-09-01 11:02:00,797 | --base-dir is not presented, adjusting uri code/. relative to /home/me/sam-test/template.yaml
2022-09-01 11:02:00,805 | Instantiating build definitions
2022-09-01 11:02:00,810 | Same function build definition found, adding function (Previous: BuildDefinition(python3.8, /home/me/sam-test/code, Zip, , c13a826a-ef02-441f-aebd-392f5fd78509, {}, {}, x86_64, []), Current: BuildDefinition(python3.8, /home/me/sam-test/code, Zip, , 9a7b9bb3-a887-4f0a-985a-2905137ff296, {}, {}, x86_64, []), Function: Function(function_id='Function', name='Function', functionname='Function', runtime='python3.8', memory=None, timeout=1, handler='app.lambda_handler', imageuri=None, packagetype='Zip', imageconfig=None, codeuri='/home/me/sam-test/code', environment=None, rolearn=None, layers=[], events={'ScheduleEvent': {'Type': 'Schedule', 'Properties': {'State': 'DISABLED', 'Schedule': 'cron(0 3 1 * ? *)', 'RetryPolicy': {'MaximumEventAgeInSeconds': 100, 'MaximumRetryAttempts': 3}}}}, metadata={'SamResourceId': 'Function'}, inlinecode=None, codesign_config_arn=None, architectures=['x86_64'], function_url_config=None, stack_path=''))
2022-09-01 11:02:00,811 | Building codeuri: /home/me/sam-test/code runtime: python3.8 metadata: {} architecture: x86_64 functions: Function
2022-09-01 11:02:00,812 | Building to following folder /home/me/sam-test/.aws-sam/build/Function

Fetching public.ecr.aws/sam/build-python3.8:latest-x86_64 Docker container image......
2022-09-01 11:02:02,139 | Mounting /home/me/sam-test/code as /tmp/samcli/source:ro,delegated inside runtime container
Using the request object from command line argument
Loading workflow module 'aws_lambda_builders.workflows'
Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
Registering workflow 'CustomMakeBuilder' with capability 'Capability(language='provided', dependency_manager=None, application_framework=None)'
Registering workflow 'NodejsNpmEsbuildBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm-esbuild', application_framework=None)'
Found workflow 'PythonPipBuilder' to support capabilities 'Capability(language='python', dependency_manager='pip', application_framework=None)'
requirements.txt file not found. Continuing the build without dependencies.
Running workflow 'PythonPipBuilder'
Running PythonPipBuilder:CopySource
Creating target folders at /tmp/samcli/artifacts
Copying directory metadata from source (/tmp/samcli/source) to destination (/tmp/samcli/artifacts)
Copying source file (/tmp/samcli/source/app.py) to destination (/tmp/samcli/artifacts/app.py)
PythonPipBuilder:CopySource succeeded
2022-09-01 11:02:02,685 | Build inside container returned response {"jsonrpc": "2.0", "id": 1, "result": {"artifacts_dir": "/tmp/samcli/artifacts"}}
2022-09-01 11:02:02,685 | Build inside container was successful. Copying artifacts from container to host
2022-09-01 11:02:02,988 | Copying from container: /tmp/samcli/artifacts/. -> /home/me/sam-test/.aws-sam/build/Function
2022-09-01 11:02:03,042 | Build inside container succeeded
2022-09-01 11:02:03,043 | There is no customer defined id or cdk path defined for resource Function, so we will use the resource logical id as the resource id

Build Succeeded

Built Artifacts : .aws-sam/build
Built Template : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
[*] Deploy: sam deploy --guided
```

Full logs for sam deploy --parameter-overrides ScheduleState=ENABLED --debug

```console
2022-09-01 11:02:41,421 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-09-01 11:02:41,421 | Using config file: samconfig.toml, config environment: default
2022-09-01 11:02:41,421 | Expand command line arguments to:
2022-09-01 11:02:41,421 | --template_file=/home/me/sam-test/.aws-sam/build/template.yaml --parameter_overrides={'ScheduleState': 'ENABLED'} --fail_on_empty_changeset --confirm_changeset --on_failure=ROLLBACK --stack_name=Test-Schedule-Disabled --s3_bucket=my-deploy-us-east-1 --s3_prefix=test-schedule-disabled --capabilities=['CAPABILITY_IAM']
2022-09-01 11:02:41,651 | Collected default values for parameters: {'Schedule': 'cron(0 3 1 * ? *)', 'ScheduleState': 'DISABLED'}
2022-09-01 11:02:41,667 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:02:41,668 | 0 stacks found in the template
2022-09-01 11:02:41,720 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:02:41,720 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:02:42,952 | File with same data already exists at test-schedule-disabled/da7c97b42276d6d18396784863e4fab3, skipping upload

Deploying with following values
===============================
Stack name : Test-Schedule-Disabled
Region : us-east-1
Confirm changeset : True
Disable rollback : False
Deployment s3 bucket : my-deploy-us-east-1
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {"ScheduleState": "ENABLED"}
Signing Profiles : {}

Initiating deployment
=====================
2022-09-01 11:02:42,979 | Collected default values for parameters: {'Schedule': 'cron(0 3 1 * ? *)', 'ScheduleState': 'DISABLED'}
2022-09-01 11:02:42,991 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:02:42,991 | 0 stacks found in the template
2022-09-01 11:02:42,991 | Collected default values for parameters: {'Schedule': 'cron(0 3 1 * ? *)', 'ScheduleState': 'DISABLED'}
2022-09-01 11:02:43,003 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:02:43,003 | 1 resources found in the stack
2022-09-01 11:02:43,515 | Stack with id Test-Schedule-Disabled does not exist
2022-09-01 11:02:44,019 | File with same data already exists at test-schedule-disabled/549dab306686cd314397228a46f73b27.template, skipping upload

Waiting for changeset to be created..
CloudFormation stack changeset
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Operation LogicalResourceId ResourceType Replacement
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Add FunctionRole AWS::IAM::Role N/A
+ Add FunctionScheduleEventPermission AWS::Lambda::Permission N/A
+ Add FunctionScheduleEvent AWS::Events::Rule N/A
+ Add Function AWS::Lambda::Function N/A
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Changeset created successfully. arn:aws:cloudformation:us-east-1:402307313821:changeSet/samcli-deploy1662026563/c2a37581-5dc9-49a9-973e-9455ae41b2f7

Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y

2022-09-01 11:03:03 - Waiting for stack create/update to complete

CloudFormation events from stack operations (refresh every 0.5 seconds)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus ResourceType LogicalResourceId ResourceStatusReason
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE_COMPLETE AWS::IAM::Role FunctionRole -
CREATE_IN_PROGRESS AWS::Lambda::Function Function -
CREATE_IN_PROGRESS AWS::Lambda::Function Function Resource creation Initiated
CREATE_COMPLETE AWS::Lambda::Function Function -
CREATE_IN_PROGRESS AWS::Events::Rule FunctionScheduleEvent -
CREATE_IN_PROGRESS AWS::Events::Rule FunctionScheduleEvent Resource creation Initiated
CREATE_COMPLETE AWS::Events::Rule FunctionScheduleEvent -
CREATE_IN_PROGRESS AWS::Lambda::Permission FunctionScheduleEventPermission -
CREATE_IN_PROGRESS AWS::Lambda::Permission FunctionScheduleEventPermission Resource creation Initiated
CREATE_COMPLETE AWS::Lambda::Permission FunctionScheduleEventPermission -
CREATE_COMPLETE AWS::CloudFormation::Stack Test-Schedule-Disabled -
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Successfully created/updated stack - Test-Schedule-Disabled in us-east-1
```

Full logs for sam deploy --debug

```console
2022-09-01 11:07:47,883 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-09-01 11:07:47,883 | Using config file: samconfig.toml, config environment: default
2022-09-01 11:07:47,883 | Expand command line arguments to:
2022-09-01 11:07:47,883 | --template_file=/home/me/sam-test/.aws-sam/build/template.yaml --fail_on_empty_changeset --confirm_changeset --on_failure=ROLLBACK --stack_name=Test-Schedule-Disabled --s3_bucket=my-deploy-us-east-1 --s3_prefix=test-schedule-disabled --capabilities=['CAPABILITY_IAM']
2022-09-01 11:07:48,135 | Collected default values for parameters: {'Schedule': 'cron(0 3 1 * ? *)', 'ScheduleState': 'DISABLED'}
2022-09-01 11:07:48,148 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:07:48,149 | 0 stacks found in the template
2022-09-01 11:07:48,194 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:07:48,194 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:07:50,777 | File with same data already exists at test-schedule-disabled/da7c97b42276d6d18396784863e4fab3, skipping upload

Deploying with following values
===============================
Stack name : Test-Schedule-Disabled
Region : us-east-1
Confirm changeset : True
Disable rollback : False
Deployment s3 bucket : my-deploy-us-east-1
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {}
Signing Profiles : {}

Initiating deployment
=====================
2022-09-01 11:07:50,800 | Collected default values for parameters: {'Schedule': 'cron(0 3 1 * ? *)', 'ScheduleState': 'DISABLED'}
2022-09-01 11:07:50,813 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:07:50,813 | 0 stacks found in the template
2022-09-01 11:07:50,813 | Collected default values for parameters: {'Schedule': 'cron(0 3 1 * ? *)', 'ScheduleState': 'DISABLED'}
2022-09-01 11:07:50,826 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:07:50,826 | 1 resources found in the stack
2022-09-01 11:07:52,088 | File with same data already exists at test-schedule-disabled/549dab306686cd314397228a46f73b27.template, skipping upload

Waiting for changeset to be created..
Error: No changes to deploy. Stack Test-Schedule-Disabled is up to date
```

Full logs for sam deploy --parameter-overrides ScheduleState=DISABLED --debug

```console
2022-09-01 11:08:23,729 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-09-01 11:08:23,729 | Using config file: samconfig.toml, config environment: default
2022-09-01 11:08:23,729 | Expand command line arguments to:
2022-09-01 11:08:23,729 | --template_file=/home/me/sam-test/.aws-sam/build/template.yaml --parameter_overrides={'ScheduleState': 'DISABLED'} --fail_on_empty_changeset --confirm_changeset --on_failure=ROLLBACK --stack_name=Test-Schedule-Disabled --s3_bucket=my-deploy-us-east-1 --s3_prefix=test-schedule-disabled --capabilities=['CAPABILITY_IAM']
2022-09-01 11:08:23,975 | Collected default values for parameters: {'Schedule': 'cron(0 3 1 * ? *)', 'ScheduleState': 'DISABLED'}
2022-09-01 11:08:23,989 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:08:23,990 | 0 stacks found in the template
2022-09-01 11:08:24,032 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:08:24,033 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:08:25,137 | File with same data already exists at test-schedule-disabled/da7c97b42276d6d18396784863e4fab3, skipping upload

Deploying with following values
===============================
Stack name : Test-Schedule-Disabled
Region : us-east-1
Confirm changeset : True
Disable rollback : False
Deployment s3 bucket : my-deploy-us-east-1
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {"ScheduleState": "DISABLED"}
Signing Profiles : {}

Initiating deployment
=====================
2022-09-01 11:08:25,161 | Collected default values for parameters: {'Schedule': 'cron(0 3 1 * ? *)', 'ScheduleState': 'DISABLED'}
2022-09-01 11:08:25,172 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:08:25,172 | 0 stacks found in the template
2022-09-01 11:08:25,172 | Collected default values for parameters: {'Schedule': 'cron(0 3 1 * ? *)', 'ScheduleState': 'DISABLED'}
2022-09-01 11:08:25,183 | Sam customer defined id is more priority than other IDs. Customer defined id for resource Function is Function
2022-09-01 11:08:25,184 | 1 resources found in the stack
2022-09-01 11:08:26,511 | File with same data already exists at test-schedule-disabled/549dab306686cd314397228a46f73b27.template, skipping upload

Waiting for changeset to be created..
CloudFormation stack changeset
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Operation LogicalResourceId ResourceType Replacement
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Modify FunctionScheduleEventPermission AWS::Lambda::Permission Conditional
* Modify FunctionScheduleEvent AWS::Events::Rule False
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Changeset created successfully. arn:aws:cloudformation:us-east-1:402307313821:changeSet/samcli-deploy1662026905/6d706a62-af69-41b0-a315-28357438525d

Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y

2022-09-01 11:10:51 - Waiting for stack create/update to complete

CloudFormation events from stack operations (refresh every 0.5 seconds)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus ResourceType LogicalResourceId ResourceStatusReason
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE_COMPLETE AWS::Events::Rule FunctionScheduleEvent -
UPDATE_COMPLETE_CLEANUP_IN_PROGRESS AWS::CloudFormation::Stack Test-Schedule-Disabled -
UPDATE_COMPLETE AWS::CloudFormation::Stack Test-Schedule-Disabled -
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Successfully created/updated stack - Test-Schedule-Disabled in us-east-1
```

### Expected result:

I would expect the default value of the `ScheduleState` parameter to be used when deploying the SAM application without explicitly overriding that parameter, rather then the previous override value that has been set.

Basically, the observed behaviour is inconsistent, because upon deployment of the SAM application the user wouldn't necessarily know which of the following scenarios is true. Either...
- The default parameter value would not have been previously overridden so that value will be used.
- The default parameter value has been overridden, so that value will be used and may differ from the default value.

### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

1. OS: Ubuntu 22.04
2. SAM version: 1.55.0
3. AWS region: us-east-1

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.