edit-icon download-icon

Conditions

Last Updated: Sep 11, 2018

Each condition item is defined by Fn::And, Fn::Or, Fn::Not, and/or Fn::Equals. It is calculated based on the specified input parameter values when a stack is created or updated. You can reference other conditions, parameter values, or mappings in conditions. You can associate a condition with a resource and resource attributes in the Resources and Outputs parts of a template. A condition is associated with a resource using the built-in function Fn::If or the Condition field of the resource.

Syntax

A condition consists of the condition name and condition body. The condition name is a string, which must be unique in the template. Conditions are defined by built-in functions Fn::And, Fn::Or, Fn::Not, and/or Fn::Equals. Other conditions can be referenced in a condition. Multiple conditions are separated by commas (,).

Examples

The following example shows how to define Conditions.

  1. "Conditions" : {
  2. "DevEnv": {"Fn::Equals": ["Dev", {"Ref": "EnvType"}]},
  3. "UTEnv": {"Fn::Equals": ["UT", {"Ref": "EnvType"}]},
  4. "PREEnv": {"Fn::Not": {"Fn::Or": ["DevEnv", "UTEnv"]}},
  5. "ProdEnv": {"Fn::And": [{"Fn::Equals": ["Prod", {"Ref": "EnvType"}]}, "PREEnv"]}
  6. }

The following example shows how to associate Conditions with Resources. In this example, whether to create a data disk and OSS bucket for the ECS instance is determined by the value of EnvType.

  1. {
  2. "ROSTemplateFormatVersion":"2015-09-01",
  3. "Parameters":{
  4. "EnvType":{
  5. "Default":"pre",
  6. "Type":"String"
  7. }
  8. },
  9. "Conditions":{
  10. "CreateProdRes":{
  11. "Fn::Equals":[
  12. "prod",
  13. {
  14. "Ref":"EnvType"
  15. }
  16. ]
  17. }
  18. },
  19. "Resources":{
  20. "WebServer":{
  21. "Type":"ALIYUN::ECS::Instance",
  22. "Properties":{
  23. "DiskMappings":{
  24. "Fn::If":[
  25. "CreateProdRes",
  26. [
  27. {
  28. "Category":"cloud_efficiency",
  29. "DiskName":"FirstDataDiskName",
  30. "Size":40
  31. },
  32. {
  33. "Category":"cloud_ssd",
  34. "DiskName":"SecondDataDiskName",
  35. "Size":40
  36. }
  37. ],
  38. {
  39. "Ref":"ALIYUN::NoValue"
  40. }
  41. ]
  42. },
  43. "VpcId":"vpc-2zew9pxh2yirtzqxdboi1",
  44. "SystemDiskCategory":"cloud_efficiency",
  45. "SecurityGroupId":"sg-2zece6wcqriejf1v91sr",
  46. "SystemDiskSize":40,
  47. "ImageId":"centos_6_8_64_40G_base_20170222.vhd",
  48. "IoOptimized":"optimized",
  49. "VSwitchId":"vsw-2zed9txvy7h2srqo6jmgq",
  50. "InstanceType":"ecs.n1.medium"
  51. }
  52. },
  53. "OssBucket": {
  54. "Type": "ALIYUN::OSS::Bucket",
  55. "Condition": "CreateProdRes",
  56. "Properties": {
  57. "AccessControl": "private",
  58. "BucketName": "myprodbucket"
  59. }
  60. }
  61. },
  62. "Outputs":{
  63. "InstanceId":{
  64. "Value":{
  65. "Fn::GetAtt":[
  66. "WebServer",
  67. "InstanceId"
  68. ]
  69. }
  70. },
  71. "OssDomain":{
  72. "Condition": "CreateProdRes",
  73. "Value":{
  74. "Fn::GetAtt":[
  75. "OssBucket",
  76. "DomainName"
  77. ]
  78. }
  79. }
  80. }
  81. }
Thank you! We've received your feedback.