Repository: aws/serverless-application-model

Issue #22: Support CloudFormation intrinsic functions in SAM resources
Created: 2016-11-23T23:43:07Z
We quite often use !Ref, !Sub, !Join in properties that are supposed to hold strings such as CodeUri, DefinitionUri, StageName (#19) etc. This is a perfectly valid CloudFormation, but the transformation logic that parses SAM does not resolve intrinsic functions before parsing the property as a string. So the transformation logic would fail and depending on the property it could either return a validation error or internal failure. Properties that are directly passed down to CloudFormation are unaffected by this problem.
This is a feature request to add support for resolving a subset of CloudFormation intrinsics in the transformation logic. We care about only the subset of properties that the transform manipulates, like CodeUri.
Following intrinsics will be supported:

!Sub
!Join

on following properties:

CodeUri on AWS::Serverless::Function
Role on AWS::Serverless::Function
DefinitionUri on AWS::Serverless::Api
StageName on  AWS::Serverless::Api

Duplicate issue #372: Nested Fn::ImportValue in Fn::Sub not working for SAM template
Created: 2018-04-06T22:03:48Z
Description:
I am trying to define Serverless API resource. But having trouble in defining location of swagger specification file using function ImportValue.
Steps to reproduce the issue:
I am not able to define AWS::Serverless::Api resource having nested function ImportValue in Location. I have tried following three ways, none of them work.
Note: Stack parameters are defined properly and export value from other stack exists. Not showing them here for brevity reason.
ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
        Name: !Sub ${AWS::StackName}-API
        StageName: !Ref ApiGatewayStageName
        DefinitionBody:
          'Fn::Transform':
            Name: 'AWS::Include'
            Parameters:
              Location:
                Fn::Sub:
                - s3://${BucketName}/${SwaggerSpecificationS3Key}
                - BucketName:
                    Fn::ImportValue:
                      !Sub "${EnvironmentName}-dist-bucket-${AWS::Region}" 

ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
        Name: !Sub ${AWS::StackName}-API
        StageName: !Ref ApiGatewayStageName
        DefinitionBody:
          'Fn::Transform':
            Name: 'AWS::Include'
            Parameters:
              Location:
                Fn::Sub:
                - s3://${BucketName}/${SwaggerSpecificationS3Key}
                - BucketName:
                    !ImportValue 'dev-dist-bucket-us-east-1' 

ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
        Name: !Sub ${AWS::StackName}-API
        StageName: !Ref ApiGatewayStageName
        DefinitionBody:
          'Fn::Transform':
            Name: 'AWS::Include'
            Parameters:
              Location:
                Fn::Sub:
                - s3://${BucketName}/${SwaggerSpecificationS3Key}
                - BucketName:
                    Fn::ImportValue: 'dev-dist-bucket-us-east-1' 

Cloudformation shows following error.
FAILED - The value of parameter Location under transform Include must resolve to a string, number, boolean or a list of any of these.
However, if I do not use ImportValue it works with a nested Fn::Sub
ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
        Name: !Sub ${AWS::StackName}-API
        StageName: !Ref ApiGatewayStageName
        DefinitionBody:
          'Fn::Transform':
            Name: 'AWS::Include'
            Parameters:
              Location:
                Fn::Sub:
                - s3://${BucketName}/${SwaggerSpecificationS3Key}
                - BucketName:
                    Fn::Sub: dist-bucket-${EnvironmentName}-${AWS::Region} 

Is it function ImportValue that cannot be used under function Sub?
or
Is it yaml format that does not support ImportValue under a Sub?

Closing comment (2018-05-02T13:19:26Z): I've got the same problem 😞
Isn't that related to #22 and aws/aws-sam-cli#30?