When you build serverless applications with AWS SAM or CloudFormation, sometimes you need to share resources between stacks. For example, you may have:

  • A Cognito User Pool in one stack (for user authentication)
  • And an API Gateway in another stack that needs to use that Cognito pool

There are two main ways to connect these stacks:


Option 1: Using Parameters

You can pass values like the User Pool ARN or ID as a parameter when you deploy the second stack.

Example:

In template.yaml:

Parameters:
  CognitoUserPoolArn:
    Type: String

Then deploy it with:

sam deploy --parameter-overrides CognitoUserPoolArn=arn:aws:cognito-idp:...

This method is simple and flexible. You can change the value anytime.


Option 2: Using Export and ImportValue (Cross-Stack Reference)

In this method, the first stack exports a value in its Outputs, and the second stack imports it automatically.

Stack 1 (Cognito):

Outputs:
  CognitoUserPoolArn:
    Value: !GetAtt CognitoUserPool.Arn
    Export:
      Name: MyApp-CognitoUserPoolArn

Stack 2 (API):

UserPoolArn: !ImportValue MyApp-CognitoUserPoolArn

This method is more automatic and organized.


If you’re working on small or temporary stacks, using parameters is okay.

But if your infrastructure is growing and you want clean, reusable, and safe connections between stacks — use Export and ImportValue.

It helps a lot when you do refactoring later or share values across multiple stacks.