{"id":4295,"date":"2021-04-11T09:44:45","date_gmt":"2021-04-11T07:44:45","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=4295"},"modified":"2024-05-24T15:41:31","modified_gmt":"2024-05-24T13:41:31","slug":"cloudformation-5","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2021\/04\/11\/cloudformation-5\/","title":{"rendered":"CloudFormation &#8211; 5 &#8211; Parameters"},"content":{"rendered":"<p><!--more--><\/p>\n<p><span style=\"color: #3366ff;\">Parameters from SSM<\/span><\/p>\n<p>Consider such a yaml template:<\/p>\n<pre class=\"lang:default decode:true \"># this will pickup the latest value of the parameter every time the template is run\r\n# the parameter value must be the Key for that Parameter. Then CFN will retrieve the value\r\n# this does not support SecureString SSM Parameter types\r\nParameters:\r\n  InstanceType:\r\n    Type: 'AWS::SSM::Parameter::Value&lt;String&gt;'\r\n    Default: \/EC2\/InstanceType\r\n\r\n  ImageId: \r\n    Type: 'AWS::SSM::Parameter::Value&lt;AWS::EC2::Image::Id&gt;'\r\n    Default: \/EC2\/AMI_ID\r\n\r\nResources:\r\n  # this instance will be automatically updated \r\n  # when we change the parameter in SSM ourselves\r\n  # and then run the CloudFormation template\r\n  MyInstanceWithParameters:\r\n    Type: AWS::EC2::Instance\r\n    Properties:\r\n      ImageId: !Ref ImageId\r\n      InstanceType: !Ref InstanceType\r\n<\/pre>\n<p>Now let&#8217;s create the parameteres in SSM:<\/p>\n<p><code>AWS Systems Manager -&gt; Parameter Store -&gt; Create parameter<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4296 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation079.jpg\" alt=\"\" width=\"464\" height=\"793\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation079.jpg 464w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation079-176x300.jpg 176w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/p>\n<p>Now go and create the second parameter. First copy the AMI Id.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4297 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation080.jpg\" alt=\"\" width=\"855\" height=\"224\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation080.jpg 855w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation080-300x79.jpg 300w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation080-768x201.jpg 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>And create parameter:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4298 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation081.jpg\" alt=\"\" width=\"652\" height=\"787\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation081.jpg 652w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation081-249x300.jpg 249w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/p>\n<p><code>Create -&gt;<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4300 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation082.jpg\" alt=\"\" width=\"736\" height=\"362\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation082.jpg 736w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation082-300x148.jpg 300w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/p>\n<p>And now go and create the stack<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4302 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation083.jpg\" alt=\"\" width=\"693\" height=\"591\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation083.jpg 693w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation083-300x256.jpg 300w\" sizes=\"(max-width: 693px) 100vw, 693px\" \/><\/p>\n<p><code>Next -&gt;<\/code><\/p>\n<p>The stack is being created. On the parameteres tab we see used parameteres and the values:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4305 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation084.jpg\" alt=\"\" width=\"840\" height=\"390\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation084.jpg 840w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation084-300x139.jpg 300w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation084-768x357.jpg 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>When we go to the created EC2 instance we see that instance type and the AMI is which was selected:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4307 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation085.jpg\" alt=\"\" width=\"770\" height=\"707\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation085.jpg 770w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation085-300x275.jpg 300w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation085-768x705.jpg 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Now let&#8217;s change the value of an AMI parameter:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4309 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation086.jpg\" alt=\"\" width=\"857\" height=\"126\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation086.jpg 857w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation086-300x44.jpg 300w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation086-768x113.jpg 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4311 size-full aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation088-1.jpg\" alt=\"\" width=\"696\" height=\"756\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation088-1.jpg 696w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation088-1-276x300.jpg 276w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><\/p>\n<p>And update the stack:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4313 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation089-1.jpg\" alt=\"\" width=\"714\" height=\"324\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation089-1.jpg 714w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation089-1-300x136.jpg 300w\" sizes=\"(max-width: 714px) 100vw, 714px\" \/><\/p>\n<p>Next -&gt; Next -&gt;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4316 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation091.jpg\" alt=\"\" width=\"795\" height=\"588\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation091.jpg 795w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation091-300x222.jpg 300w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation091-768x568.jpg 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>AMI of EC2 instance has been changed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4318 size-full aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation090.jpg\" alt=\"\" width=\"796\" height=\"805\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation090.jpg 796w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation090-297x300.jpg 297w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation090-768x777.jpg 768w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation090-100x100.jpg 100w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #3366ff;\">Public Parameters from SSM<\/span><\/p>\n<p>Some AWS services publish information about common artifacts as AWS Systems Manager <em>public<\/em> parameters. For example, the Amazon Elastic Compute Cloud (Amazon EC2) service publishes information about Amazon Machine Images (AMIs) as public parameters.<\/p>\n<p>Consider such a yaml template:<\/p>\n<pre class=\"lang:default decode:true\"># this will pickup the latest value of the parameter every time the template is run\r\n# the parameter value must be the Key for that Parameter. Then CFN will retrieve the value\r\n# this does not support SecureString SSM Parameter types\r\nParameters:\r\n  LatestLinuxAmiId:\r\n    Type: 'AWS::SSM::Parameter::Value&lt;AWS::EC2::Image::Id&gt;'\r\n    # obtain list with\r\n    # aws ssm get-parameters-by-path --path \/aws\/service\/ami-amazon-linux-latest  --query 'Parameters[].Name'\r\n    Default: '\/aws\/service\/ami-amazon-linux-latest\/amzn2-ami-hvm-x86_64-gp2'\r\n\r\n  # this works for Windows too\r\n  LatestWindowsAmiId:\r\n    Type: 'AWS::SSM::Parameter::Value&lt;AWS::EC2::Image::Id&gt;'\r\n    # obtain list with\r\n    # aws ssm get-parameters-by-path --path \"\/aws\/service\/ami-windows-latest\" --region us-east-1\r\n    Default: '\/aws\/service\/ami-windows-latest\/Windows_Server-2016-English-Core-Base'\r\n\r\nResources:\r\n  # this instance will be automatically updated \r\n  # when AWS releases a new AMI\r\n  # and then run the CloudFormation template\r\n  MyInstanceWithParameters:\r\n    Type: AWS::EC2::Instance\r\n    Properties:\r\n      ImageId: !Ref LatestLinuxAmiId\r\n      InstanceType: t2.micro\r\n<\/pre>\n<p>To obtain the list of all available linux AMI by AWS:<\/p>\n<pre class=\"lang:default decode:true\">C:\\Users\\mirth&gt;aws ssm get-parameters-by-path --path \/aws\/service\/ami-amazon-linux-latest --query \"Parameters[].Name\"\r\n[\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn-ami-hvm-x86_64-ebs\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn-ami-hvm-x86_64-gp2\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn-ami-hvm-x86_64-s3\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn-ami-minimal-hvm-x86_64-s3\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn-ami-minimal-pv-x86_64-s3\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn-ami-pv-x86_64-s3\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn2-ami-hvm-arm64-gp2\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn2-ami-hvm-x86_64-ebs\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn2-ami-hvm-x86_64-gp2\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn2-ami-minimal-hvm-arm64-ebs\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn-ami-minimal-hvm-x86_64-ebs\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn-ami-minimal-pv-x86_64-ebs\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn-ami-pv-x86_64-ebs\",\r\n    \"\/aws\/service\/ami-amazon-linux-latest\/amzn2-ami-minimal-hvm-x86_64-ebs\"\r\n]\r\n<\/pre>\n<p>Or all available Windows AMI:<\/p>\n<pre class=\"lang:default decode:true \">C:\\Users\\mirth&gt;aws ssm get-parameters-by-path --path \"\/aws\/service\/ami-windows-latest\" --region eu-central-1\r\n{\r\n    \"Parameters\": [\r\n        {\r\n            \"Name\": \"\/aws\/service\/ami-windows-latest\/Windows_Server-2012-R2_RTM-Chinese_Simplified-64Bit-Base\",\r\n            \"Type\": \"String\",\r\n            \"Value\": \"ami-0ba5ffc543cb0f4b9\",\r\n            \"Version\": 66,\r\n            \"LastModifiedDate\": \"2021-06-11T02:09:00.341000+02:00\",\r\n            \"ARN\": \"arn:aws:ssm:eu-central-1::parameter\/aws\/service\/ami-windows-latest\/Windows_Server-2012-R2_RTM-Chinese_Simplified-64Bit-Base\",\r\n            \"DataType\": \"text\"\r\n        },\r\n        {\r\n            \"Name\": \"\/aws\/service\/ami-windows-latest\/Windows_Server-2012-R2_RTM-Chinese_Traditional-64Bit-Base\",\r\n            \"Type\": \"String\",\r\n            \"Value\": \"ami-0b71756f3f2cb18d1\",\r\n            \"Version\": 66,\r\n            \"LastModifiedDate\": \"2021-06-11T02:09:18.679000+02:00\",\r\n            \"ARN\": \"arn:aws:ssm:eu-central-1::parameter\/aws\/service\/ami-windows-latest\/Windows_Server-2012-R2_RTM-Chinese_Traditional-64Bit-Base\",\r\n            \"DataType\": \"text\"\r\n        },\r\n        {\r\n            \"Name\": \"\/aws\/service\/ami-windows-latest\/Windows_Server-2012-R2_RTM-Dutch-64Bit-Base\",\r\n            \"Type\": \"String\",\r\n            \"Value\": \"ami-0104d8db9b2ce0abf\",\r\n            \"Version\": 66,\r\n            \"LastModifiedDate\": \"2021-06-11T02:09:36.595000+02:00\",\r\n            \"ARN\": \"arn:aws:ssm:eu-central-1::parameter\/aws\/service\/ami-windows-latest\/Windows_Server-2012-R2_RTM-Dutch-64Bit-Base\",\r\n            \"DataType\": \"text\"\r\n        },\r\n        {\r\n            \"Name\": \"\/aws\/service\/ami-windows-latest\/Windows_Server-2012-R2_RTM-Hungarian-64Bit-Base\",\r\n            \"Type\": \"String\",\r\n            \"Value\": \"ami-000f505b6d84d1fe8\",\r\n            \"Version\": 65,\r\n            \"LastModifiedDate\": \"2021-06-11T02:11:59.592000+02:00\",\r\n            \"ARN\": \"arn:aws:ssm:eu-central-1::parameter\/aws\/service\/ami-windows-latest\/Windows_Server-2012-R2_RTM-Hungarian-64Bit-Base\",\r\n            \"DataType\": \"text\"\r\n        },\r\n        {\r\n            \"Name\": \"\/aws\/service\/ami-windows-latest\/Windows_Server-2012-R2_RTM-Japanese-64Bit-Base\",\r\n            \"Type\": \"String\",\r\n            \"Value\": \"ami-0ad37555d48e09d90\",\r\n            \"Version\": 66,\r\n            \"LastModifiedDate\": \"2021-06-11T02:12:19.789000+02:00\",\r\n            \"ARN\": \"arn:aws:ssm:eu-central-1::parameter\/aws\/service\/ami-windows-latest\/Windows_Server-2012-R2_RTM-Japanese-64Bit-Base\",\r\n            \"DataType\": \"text\"\r\n        },\r\n        {\r\n-- More  --<\/pre>\n<p>Let&#8217;s go and run this template:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4322 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation19.jpg\" alt=\"\" width=\"713\" height=\"768\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation19.jpg 713w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation19-279x300.jpg 279w\" sizes=\"(max-width: 713px) 100vw, 713px\" \/><\/p>\n<p><code>Next-&gt;<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4323 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation20.jpg\" alt=\"\" width=\"724\" height=\"587\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation20.jpg 724w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation20-300x243.jpg 300w\" sizes=\"(max-width: 724px) 100vw, 724px\" \/><\/p>\n<p><code>Next -&gt; Next<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4327 size-full aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation21-1.jpg\" alt=\"\" width=\"802\" height=\"666\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation21-1.jpg 802w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation21-1-300x249.jpg 300w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation21-1-768x638.jpg 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p><code>Create stack -&gt;<\/code><\/p>\n<p>As we see the value of the parameters has been resolved:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4329 aligncenter\" src=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation22.jpg\" alt=\"\" width=\"820\" height=\"443\" srcset=\"http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation22.jpg 820w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation22-300x162.jpg 300w, http:\/\/miro.borodziuk.eu\/wp-content\/uploads\/CloudFormation22-768x415.jpg 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>Read more at: https:\/\/docs.aws.amazon.com\/systems-manager\/latest\/userguide\/parameter-store-public-parameters.html<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":4331,"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\/4295"}],"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=4295"}],"version-history":[{"count":16,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/4295\/revisions"}],"predecessor-version":[{"id":4334,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/4295\/revisions\/4334"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media\/4331"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=4295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=4295"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=4295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}