Exporting output value

Hello,
I’m trying to share cognito authorizer between my stacks for this I’m exporting my authorizer but when I try to reference it in another service I get the error

Trying to request a non exported variable from CloudFormation. Stack name: “myApp-services-test” Requested variable: “ExtApiGatewayAuthorizer-test”.

Here is my stack where I have authorizer defined and exported:

    CognitoUserPool:
  Type: AWS::Cognito::UserPool
  Properties:
    # Generate a name based on the stage
    UserPoolName: ${self:provider.stage}-user-pool
    # Set email as an alias
    UsernameAttributes:
      - email
    AutoVerifiedAttributes:
      - email

    ApiGatewayAuthorizer: 
  Type: AWS::ApiGateway::Authorizer
  Properties: 
    Name: CognitoAuthorizer
    Type: COGNITO_USER_POOLS
    IdentitySource: method.request.header.Authorization
    RestApiId: { "Ref": "ProxyApi" }
    ProviderARNs: 
      - Fn::GetAtt:
          - CognitoUserPool
          - Arn

    ApiGatewayAuthorizerId:
  Value:
    Ref: ApiGatewayAuthorizer
  Export:
    Name: ExtApiGatewayAuthorizer-${self:provider.stage}

this is successfully exported as I can see it in stack exports list from my aws console.

I try to reference it in another stack like this:

  myFunction:
handler: handler.myFunction
events:
  - http:
      path: /{userID}
      method: put
      cors: true
      authorizer:
        type: COGNITO_USER_POOLS
        authorizerId: ${myApp-services-${self:provider.stage}.ExtApiGatewayAuthorizer-${self:provider.stage}}

my env info

  Your Environment Information ---------------------------
 Operating System:          darwin
 Node Version:              12.13.1
 Framework Version:         1.60.5
 Plugin Version:            3.2.7
 SDK Version:               2.2.1
 Components Core Version:   1.1.2
 Components CLI Version:    1.4.0
1 Like

Answering my own question

it looks like I should have imported by output name not output export name, which is bit weird and all the docs I have seen point to export name, but this is how I was able to make it work

replaced this -

authorizerId: ${myApp-services-${self:provider.stage}.ExtApiGatewayAuthorizer-${self:provider.stage}}

with -
authorizerId: ${myApp-services-${self:provider.stage}.ApiGatewayAuthorizerId}

2 Likes

Awesome! This solved my issue that I have been having for the last day or so!

Thanks!