title: Invoke Lambda from CloudFormation template or vice-versa
body: <p>Sometimes I need certain fields in a CF template to be dynamically generated from a list of complex objects, currently I don't know of any way to loop over anything using CF functions so parts of the execution is delegated to Lambdas -- by providing the list of parameters as character-delimited strings -- which in turn use the Python/Ruby AWS SDK to create other resources.</p>
  
  <p>It seems to me that it would be easier to invoke a Lambda first, that Lambda would dynamically construct the CF stack given an arbitrarily complex JSON payload without having to encode list of objects as character-delimited strings.</p>
  
  <p>This seems like inverting the current pattern I have where the CF template invokes Lambdas and instead have a Lambda receive the parameters and then create the CF stack using Python/Ruby SDK (as part of the Lambda execution).</p>
  
  <p>I haven't found much about this pattern being a bad idea or it being discouraged but I also haven't found many examples of it on the web. </p>
  
  <p>Btw, I'm quite new to CF and Lambdas so I'm not sure if this is a no-no for AWS/CF/Lambdas, so this question is partly to figure that out.</p>
  
  <h1>Example</h1>
  
  <p>Creating an ECS task definition with a dynamic list of port mappings:</p>
  
  <pre><code>"ecsTask":{
        "Properties":{
          # ... other stuffs
          "container_definitions": [
            {
              "name": {"Ref":"AWS::StackName"},
              "image": {"Ref":"DockerImage"},
              "portMappings": [
                  # ... dynamic list of ports, example below
                  {
                      "ContainerPort": "123",
                      "HostPort": "456"
                  },
              ],
              "environment":{ "Ref": "EnvironmentVariables" },
            # ... other stuffs
            }
          ]
        },
  </code></pre>