{"id":4221,"date":"2021-03-14T17:45:16","date_gmt":"2021-03-14T16:45:16","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=4221"},"modified":"2021-06-01T17:56:28","modified_gmt":"2021-06-01T15:56:28","slug":"cloudformation-2-intrinsic-functions-user-data","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2021\/03\/14\/cloudformation-2-intrinsic-functions-user-data\/","title":{"rendered":"CloudFormation &#8211; 2 &#8211; Intrinsic Functions"},"content":{"rendered":"<p><!--more--><\/p>\n<p><span style=\"color: #3366ff;\">Intrinsic Functions<\/span><\/p>\n<p><strong><code>Fn::Ref<\/code><\/strong><\/p>\n<ul>\n<li>The <code>Fn::Ref<\/code> function can be leveraged to reference\n<ul>\n<li>Parameters =&gt; returns the value of the parameter<\/li>\n<li>Resources =&gt; returns the physical ID of the underlying resource (ex: EC2 ID)<\/li>\n<\/ul>\n<\/li>\n<li>The shorthand for this in YA\\\/1_ is <code>!Ref<\/code><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">DbSubnetl: \r\n  Type: AWS::EC2::Subnet \r\n  Properties: \r\n    VpcId: !Ref MyVPC<\/pre>\n<p>&nbsp;<\/p>\n<p><strong><code>Fn::GetAtt<\/code> <\/strong><\/p>\n<ul>\n<li>Attributes are attached to any resources you create<\/li>\n<li>To know the attributes of your resources, the best place to look at is the documentation.<\/li>\n<li>For example: the AZ of an EC2 machine !<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">Resources: \r\n  EC2lnstance: \r\n    Type: \"AWS::EC2::Instance\" \r\n    Properties: \r\n      Imageld: ami-1234567 \r\n      InstanceType: t2.micro<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">NewVolume: \r\n  Type: \"AWS::EC2::Volume\" \r\n  Condition: CreateProdResources \r\n  Properties: \r\n    Size: 100 \r\n    AvailabilityZone: \r\n      !GetAtt EC2lnstance.AvailabilityZone<\/pre>\n<p>&nbsp;<\/p>\n<p><strong><code>Fn::FindInMap<\/code> <\/strong><\/p>\n<p>Accessing Mapping Values<\/p>\n<ul>\n<li>We use <code>Fn::FindInMap<\/code> to return a named value from a specific key<\/li>\n<li><code>!FindInMap [ MapName, TopLevelKey, SecondLevelKey ]<\/code><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">AWSTemplateFormatVersion: \"2010-09-09\" \r\nMappings: \r\n  RegionMap: \r\n    us-east-1: \r\n      \"32\": \"ami-6411e20d\" \r\n      \"64\": \"ami-7a11e213\" \r\n    us-west-1: \r\n      \"32\": \"ami-c9c7978c\" \r\n      \"64\": \"ami-cfc7978a\" \r\n    eu-west-1: \r\n      \"32\": \"ami-37c2f643\" \r\n      \"64\": \"ami-31c2f645\" \r\n    ap-southeast-1: \r\n      \"32\": \"ami-66f28c34\" \"\r\n       64\": \"ami-60f28c32\" \r\n    ap-northeast-1: \r\n      \"32\": \"ami-9c03a89d\" \r\n      \"64\": \"ami-a003a8a1\" \r\nResources: \r\n  myEC2Instance: \r\n    Type: \"AWS::EC2::Instance\" \r\n    Properties: \r\n      Imageld: !FindInMap [RegionMap, !Ref \"AWS::Region\", 32]\r\n      InstanceType: m1.small\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong><code>Fn:ImportValue<\/code><\/strong><\/p>\n<ul>\n<li>Import values that are exported in other templates<\/li>\n<li>For this, we use the <code>Fn::ImportValue<\/code> function<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">Resources: \r\n  MySecureInstance: \r\n    Type: AWS::EC2::Instance \r\n    Properties: \r\n      AvailabilityZone: us-east-la \r\n      Imageld: ami-a4c7edb2 \r\n      InstanceType: t2.micro \r\n      SecurityGroups: \r\n        - !ImportValue SSHSecurityGroup<\/pre>\n<p>&nbsp;<\/p>\n<p><strong><code>Fn::Join<\/code> <\/strong><\/p>\n<ul>\n<li>Join values with a delimiter<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">!Join [ delimiter, [ comma-delimited list of values ] ]<\/pre>\n<ul>\n<li>This creates &#8220;a:b:c&#8221; :<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">!Join [ \":\" , [ a, b, c ] ]<\/pre>\n<p>&nbsp;<\/p>\n<p>Function <code><strong>Fn::Sub<\/strong><\/code><\/p>\n<ul>\n<li><code>Fn::Sub<\/code>, or<code> !Sub<\/code> as a shorthand, is used to substitute variables from a text. It&#8217;s a very handy function that will allow you to fully customize your templates.<\/li>\n<li>For example, you can combine <code>Fn::Sub<\/code> with References or AWS Pseudo variables !<\/li>\n<li>String must contain <code>${VariableName}<\/code> and will substitute them<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">!Sub\r\n  - String \r\n  - { VarlName: VarlValue, Var2Name: Var2Value }<\/pre>\n<pre class=\"lang:default decode:true \">!Sub String<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #3366ff;\">Conditions Functions<\/span><\/p>\n<pre class=\"lang:default decode:true \">Conditions:\r\n  CreateProdResources: !Equals [ !Ref EnvType, prod ]<\/pre>\n<ul>\n<li>The logical ID if for you to choose. It&#8217;s how you name condition<\/li>\n<li>The intrinsic function (logical) can be any of the following:<\/li>\n<li><code>Fn::And<\/code><\/li>\n<li><code>Fn::Equals<\/code><\/li>\n<li><code>Fn::If<\/code><\/li>\n<li><code>Fn::Not<\/code><\/li>\n<li><code>Fn::Or<\/code><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":4222,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/4221"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=4221"}],"version-history":[{"count":3,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/4221\/revisions"}],"predecessor-version":[{"id":4228,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/4221\/revisions\/4228"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media\/4222"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=4221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=4221"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=4221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}