AWS Optical Cloud Line

I am trying to install a cloud information template that will either launch a clean instance or one of the snapshots. I would like to be able to use an operator like if / else to make it look like

pseudo code: if InputSnapshotId: "SnapshotId" : {"Ref" : "InputSnapshotId"}, else: "Size" : 20, 

In cloudformation, I tried several things like:

 "WebserverInstanceDataVolume" : { "Type" : "AWS::EC2::Volume", "Properties" : { "Fn::If" : [ {"Ref" : "FromSnapshot"}, {"SnapshotId" : { "Ref" : "InputSnapshotId" }}, {"Size" : "20"} ], "VolumeType" : "standard", "AvailabilityZone" : { "Fn::GetAtt" : [ "WebserverInstance", "AvailabilityZone" ]}, "Tags" : [ {"Key" : "Role", "Value": "data" }, ] }, "DeletionPolicy" : "Delete" }, 

Or wrap in Fn :: If in {}:

  {"Fn::If" : [ {"Ref" : "FromSnapshot"}, {"SnapshotId" : { "Ref" : "InputSnapshotId" }}, {"Size" : "20"} ]} 

All of which kick different types or errors. The first gives the "Found unsupported Fn :: If property" in cloudformation, the second, just invalid JSON. I could take a picture of an empty volume and define a size parameter, and then always pass SnapshotId and size, but I feel there should be a way to have an extra line in cloud form.

Any ideas?

+6
source share
1 answer

You can do the following:

  "Conditions" : { "NotUseSnapshot" : {"Fn::Equals" : [{"Ref" : "InputSnapshotId"}, ""]} }, "Resources" : { "WebserverInstanceDataVolume" : { "Type" : "AWS::EC2::Volume", "Properties" : { "Size" : { "Fn::If" : [ "NotUseSnapshot", "20", {"Ref" : "AWS::NoValue"} ] }, "SnapshotId" : { "Fn::If" : [ "NotUseSnapshot", {"Ref" : "AWS::NoValue"}, {"Ref" : "InputSnapshotId"} ] }, "VolumeType" : "standard", "AvailabilityZone" : { "Fn::GetAtt" : [ "WebserverInstance", "AvailabilityZone" ]}, "Tags" : [ {"Key" : "Role", "Value": "data" } ] }, "DeletionPolicy" : "Delete" } } 

Here is the link to the functional template: https://github.com/caussourd/public-cloudformation-templates/blob/master/conditional_volume_creation.template

+3
source

Source: https://habr.com/ru/post/976080/


All Articles