Example to demonstrate using of Dynamic Blocks.

Objective:
šŸ¤Æ TO set up ECS service definition such a way that, deploying my application behind multiple-loadbalancer/single-loadbalancer/no-loadbalancer should be optional.

Explantion:
I am using dynamic Block to set loadbalancer block in my service definition as optional.

šŸ¼ Set variable as type list and pass target group arn through varaible. Remember its list of string, therefore defined in quotes.
šŸ¼ In ECS service definition I have said loadbalancer is my Dynamic block.
šŸ¼ Iterate based on target_group_arn
šŸ¼ for each values defined in target_group vraibles set the loadbalancer block definition respectively. If there are two values defined in variable set two loadbalancer block with each value. Same goes with one and no value defined in varaible.
šŸ¼ Since my container_port and container_name are same but only target_group_arnis different (one is behind private LB and other public LB), hence just change it's values in loadbalancer block. target_group_arn = target_group_arn.value`

šŸ„‚

Links:
https://www.terraform.io/docs/configuration/expressions.html#dynamic-blocks

https://github.com/hashicorp/terraform-guides/tree/master/infrastructure-as-code/terraform-0.12-examples/dynamic-blocks-and-splat-expressions