Search code examples
amazon-web-servicesreferenceaws-cloudformationamazon-ecs

How to reference Cloudformation Ressources in a new Cloudformation template?


I have two Cloudformation files and I want to reference already created Ressources from one template, in another template. For Example: In the first one I create an ECS Cluster. In the second one I want to reference this cluster and build a Service in it. How can I do it?


Solution

  • To do this you have to exporting stack output values from the first template. Presumably this would be ECS Cluster name and/or its arn:

    MyCluster:
       Type: AWS::ECS::Cluster
       Properties: 
        #....
    
    Outputs:
      MyClusterName:
         Value: !Ref MyCluster
         Export:
            Name: ECSClusterName
    

    Then in the second template you would use ImportValue to reference the exported output:

    MyESSService:
       Type: AWS::ECS::Service
       Properties: 
         Cluster: !ImportValue ECSClusterName