Which of the following functions allows for different options depending on whether a null value exists?
You can use intrinsic functions, such as Fn::If, Fn::Equals, and Fn::Not, to conditionally create stack resources. These conditions are evaluated based on input parameters that you declare when you create or update a stack. After you define all your conditions, you can associate them with resources or resource properties in the Resources and Outputs sections of a template. Show
You define all conditions in the Conditions section of a template except for Fn::If conditions. You can use the Fn::If condition in the metadata attribute, update policy attribute, and property values in the Resources section and Outputs sections of a template. You might use conditions when you want to reuse a template that can create resources in different contexts, such as a test environment versus a production environment. In your template, you can add an EnvironmentType input parameter, which accepts either prod or test as inputs. For the production environment, you might include Amazon EC2 instances with certain capabilities; however, for the test environment, you want to use less capabilities to save costs. With conditions, you can define which resources are created and how they're configured for each environment type. For more information about the Conditions section, see Conditions. You can only reference other conditions and values from the Parameters and Mappings sections of a template. For example, you can reference a value from an input parameter, but you can't reference the logical ID of a resource in a condition. Topics
Associating a conditionTo conditionally create resources, resource properties, or outputs, you must associate a condition with them. Add the Condition: key and the logical ID of the condition as an attribute to associate a condition, as shown in the following snippet. AWS CloudFormation creates the NewVolume resource only when the CreateProdResources condition evaluates to true. JSON"NewVolume" : { "Type" : "AWS::EC2::Volume", "Condition" : "CreateProdResources", "Properties" : { "Size" : "100", "AvailabilityZone" : { "Fn::GetAtt" : [ "EC2Instance", "AvailabilityZone" ]} }YAMLNewVolume: Type: "AWS::EC2::Volume" Condition: CreateProdResources Properties: Size: 100 AvailabilityZone: !GetAtt EC2Instance.AvailabilityZoneFn::IfFor the Fn::If function, you only need to specify the condition name. The following snippet shows how to use Fn::If to conditionally specify a resource property. If the CreateLargeSize condition is true, CloudFormation sets the volume size to 100. If the condition is false, CloudFormation sets the volume size to 10. JSON{ "NewVolume": { "Type": "AWS::EC2::Volume", "Properties": { "Size": { "Fn::If": [ "CreateLargeSize", "100", "10" ] }, "AvailabilityZone": { "Fn::GetAtt": [ "Ec2Instance", "AvailabilityZone" ] } }, "DeletionPolicy": "Snapshot" } }YAMLNewVolume: Type: 'AWS::EC2::Volume' Properties: Size: 'Fn::If': - CreateLargeSize - '100' - '10' AvailabilityZone: 'Fn::GetAtt': - Ec2Instance - AvailabilityZone DeletionPolicy: SnapshotNested conditionsYou can also use conditions inside other conditions. The following snippet is from the Conditions section of a template. The MyAndCondition condition includes the SomeOtherCondition condition: JSON"MyAndCondition": { "Fn::And": [ {"Fn::Equals": ["sg-mysggroup", {"Ref": "ASecurityGroup"}]}, {"Condition": "SomeOtherCondition"} ] }YAMLMyAndCondition: !And - !Equals ["sg-mysggroup", !Ref "ASecurityGroup"] - !Condition SomeOtherConditionFn::AndReturns true if all the specified conditions evaluate to true, or returns false if any one of the conditions evaluates to false. Fn::And acts as an AND operator. The minimum number of conditions that you can include is 2, and the maximum is 10. DeclarationYAMLSyntax for the full function name: Fn::And: [condition]Syntax for the short form: !And [condition]Parameterscondition A condition that evaluates to true or false. ExampleThe following MyAndCondition evaluates to true if the referenced security group name is equal to sg-mysggroup and if SomeOtherCondition evaluates to true: JSON"MyAndCondition": { "Fn::And": [ {"Fn::Equals": ["sg-mysggroup", {"Ref": "ASecurityGroup"}]}, {"Condition": "SomeOtherCondition"} ] }YAMLMyAndCondition: !And - !Equals ["sg-mysggroup", !Ref ASecurityGroup] - !Condition SomeOtherConditionFn::EqualsCompares if two values are equal. Returns true if the two values are equal or false if they aren't. DeclarationJSON"Fn::Equals" : ["value_1", "value_2"]YAMLSyntax for the full function name: Fn::Equals: [value_1, value_2]Syntax for the short form: !Equals [value_1, value_2]Parametersvalue A value of any type that you want to compare. ExampleThe following UseProdCondition condition evaluates to true if the value for the EnvironmentType parameter is equal to prod: JSON"UseProdCondition" : { "Fn::Equals": [ {"Ref": "EnvironmentType"}, "prod" ] }YAMLUseProdCondition: !Equals [!Ref EnvironmentType, prod]Fn::IfReturns one value if the specified condition evaluates to true and another value if the specified condition evaluates to false. Currently, CloudFormation supports the Fn::If intrinsic function in the metadata attribute, update policy attribute, and property values in the Resources section and Outputs sections of a template. You can use the AWS::NoValue pseudo parameter as a return value to remove the corresponding property. DeclarationParameterscondition_name A reference to a condition in the Conditions section. Use the condition's name to reference it. value_if_trueA value to be returned if the specified condition evaluates to true. value_if_falseA value to be returned if the specified condition evaluates to false. ExamplesTo view additional samples, see Sample templates. Example 1The following snippet uses an Fn::If function in the SecurityGroups property for an Amazon EC2 resource. If the CreateNewSecurityGroup condition evaluates to true, CloudFormation uses the referenced value of NewSecurityGroup to specify the SecurityGroups property; otherwise, CloudFormation uses the referenced value of ExistingSecurityGroup. JSON"SecurityGroups" : [{ "Fn::If" : [ "CreateNewSecurityGroup", {"Ref" : "NewSecurityGroup"}, {"Ref" : "ExistingSecurityGroup"} ] }]YAMLSecurityGroups: - !If [CreateNewSecurityGroup, !Ref NewSecurityGroup, !Ref ExistingSecurityGroup]Example 2In the Output section of a template, you can use the Fn::If function to conditionally output information. In the following snippet, if the CreateNewSecurityGroup condition evaluates to true, CloudFormation outputs the security group ID of the NewSecurityGroup resource. If the condition is false, CloudFormation outputs the security group ID of the ExistingSecurityGroup resource. JSON"Outputs" : { "SecurityGroupId" : { "Description" : "Group ID of the security group used.", "Value" : { "Fn::If" : [ "CreateNewSecurityGroup", {"Ref" : "NewSecurityGroup"}, {"Ref" : "ExistingSecurityGroup"} ] } } }YAMLOutputs: SecurityGroupId: Description: Group ID of the security group used. Value: !If [CreateNewSecurityGroup, !Ref NewSecurityGroup, !Ref ExistingSecurityGroup]Example 3The following snippet uses the AWS::NoValue pseudo parameter in an Fn::If function. The condition uses a snapshot for an Amazon RDS DB instance only if a snapshot ID is provided. If the UseDBSnapshot condition evaluates to true, CloudFormation uses the DBSnapshotName parameter value for the DBSnapshotIdentifier property. If the condition evaluates to false, CloudFormation removes the DBSnapshotIdentifier property. JSON"MyDB" : { "Type" : "AWS::RDS::DBInstance", "Properties" : { "AllocatedStorage" : "5", "DBInstanceClass" : "db.t2.small", "Engine" : "MySQL", "EngineVersion" : "5.5", "MasterUsername" : { "Ref" : "DBUser" }, "MasterUserPassword" : { "Ref" : "DBPassword" }, "DBParameterGroupName" : { "Ref" : "MyRDSParamGroup" }, "DBSnapshotIdentifier" : { "Fn::If" : [ "UseDBSnapshot", {"Ref" : "DBSnapshotName"}, {"Ref" : "AWS::NoValue"} ] } } }YAMLMyDB: Type: "AWS::RDS::DBInstance" Properties: AllocatedStorage: 5 DBInstanceClass: db.t2.small Engine: MySQL EngineVersion: 5.5 MasterUsername: !Ref DBUser MasterUserPassword: !Ref DBPassword DBParameterGroupName: !Ref MyRDSParamGroup DBSnapshotIdentifier: !If [UseDBSnapshot, !Ref DBSnapshotName, !Ref "AWS::NoValue"]Example 4The following snippet provides an Auto Scaling update policy only if the RollingUpdates condition evaluates to true. If the condition evaluates to false, CloudFormation removes the AutoScalingRollingUpdate update policy. JSON"UpdatePolicy": { "AutoScalingRollingUpdate": { "Fn::If": [ "RollingUpdates", { "MaxBatchSize": "2", "MinInstancesInService": "2", "PauseTime": "PT0M30S" }, { "Ref" : "AWS::NoValue" } ] } }YAMLUpdatePolicy: AutoScalingRollingUpdate: !If - RollingUpdates - MaxBatchSize: 2 MinInstancesInService: 2 PauseTime: PT0M30S - !Ref "AWS::NoValue"Fn::NotReturns true for a condition that evaluates to false or returns false for a condition that evaluates to true. Fn::Not acts as a NOT operator. DeclarationYAMLSyntax for the full function name: Fn::Not: [condition]Syntax for the short form: !Not [condition]Parameterscondition A condition such as Fn::Equals that evaluates to true or false. ExampleThe following EnvCondition condition evaluates to true if the value for the EnvironmentType parameter isn't equal to prod: JSON"MyNotCondition" : { "Fn::Not" : [{ "Fn::Equals" : [ {"Ref" : "EnvironmentType"}, "prod" ] }] }YAMLMyNotCondition: !Not [!Equals [!Ref EnvironmentType, prod]]Fn::OrReturns true if any one of the specified conditions evaluate to true, or returns false if all the conditions evaluates to false. Fn::Or acts as an OR operator. The minimum number of conditions that you can include is 2, and the maximum is 10. DeclarationJSON"Fn::Or": [{condition}, {...}]YAMLSyntax for the full function name: Syntax for the short form: !Or [condition, ...]Parameterscondition A condition that evaluates to true or false. ExampleThe following MyOrCondition evaluates to true if the referenced security group name is equal to sg-mysggroup or if SomeOtherCondition evaluates to true: JSON"MyOrCondition" : { "Fn::Or" : [ {"Fn::Equals" : ["sg-mysggroup", {"Ref" : "ASecurityGroup"}]}, {"Condition" : "SomeOtherCondition"} ] }YAMLMyOrCondition: !Or [!Equals [sg-mysggroup, !Ref ASecurityGroup], Condition: SomeOtherCondition]Supported functionsYou can use the following functions in the Fn::If condition:
You can use the following functions in all other condition functions, such as Fn::Equals and Fn::Or:
Which of the following functions can be used to include NULL values in calculations?Which of the following functions can be used to include NULL values in calculations? Answer: B. NVL is a general function to provide alternate values to the NULL values. It can really make a difference in arithmetic calculations using AVG, STDDEV and VARIANCE group functions.
Which of the following functions can be used to convert a character string to lower case letters?The LOWER function converts a text string to all lowercase letters. The LOWER function takes just one argument, text, which can be a text value or cell reference.
Which function can be used to determine the number of rows containing a specified value?The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values.
Which of the following is not a data type for applying the single row function?Which of the following is not a Conversion type Single Row function? Answer: C. Conversion functions convert a value from one data type to another. The NVL function replaces a null value with an alternate value.
|