{"id":1155,"date":"2018-02-22T00:25:43","date_gmt":"2018-02-22T00:25:43","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=1155"},"modified":"2018-02-23T11:14:48","modified_gmt":"2018-02-23T11:14:48","slug":"cloudformation-conditions","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=1155","title":{"rendered":"CloudFormation Conditions"},"content":{"rendered":"<p><a href=\"http:\/\/wiki.thomasandsofia.com\/2018\/02\/18\/cloud-formation-main-menu\/\" rel=\"noopener\">Course Main Menu<\/a><\/p>\n<h2>Section 7 Main Menu<\/h2>\n<ul>\n<li><a href=\"#Overview\">Overview<\/a><\/li>\n<li><a href=\"#Hands\">Hands On<\/a><\/li>\n<li><a href=\"#Functions\">Functions<\/a><\/li>\n<li><a href=\"#GetAtt\">Fn::GetAtt (Get Attribute)<\/a><\/li>\n<li><a href=\"#quiz\">Quiz<\/a><\/li>\n<\/ul>\n<p><a name=\"Overview\"><\/a><\/p>\n<h2>Overview<\/h2>\n<p><a href=\"https:\/\/www.udemy.com\/aws-cloudformation-master-class\/learn\/v4\/t\/lecture\/8162164?start=0\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/aws-cloudformation-master-class\/learn\/v4\/t\/lecture\/8162164?start=0<\/a><\/p>\n<h4>What are conditionals used for?<\/h4>\n<ul>\n<li>Conditionals are used to control the creation of resources or outputs based on a condition.<\/li>\n<li>Conditions can be whatever you want them to be, but common ones are:\n<ul>\n<li>Environment (dev \/ test \/ prod)<\/li>\n<li>AWS Region<\/li>\n<li>Any parameter value<\/li>\n<\/ul>\n<\/li>\n<li>Each condition can reference another condition, parameter value or mapping.<\/li>\n<\/ul>\n<p>How to define a condition<\/p>\n<pre>Conditions:\r\n   Logical_Id:\r\n      Intrinsic_Function<\/pre>\n<ul>\n<li>The logical ID is for you to choose. It is how you name your condition.\n<ul>\n<li>Comment: This seems more like a variable name, set to either TRUE or FALSE<\/li>\n<\/ul>\n<\/li>\n<li>The intrinsic function (logical) can be any of the following:\n<ul>\n<li>Fn::And<\/li>\n<li>Fn::Equals<\/li>\n<li>Fn::If<\/li>\n<li>Fn::Not<\/li>\n<li>Fn::or<\/li>\n<\/ul>\n<\/li>\n<li>These are good for determining if you&#8217;re going to create some resources or not.<\/li>\n<\/ul>\n<p><a name=\"Hands\"><\/a><\/p>\n<h2>Conditions Hands On<\/h2>\n<p><a href=\"https:\/\/www.udemy.com\/aws-cloudformation-master-class\/learn\/v4\/t\/lecture\/8139156?start=0\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/aws-cloudformation-master-class\/learn\/v4\/t\/lecture\/8139156?start=0<\/a><\/p>\n<p>This lesson analyzes a CF template that optionally creates a volume and a mount point only if &#8220;prod&#8221; is specified as a parameter.<\/p>\n<p>It uses parameters, mappings, conditionals, outputs, so it&#8217;s a great all around example.<\/p>\n<pre>AWSTemplateFormatVersion: \"2010-09-09\"\r\nMappings:\r\n  RegionMap:\r\n    us-east-1:\r\n      AMI: \"ami-a4c7edb2\"\r\n      TestAz: \"us-east-1a\"\r\n    us-west-1:\r\n      AMI: \"ami-6df1e514\"\r\n      TestAz: \"us-west-1a\"\r\n    us-west-2:\r\n      AMI: \"ami-327f5352\"\r\n      TestAz: \"us-west-2a\"\r\n    eu-west-1:\r\n      AMI: \"ami-d7b9a2b1\"\r\n      TestAz: \"eu-west-1a\"\r\n    sa-east-1:\r\n      AMI: \"ami-87dab1eb\"\r\n      TestAz: \"sa-east-1a\"\r\n    ap-southeast-1:\r\n      AMI: \"ami-77af2014\"\r\n      TestAz: \"ap-southeast-1a\"\r\n    ap-southeast-2:\r\n      AMI: \"ami-10918173\"\r\n      TestAz: \"ap-southeast-2a\"\r\n    ap-northeast-1:\r\n      AMI: \"ami-e21cc38c\"\r\n      TestAz: \"ap-northeast-1a\"\r\nParameters:\r\n  EnvType:\r\n    Description: Environment type.\r\n    Default: test\r\n    Type: String\r\n    AllowedValues:\r\n      - prod\r\n      - test\r\n    ConstraintDescription: must specify prod or test.\r\n\r\nConditions:\r\n  CreateProdResources: !Equals [ !Ref EnvType, prod ]\r\n\r\nResources:\r\n  EC2Instance:\r\n    Type: \"AWS::EC2::Instance\"\r\n    Properties:\r\n      ImageId: !FindInMap [RegionMap, !Ref \"AWS::Region\", AMI]\r\n      InstanceType: t2.micro\r\n      AvailabilityZone: !FindInMap [RegionMap, !Ref \"AWS::Region\", TestAz]\r\n\r\n  MountPoint:\r\n    Type: \"AWS::EC2::VolumeAttachment\"\r\n    Condition: CreateProdResources\r\n    Properties:\r\n      InstanceId:\r\n        !Ref EC2Instance\r\n      VolumeId:\r\n        !Ref NewVolume\r\n      Device: \/dev\/sdh\r\n\r\n  NewVolume:\r\n    Type: \"AWS::EC2::Volume\"\r\n    Condition: CreateProdResources\r\n    Properties:\r\n      Size: 100\r\n      AvailabilityZone:\r\n        !GetAtt EC2Instance.AvailabilityZone\r\n\r\nOutputs:\r\n  VolumeId:\r\n    Condition: CreateProdResources\r\n    Value:\r\n      !Ref NewVolume\r\n<\/pre>\n<p>Description:<\/p>\n<ul>\n<li>If Parameter EnvType = &#8216;prod&#8217;:\n<ul>\n<li>Create VolumeAttachment &#8220;MountPoint&#8221; on EC2Instance and mount NewVolume to it at \/dev\/sdh<\/li>\n<li>Create Volume &#8220;NewVolume&#8221; in the same AZ as EC2Instance and make it 100GB<\/li>\n<li>Output VolumeId with the ID of NewVolume<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><a name=\"Functions\"><\/a><\/p>\n<h2>Conditions Functions<\/h2>\n<p><a href=\"https:\/\/www.udemy.com\/aws-cloudformation-master-class\/learn\/v4\/t\/lecture\/8139162?start=0\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/aws-cloudformation-master-class\/learn\/v4\/t\/lecture\/8139162?start=0<\/a><\/p>\n<p>Ha! He only points to the documentation!\u00a0 LAME!<\/p>\n<p><a href=\"http:\/\/docs.aws.amazon.com\/AWSCloudFormation\/latest\/UserGuide\/intrinsic-function-reference-conditions.html\" target=\"_blank\" rel=\"noopener\">http:\/\/docs.aws.amazon.com\/AWSCloudFormation\/latest\/UserGuide\/intrinsic-function-reference-conditions.html<\/a><\/p>\n<p><a name=\"GetAtt\"><\/a><\/p>\n<h2>Fn::GetAtt (Get Attribute)<\/h2>\n<p><a href=\"https:\/\/www.udemy.com\/aws-cloudformation-master-class\/learn\/v4\/t\/lecture\/8162166?start=0\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/aws-cloudformation-master-class\/learn\/v4\/t\/lecture\/8162166?start=0<\/a><\/p>\n<ul>\n<li>Attributes are attached to any resources you create<\/li>\n<li>To know the attributes that exist for each resource, you need to look at the documentation<\/li>\n<\/ul>\n<pre>Fn::GetAtt: [ logicalNameOfResource, attributeName ]\r\n!GetAtt logicalNameOfResource.attributeName<\/pre>\n<p>*Notice the OOP notation! \ud83d\ude42<br \/>\n<a name=\"Quiz\"><\/a><\/p>\n<h2>Quiz<\/h2>\n<h4>Conditions cannot be applied to<\/h4>\n<ul>\n<li>Resources<\/li>\n<li>Outputs<\/li>\n<li><strong>Parameters<\/strong><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Course Main Menu Section 7 Main Menu Overview Hands On Functions Fn::GetAtt (Get Attribute) Quiz Overview https:\/\/www.udemy.com\/aws-cloudformation-master-class\/learn\/v4\/t\/lecture\/8162164?start=0 What are conditionals used for? Conditionals are used to control the creation of resources or outputs based on a condition. Conditions can be whatever you want them to be, but common ones are: Environment (dev \/ test \/ ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=1155\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-1155","post","type-post","status-publish","format-standard","hentry","category-cloudformation"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1155"}],"version-history":[{"count":7,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1155\/revisions"}],"predecessor-version":[{"id":1163,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1155\/revisions\/1163"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}