未知数据源 2024年10月02日
Introducing new language extensions in AWS CloudFormation
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

AWS CloudFormation发布新语言扩展,包括JSON字符串转换、长度计算及对更新和删除策略中内在函数和伪参数引用的支持。这些扩展是与社区开放讨论的结果,本文还介绍了如何使用这些新特性及相关注意事项。

🎯AWS CloudFormation是一项基础设施即代码服务,新语言扩展包括用于JSON字符串转换的Fn::ToJsonString,它接受数组或对象及内在函数并转换为转义JSON字符串,但不支持!Ref AWS::NotificationARNs。

📏Fn::Length内在函数可返回给定列表的长度,支持List和CommaDelimitedList类型的参数,可与其他内在函数结合,但存在一些限制,如只能在Transforms运行时计算已知值。

💪对DeletionPolicy和UpdateReplacePolicy的内在支持,解决了之前无法动态解析内在函数或参数引用的限制,可从模板的Parameters、Mappings和Conditions部分解析值。

📋使用新语言特性需在模板的transform部分添加AWS::LanguageExtensions,若有多个转换,建议将AWS managed transforms置于末尾,AWS::LanguageExtensions须在AWS::Serverless之前列出。

<section class="blog-post-content"><p><a href="https://aws.amazon.com/cloudformation/&quot;&gt;AWS CloudFormation</a>, an Infrastructure as Code (IaC) service that lets you model, provision, and manage AWS and third-party resources, recently released a new language transform that enhances the core CloudFormation language. For our first release, these enhancements are new intrinsic functions for JSON string conversion (<a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ToJsonString.html&quot;&gt;Fn::ToJsonString&lt;/a&gt;), length (<a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-length.html&quot;&gt;Fn::Length&lt;/a&gt;), and support for <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/function-refs-in-policy-attributes.html&quot;&gt;intrinsic functions and pseudo-parameter references</a> in update and deletion policies.</p><p>These new language extensions are the result of open discussions with the larger CloudFormation community via our Request For Comments (RFC) proposals for new language features at our <a href="https://github.com/aws-cloudformation/cfn-language-discussion&quot;&gt;Language Discussion Github repository</a>. We want to collaborate with the community to better align features and incorporate early feedback into the development cycle to meet the community’s needs. We invite you to participate in new RFCs to help shape the future of the CloudFormation language.</p><p>In this post, I’ll dive deeper into what these new extensions mean for your authoring experience, as well as give a few examples of how to use them.</p><h2>Prerequisites</h2><p>To use these new language features, you must add AWS::LanguageExtensions to the transform section of your template.</p><p>If you have a list of transforms, then we recommend having AWS managed transforms at the end, and AWS::LanguageExtensions must be listed before AWS::Serverless.</p><p>This transform will cover all of the existing and future language extensions.</p><h2>Fn::ToJsonString</h2><p>Sometimes, resources require a fully-composed JSON string as an input.</p><p>Although this does work and is currently fully-supported by CloudFormation, it’s not easy to read at-a-glance.</p><p><a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ToJsonString.html&quot;&gt;Fn::ToJsonString&lt;/a&gt; lets us significantly simplify this example and re-introduce readability:</p><p>Fn::ToJsonString accepts either an array or an object, as well as intrinsic functions, and converts it into an escaped JSON string. The only intrinsic function not supported is !Ref AWS::NotificationARNs.</p><p>You can see more examples and the actual RFC at the <a href="https://github.com/aws-cloudformation/cfn-language-discussion/blob/main/RFCs/0014-ToJsonString.md&quot;&gt;CloudFormation GitHub RFC Repo</a></p><h2>Fn::Length</h2><p>The Fn::Length intrinsic function returns the length of a given list. Consider the following example:</p><p>In this scenario, Fn::Length will consider the immediately following list and return the value of three (the number of items in the list). This means that the !Equals function will evaluate to true. Finally, the condition IsLengthThree will evaluate to true in this template.</p><p>The Length intrinsic function also supports parameters of the type List&lt;Number&gt; and CommaDelimitedList, and it can be combined with other intrinsic functions, such as !Select or !Split.</p><p>This block of code selects either the last subnet or the first subnet in the parameter CIDRblocks. Before, you couldn’t bounds-check your lists using !Select, but with Fn::Length you can! Note that we didn’t have to !Split our CommaDelimitedList, the Fn::Length function did that for us.</p><p>There are some minor limitations to Fn::Length. It can currently only compute values that are known when <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html&quot;&gt;Transforms&lt;/a&gt; are run, not at resource provisioning time. For example, this means that getting the length of a split <a href="https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html&quot;&gt;Amazon Resource Names (ARNs)</a> won’t work.</p><p>See more examples and the actual RFC at the <a href="https://github.com/aws-cloudformation/cfn-language-discussion/blob/main/RFCs/0070-Fn::Length.md&quot;&gt;CloudFormation GitHub RFC Repo</a>.</p><h2>Intrinsic support for DeletionPolicy and UpdateReplacePolicy</h2><p>One of the previous limitations of <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html&quot;&gt;Deletion&lt;/a&gt; and <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatereplacepolicy.html&quot;&gt;UpdateReplace&lt;/a&gt; policies was that they couldn’t dynamically resolve intrinsic functions or parameter references. This limitation is now removed with language extensions! You can resolve values from the Parameters, <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html&quot;&gt;Mappings&lt;/a&gt;, and <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html&quot;&gt;Conditions&lt;/a&gt; sections of your CloudFormation templates.</p><p>A common use case that we’ve heard is having DeletionPolicy: Retain only in production. It’s now trivial to have this using Parameters and Mappings:</p><p>Here is the same example, represented with Conditions instead:</p><p>Find all of our examples and the actual RFC at the <a href="https://github.com/aws-cloudformation/cfn-language-discussion/blob/main/RFCs/0011-DeletionPolicy.md&quot;&gt;CloudFormation GitHub RFC Repo</a>.</p><h2>Conclusion</h2><p>In this post, we walked through the new CloudFormation language extensions transform, how to enable them in your templates, and how to engage in future language extensions via our open language discussion repository. We have more language extensions planned, and your feedback can help shape the future of the CloudFormation language. Leave us your feedback at our <a href="https://github.com/aws-cloudformation/cfn-language-discussion&quot;&gt;Language Discussion Github repository</a>. We look forward to hearing from you!</p><p><strong>About the Author:</strong></p></section>

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

AWS CloudFormation 语言扩展 Fn::ToJsonString Fn::Length 内在支持
相关文章