Search code examples
amazon-web-servicesaws-cloudformationsam

AWS SAM - How to combine stage variables with Fn::Sub on swagger endpoint uri


We have a stack that makes use of the other stack's output with cross-stack referencing using Fn::ImportValue within a swagger definition body.

Note: other parts ommitted to shorten the code

SampleApi:
  Type: AWS::Serverless::Api
  Properties:
    StageName: Stage
    Variables:
      SampleFunctionName:
        Fn::ImportValue: 
          !Sub ${OtherStackName}-SampleFunctionName
    DefinitionBody:
      swagger: 2.0
      paths:
        /sample:
          get:
            x-amazon-apigateway-integration:
              uri:
                Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${stageVariables.SampleFunctionName}/invocations

Where ${OtherStackName}-SampleFunctionName is the name of the function that's being imported from other stack.

Now, our problem now is that we cannot put it inside the endpoint's uri. Cloudformation is complaining about it during sam deploy, saying that ${stageVariables.SampleFunctionName} is an illegal attribute inside Fn::Sub.

I have tried several ways including putting the whole uri on the stage variable itself, but still it does not come.

Your thoughts are very much appreciated!


Solution

  • I made it to work this way

    x-amazon-apigateway-integration:
      uri:
        Fn::Join:
          - ''
          - - Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/
            - Fn::ImportValue: 
                Fn::Sub: ${StackName}-FunctionArn
            - /invocations
    

    For some reason, shorthand functions won't work inside uri and ${stageVariables.<somename>} do not work inside an Fn::Sub function.

    Note that this only happens inside a swagger definition body