slayer27
slayer27

Reputation: 65

Cloudformation Split & ImportValue

I have 2 stacks in CloudFormation. One is creating a vpc with a couple of subnets which are exported in order to be used in other stacks. The idea is to have those subnets to be used in other stacks. The vpc stack exports the values properly but I am unable to Import them in the second stack because it uses a list of strings.

Stack 1:

  PrivateSubnets:
    Description: "A list of the public subnets"
    Value: !Join [ ",", [ !Ref PrivateSubnet1, !Ref PrivateSubnet2 ]]
    Export:
      Name: !Sub "${StagingArea}-PrivateSubnets"
  
  PublicSubnet1:
    Description: "Reference to the publi subnet in the 1st Availability Zone"
    Value: !Ref PublicSubnet1

  PublicSubnet2: 
    Description: "A reference to the public subnet in the 2nd Availability Zone"
    Value: !Ref PublicSubnet2

  PrivateSubnet1:
    Description: "A reference to the private subnet in the 1st Availability Zone"
    Value: !Ref PrivateSubnet1
    Export:
      Name: !Sub "${StagingArea}-PrivateSubnet1"
  PrivateSubnet2: 
    Description: "A reference to the private subnet in the 2nd Availability Zone"
    Value: !Ref PrivateSubnet2
    Export:
      Name: !Sub "${StagingArea}-PrivateSubnet2"

When I try to ImportValue into my second stack it does not work. Stack 2 below:

  DbSubnetGroup:
    Type: AWS::RDS::DBSubnetGroup
    Properties:
      DBSubnetGroupDescription: !Sub "${StagingArea} RDS DB Subnet Group"
      SubnetIds: 
        - !ImportValue 'Fn::Sub': '{$StagingArea}-PrivateSubnet1'
        - !ImportValue 'Fn::Sub': '{$StagingArea}-PrivateSubnet2'

Is it possible to get the values exported from the first stack into the second stack? I have tried various options but none of them seem to be working.

Upvotes: 1

Views: 1028

Answers (1)

Marcin
Marcin

Reputation: 238239

Sub is incorrect (wrong use of $) and its better to follow the docs and have the statement in two lines:

        - Fn::ImportValue:
            !Sub '${StagingArea}-PrivateSubnet1'
        - Fn::ImportValue:
            !Sub '${StagingArea}--PrivateSubnet2'

Upvotes: 1

Related Questions