Feature request: !Sub (and other fns) in Application.Location
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 1.2k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 52
Description
### Describe your idea/feature/enhancement
`AWS::Serverless::Application`'s post-build `Location` property should support string functions like `Fn::Sub` and similar.
My project's current workflow tackles "build-once" cross-region deployment by:
- Cross-region replicating packaged SAM artifacts to S3 buckets in multiple regions (for e.g. `my-bucket-us-east-1` to `my-bucket-eu-west-1`, etc)
- **Post-processing** the SAM output template to parameterize the deployment bucket name, for resources like `AWS::Serverless::LayerVersion` and `AWS::Serverless::Function`
In this way, `sam build` / `sam package` can be run against a single region/environment, and with a slight template tweak the same assets can be deployed in any region where we have a CRR'd bucket set up.
Today I tried to extend this for our first nested `AWS::Serverless::Application`, but found that we can't `Fn::Sub` on the `Location` property - because SAM expects it to **either** be a string URL **or** an object [referencing the SAM application registry](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-nested-applications.html#serverless-sam-template-nested-applications-how-to-serverlessrepo).
For e.g. the following resource:
```json
{
"Type": "AWS::Serverless::Application",
"Properties": {
"Location": {
"Fn::Sub": "https://s3.${AWS::Region}.amazonaws.com/my-bucket-${AWS::Region}/sam/example.template"
}
},
"Metadata": {
"SamResourceId": "MyCoolResource"
}
}
```
...fails because the validator misinterprets the object `Fn:...` syntax as an application registry specifier, with the following error:
```
Error: [InvalidResourceException('MyCoolResource', 'Resource is missing the required [ApplicationId] property.')] ('MyCoolResource', 'Resource is missing the required [ApplicationId] property.')
Traceback:
File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.8/site-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
[...SNIP LONG STACK TRACE...]
current = SamLocalStackProvider(
File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.8/site-packages/samcli/lib/providers/sam_stack_provider.py", line 51, in __init__
self._template_dict = self.get_template(
File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.8/site-packages/samcli/lib/providers/sam_base_provider.py", line 188, in get_template
template_dict = SamTranslatorWrapper(template_dict, parameter_values=parameters_values).run_plugins()
File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.8/site-packages/samcli/lib/samlib/wrapper.py", line 75, in run_plugins
raise InvalidSamDocumentException(
```
### Proposal
In the event that an [intrinsic string function](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html) object (like `{"Fn::Sub": ...}`) is passed to the `Location` field, it should be treated as a string template URL and passed through to the underlying `TemplateURL` parameter of `AWS::CloudFormation::Stack`: Not interpreted as a SAM Application Registry reference.
- I don't believe this should change the SAM spec, as the Location is still "a string", just indirected via CFn functions
- I'm not really sure whether it would require service-side changes or only different validation in the CLI... Assume service might need an update as well?
### Additional Details
Appreciate it may a bit of a niche case, but would hope that others are also looking for solutions to cross-region deployment without having to re-run SAM build/package CLIs in the other regions?
Contributor guide
Assessment
This issue has not been assessed yet.