I need to reference a function from one stack into...
# sst
a
I need to reference a function from one stack into other stack, but I can’t share the
Function
?
f
I wonder if importing
Function as Fn
works:
Copy code
import { Function as Fn } from "@serverless-stack/resources";
j
Hey @Adrián Mouly, I am not sure if this is what you are looking for, but I have a common resources stack that exposes various constructs such as functions to other stacks. I have tried to reproduce a minimal example for you below. It works for me on an Authorizer, but it should work in any construct taking an sst.FunctionDefinition parameter. You should also be able to refer to any of the function properties if you don’t need the full definition.
Copy code
/*
  Interfaces
*/
interface SharedStackProps extends sst.StackProps {
  readonly sharedFunction: sst.Function,
}

/*
  📁Index.ts
*/
import StackCommonResources from '@stacks/StackCommonResources'
import StackOne from '@stacks/StackOne'

// Stack in which shared functions are created
const stackCommonResources = new StackCommonResources(app, 'StackCommonResources')
const sharedStackProps = {
  sharedFunction: stackCommonResources.sharedFunction
}

// Stack consuming shared functions
const stackOne = new StackOne(app, 'StackOne', sharedStackProps)


/*
  📁stackCommonResources.ts
  This stack defines a shared function
*/
export default class StackCommonResources extends sst.Stack {

  public readonly sharedFunction: sst.Function

  constructor(scope: <http://sst.App|sst.App>, id: string, props?: sst.StackProps) {
    super(scope, id, props)

    this.sharedFunction = new sst.Function(this, 'sharedFunction', {
      handler: 'src/sharedFunction/index.handler',
      functionName: `${scope.stage}-my-shared-function`,
      ...
    })

    this.addOutputs({})
  }
}

/*
  📁stackOne.ts
  This stack imports a function from stackCommonResources
*/
export default class StackOne extends sst.Stack {

  public readonly sharedFunction: sst.Function

  constructor(scope: <http://sst.App|sst.App>, id: string, props: SharedStackProps) {
    super(scope, id, props)

    const authorizer = new apigateway.TokenAuthorizer(this, 'authorizer', {
      handler: props.sharedFunction,
      ...
    })

    this.addOutputs({})
  }
}
a
Ok, using
Function as Fn
worked.
My problem is I’m decontructing the imports, I’m not using the
sst
way.
I’m importing resources like:
import { Api, App, Queue, Stack, StackProps, Topic, Function as SstFunction } from ‘@serverless-stack/resources’;