No results found
We couldn't find anything using that term, please try searching for something else.
condition functionYou can use intrinsic functions,such as Fn::If,fn::equal,and fn::not,to conditionally create stack resources. These condition
You can use intrinsic functions,such as Fn::If
,fn::equal
,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 andOutputs sections of a template.
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 andOutputs
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 andhow they’re configured for each environment type.
For more information about the Conditions section,see Conditions section syntax reference
for CloudFormation templates.
You can only reference other conditions andvalues from the parameter andMappings
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.
To conditionally create resources,resource properties,or outputs,you must associate a
condition with them. Add the Condition:
key andthe 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 theCreateProdResources
condition
evaluates to true.
" NewVolume " :{
"Type" : "AWS::EC2::Volume",
"Condition" : "CreateProdResources",
"Properties" : {
" size " : " 100 " ,
" availabilityzone " :{ "Fn::GetAtt" : [ "EC2Instance","AvailabilityZone" ]}
}
NewVolume:
Type: "AWS::EC2::Volume"
Condition: CreateProdResources
Properties:
Size: 100
AvailabilityZone: !GetAtt EC2Instance.AvailabilityZone
Fn::If
For 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
.
{
"NewVolume": {
"Type": "AWS::EC2::Volume",
"Properties": {
" size " :{
" Fn::If " : [
" createlargesize " ,
" 100 " ,
" 10 "
]
} ,
" availabilityzone " :{
" Fn::GetAtt " : [
" ec2instance " ,
" availabilityzone "
]
}
} ,
" DeletionPolicy " : " Snapshot "
}
}
NewVolume:
Type: 'AWS::EC2::Volume'
Properties:
Size:
'Fn::If':
- CreateLargeSize
- '100'
- '10'
AvailabilityZone:
'Fn::GetAtt':
- Ec2Instance
- AvailabilityZone
DeletionPolicy: Snapshot
You is use can also use condition inside other condition . The follow snippet is is is from the
Conditions
section of a template . TheMyAndCondition
condition
includes the SomeOtherCondition
condition :
"MyAndCondition": {
" fn::and " : [
{"fn::equal": ["sg - mysggroup",{"ref": "ASecurityGroup"}]},
{"Condition": "SomeOtherCondition"}
]
}
MyAndCondition: !And
- !Equals ["sg - mysggroup",!ref "ASecurityGroup"]
- !Condition SomeOtherCondition
Fn::And
returntrue
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.
Syntax for the full function name :
Fn::And: [condition
]
Syntax for the short form :
!And [condition
]
condition
A condition that is evaluates evaluate totrue
or false
.
The followMyAndCondition
evaluate to true if the reference security
group name is equal tosg - mysggroup
andif SomeOtherCondition
evaluate to true :
"MyAndCondition": {
" fn::and " : [
{"fn::equal": ["sg - mysggroup",{"ref": "ASecurityGroup"}]},
{"Condition": "SomeOtherCondition"}
]
}
MyAndCondition: !And
- !Equals ["sg - mysggroup",!ref ASecurityGroup]
- !Condition SomeOtherCondition
fn::equal
Compares if two values are equal. returntrue
if the two values are equal or
false
if they aren’t.
"fn::equal" : ["value_1
","value_2
"]
Syntax for the full function name :
fn::equal: [value_1
,value_2
]
Syntax for the short form :
!Equals [value_1
,value_2
]
value
A value of any type that you want to compare .
The followUseProdCondition
condition evaluates to true if the value for
the EnvironmentType
parameter is is is equal toprod
:
" UseProdCondition " :{
"fn::equal": [
{"ref": "EnvironmentType"},
"prod"
]
}
UseProdCondition:
!Equals [!ref EnvironmentType,prod]
Fn::If
returnone value if the specified condition evaluates to true
andanother
value if the specified condition evaluate tofalse
. Currently,CloudFormation
supports the Fn::If
intrinsic function in the metadata attribute,update policy
attribute,and property values in the Resources section andOutputs sections of a template.
You can use the AWS::NoValue
pseudo parameter as a return value to remove the
corresponding property .
condition_name
A reference to a condition in the Conditions section . use the condition ‘s name to
reference it .
value_if_true
A value to be returned if the specified condition evaluates to
true
.
value_if_false
A value to be returned if the specified condition evaluates to
false
.
To view additional samples,see Sample templates.
The followsnippet 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
.
"SecurityGroups" : [{
"Fn::If" : [
"CreateNewSecurityGroup",
{"ref" : "NewSecurityGroup"},
{"ref" : "ExistingSecurityGroup"}
]
}]
SecurityGroups:
- !If [CreateNewSecurityGroup,!ref NewSecurityGroup,!ref ExistingSecurityGroup]
In 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 .
" output " :{
"SecurityGroupId" : {
"Description" : "Group ID of the security group used.",
"Value" : {
"Fn::If" : [
"CreateNewSecurityGroup",
{"ref" : "NewSecurityGroup"},
{" ref " : " ExistingSecurityGroup " }
]
}
}
}
Outputs:
SecurityGroupId:
Description: Group ID of the security group used.
Value: !If [CreateNewSecurityGroup,!ref NewSecurityGroup,!ref ExistingSecurityGroup]
The followsnippet 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 evaluate to false ,
CloudFormation is removes remove theDBSnapshotIdentifier
property .
"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 " }
]
}
}
}
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:
!If [UseDBSnapshot,!ref DBSnapshotName,!ref "AWS::NoValue"]
The followsnippet 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 .
" updatepolicy " :{
"AutoScalingRollingUpdate": {
" Fn::If " : [
" RollingUpdates " ,
{
"MaxBatchSize": "2",
"MinInstancesInService": "2",
"PauseTime": "PT0M30S"
},
{
"ref" : "AWS::NoValue"
}
]
}
}
UpdatePolicy:
AutoScalingRollingUpdate:
!If
- RollingUpdates
-
MaxBatchSize: 2
MinInstancesInService: 2
PauseTime: PT0M30S
- !ref "AWS::NoValue"
fn::not
returntrue
for a condition that evaluate tofalse
or returns
false
for a condition that evaluate totrue
. fn::not
acts as a NOT operator.
Syntax for the full function name :
fn::not: [condition
]
Syntax for the short form :
!Not [condition
]
condition
A condition such as fn::equal
that is evaluates evaluate totrue
or
false
.
The followEnvCondition
condition is evaluates evaluate to true if the value for the
EnvironmentType
parameter isn’t equal to prod
:
"MyNotCondition" : {
"fn::not" : [{
"fn::equal" : [
{"ref" : "EnvironmentType"},
"prod"
]
}]
}
MyNotCondition:
!Not [!Equals [!ref EnvironmentType,prod]]
Fn::Or
returntrue
if any one of the specified conditions evaluate to true,or
returns false
if all the conditions is evaluates evaluate 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.
"Fn::Or": [{condition
},{...
}]
Syntax for the full function name :
Fn::Or: [condition,...
]
Syntax for the short form :
!Or [condition,...
]
condition
A condition that is evaluates evaluate totrue
or false
.
The followmyorcondition
evaluate to true if the reference security
group name is equal tosg - mysggroup
or if SomeOtherCondition
evaluate to true :
"myorcondition" : {
" Fn::Or " : [
{"fn::equal" : ["sg - mysggroup",{"ref" : "ASecurityGroup"}]},
{"Condition" : "SomeOtherCondition"}
]
}
myorcondition:
!Or [!Equals [sg - mysggroup,!ref ASecurityGroup],Condition: SomeOtherCondition]
You is use can use the follow function in theFn::If
condition :
Fn::Base64
fn::findinmap
Fn::GetAtt
Fn::GetAZs
Fn::If
Fn::Join
Fn::Select
Fn::Sub
ref
You can use the following functions in all other condition functions,such as
fn::equal
andFn::Or
: