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

Support !Ref of template parameters #657

Merged
merged 10 commits into from
Sep 21, 2018
Merged

Support !Ref of template parameters #657

merged 10 commits into from
Sep 21, 2018

Conversation

sanathkr
Copy link
Contributor

@sanathkr sanathkr commented Sep 7, 2018

Issue #, if available:
#573, #572, #528, #490

Description of changes:
Still very much a work in progress. This PR supports resolving !Ref of template parameters anywhere within a SAM template. It does not yet support other intrinsic functions like !Sub or !FindInMap.

  • Customers can provide values for template parameters using --parameter-overrides CLI option
  • Automatically picks up default values of parameters from template
  • Assigns sane defaults for CFN pseudo-parameters like AWS::Region. This can also be overridden using --parameter-overrides CLI option

Remaining:

  • Docstrings
  • Unit tests
  • Integration test for start-api and start-lambda
  • Linter fixes

Thanks to @jfuss for providing the initial version of implementation. This PR is a cleaned up version of Jacob's implementation.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

- Provide values for template parameters
- Automatically picks up default values
- Assigns sane defaults for CFN pseudo-parameters like AWS::Region
jfuss
jfuss previously requested changes Sep 14, 2018
samcli/cli/types.py Show resolved Hide resolved
samcli/cli/types.py Show resolved Hide resolved
@@ -85,6 +86,13 @@ def __init__(self,
Additional arguments passed to the debugger
debugger_path str
Path to the directory of the debugger to mount on Docker
aws_profile
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the type to aws_profile and aws_region.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

AWS Credential profile to use
aws_region
AWS region to use
parameter_overrides : dict
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove the : to remain consistent with the other docstrings.

@@ -103,6 +104,12 @@ def invoke_common_options(f):
type=click.Path(exists=True),
help="JSON file containing values for Lambda function's environment variables."),

click.option("--parameter-overrides",
type=CfnParameterOverridesType(),
help="Optional. A string that contains CloudFormation parameter overrides encoded as key-value "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be key=value instead of key-value? You have an example but keeping this consistent will help communicate what we are expecting.

@@ -116,3 +118,48 @@ def test_invoke_raises_exception_with_noargs_and_event(self):
process_stderr = b"".join(process.stderr.readlines()).strip()
error_output = process_stderr.decode('utf-8')
self.assertIn("no_event and event cannot be used together. Please provide only one.", error_output)

def test_invoke_with_timeout_set_by_parameters(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is almost identical to the 'test_invoke_with_timeout_set' test. Can we parameterize them into one test? This will be help as we build out the tests more to cover different cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea. will do

)

])
def test_successful_parsing(self, input, expected):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this!

tests/unit/cli/test_types.py Show resolved Hide resolved
])
def test_successful_parsing(self, input, expected):
result = self.param_type.convert(input, None, None)
print(input)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove prints from tests.

"AWS::AccountId": "123456789012",
"AWS::Partition": "aws",

# There is not much value in inferring actual AWS region here. These values are just placeholders to help
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update this comment, to reflect the AWS::Region override change.


default_values = {}

parameter_definition = sam_template.get("Parameters", None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not default this to{}?

@sanathkr sanathkr changed the title WIP: Support !Ref of template parameters Support !Ref of template parameters Sep 21, 2018
Removes wrapping double quotes and any '\ ' characters. They are usually added to preserve spaces when passing
value thru shell.

Ex:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a nit:

Making it look like in the docstring, looks nice in the editor, because it interprets it as a python prompt. https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard

Examples
----------
>>> _unquote('val\ ue')
value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I will add it..

@@ -98,6 +106,7 @@ def __init__(self,
self._debug_port = debug_port
self._debug_args = debug_args
self._debugger_path = debugger_path
self._parameter_overrides = parameter_overrides or {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We set this to be None by default everywhere else, should we do that here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is set as None everywhere else, but as a good practice I set to empty dictionary if customers don't specify any value.

@alytle
Copy link

alytle commented Sep 21, 2018

Yay for this one. Just started using SAM, and this is a blocker. Thanks for adding it!

@webwizrd
Copy link

webwizrd commented Nov 4, 2019

I have the following issue with !Ref:
My template.yaml contains 2 Lambda functions that each !Ref a globally defined parameter in their individual environments section:

Parameters:
  GlobalClientId:
    Type: String
    Description: Issued client ID

Resources:
AuthorizeFunction:
    Type: AWS::Serverless::Function
    Properties:
    (...)
   Environment:
        Variables:
          CLIENT_ID: !Ref GlobalClientId
CallbackFunction:
    Type: AWS::Serverless::Function
    Properties:
    (...)
   Environment:
        Variables:
          CLIENT_ID: !Ref GlobalClientId

When executing via sam local and --env-vars .env with the .env file being:

{
    "AuthorizeFunction": 
        {
        "CLIENT_ID":"ABCD" 
        },
   "CallbackFunction": 
        {
        "CLIENT_ID":"ABCD" 
        }
}

only the first Function receives the value in the .env file (process.env.CLIENT_ID=="ABCD").
The second Function receives the referenced parameter name as a value !? (process.env.CLIENT_ID=="GlobalClientId").

Am I using this wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants