服务
关于
CloudProse博客
开发人员经验

CloudFormation YAML在这里,并且它's awesome

贾里德·崔特(Jared Short Trek)
贾里德·肖特(Jared Short) | 2016年9月20日

2016年9月20日星期二

您可能最近已经看到有关Trek10的公告,这是自CloudFormation本身以来对CloudFormation的最大更新。 AWS CloudFormation更新– YAML,跨堆栈引用,简化的替代{:target =“ _ blank”}。

YAML更具可读性,更易于使用,更易于浏览,并且使我的头部受到的伤害小得多……但是显而易见的是,我有何感想?

内联评论

“Comment your 码” is the mantra we 所有 love to 预先tend saves us from documentation, but with JSON you had to resort to either a build step to rip out inline comments, or just 预先tend the problem doesn’t exist. But those days are over, no more thousand line JSON blobs with no inline hints to help you sort out the madness. YAML supports inline comments, and it is beautiful.

之前: (告诉我:该安全组起什么作用?此版本从什么版本的Linux开始?)

{ "Parameters" : { "KeyName" : { "Description" : "The EC2 Key Pair to 所有ow SSH access to the instance", "Type" : "AWS::EC2::KeyPair::KeyName" } }, "Resources" : { "Ec2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "SecurityGroups" : [ "MyExistingSecurityGroup" ], "KeyName" : { "Ref" : "KeyName"}, "ImageId" : "ami-6869aa05" } } }}

后: (相同的问题…内联回答!)

<pre><code>参数:KeyName:说明:允许SSH访问实例的EC2密钥对类型:AWS :: EC2 :: KeyPair :: KeyName资源:Ec2Instance:类型:AWS :: EC2 :: Instance属性:SecurityGroups:-MyExistingSecurityGroup#SSH访问适用于CloudOps团队VPN KeyName:Ref:KeyName ImageId:ami-6869aa05#Amazon Linux 2016.03.3</code></pre>

交叉堆栈参考

如果您已经使用CloudFormation了一段时间,那么毫无疑问,您会问自己:“为什么必须为这些其他堆栈创建的内容输入这些参数?为什么不知道?”我们问自己。日常。

我很自豪地告诉您,在使用AWS Lambda和自定义支持的资源之前,我们开始进行交叉堆栈引用。但是,这是一个solution肿,丑陋的解决方案。我从来没有更高兴将解决方案退回去并使其摆脱困境。

AWS now provides an elegant Cross Stack solution that, if you follow practical naming conventions, is 预先tty darn powerful.

之前:

<pre><code>{“ Parameters”:{“ KeyName”:{“ Description”:“允许SSH访问实例的EC2密钥对”,“ Type”:“ AWS :: EC2 :: KeyPair :: KeyName”,“默认”: “ cloudops-key”},“ InstanceType”:{“ Type”:“ String”,“ Default”:“ t2.medium”},“ Ec2SecurityGroup”:{“ Type”:“ AWS :: EC2 :: SecurityGroup :: Id“,”默认“:” sg-1234567“},” ImageId“:{”类型“:”字符串“,”默认“:” ami-6869aa05“}},”资源“:{” Ec2Instance“:{”类型“:” AWS :: EC2 :: Instance“,”属性“:{” ImageId“:{” Ref“:” ImageId“},” KeyName“:{” Ref“:” KeyName“},” InstanceType“: {“ Ref”:“ InstanceType”},“ SecurityGroups”:[{“ Ref”:“ Ec2SecurityGroup”}]}}}}</code></pre>

后:

<pre><code>资源:Ec2Instance:类型:AWS :: EC2 :: Instance属性:SecurityGroups:-!ImportValue CloudOpsSecurityGroup InstanceType:!ImportValue DefaultsInstanceType键名:!ImportValue CloudOpsSSHKey ImageId:!ImportValue DefaultsImageId</code></pre>

Note: Yes, you could use sub-templates to get somewhat this functionality 预先viously… but if you’ve ever used them in any kind of rapidly moving environment with lots of sub-stack dependencies, you know what I mean when I say “I don’t hate myself quite that much”.

更好的“脚本”和字符串替换

用户数据和cloud-init文件非常便于从CloudFormation模板配置ec2实例,而无需通过构建管道滚动自己的AMI。就是说,JSON中用于将这些脚本组合在一起的语法是-,您可以忘记任何有用的语法突出显示。

之前: (甚至不尝试阅读这篇文章……只是欣赏它的丑陋)

<pre><code>“ ContainerInstances”:{“ Type”:“ AWS :: AutoScaling :: LaunchConfiguration”,“ Metadata”:{“ AWS :: CloudFormation :: Init”:{“ config”:{“ commands”:{“ 01_add_instance_to_cluster”:{ “ command”:{“ Fn :: Join”:[“”,[“#!/ bin / bash \ n”,“ echo ECS_CLUSTER =”,{“ Ref”:“ ECSCluster”},“>>/etc/ecs/ecs.config“]]}}},” files“:{” /etc/cfn/cfn-hup.conf“:{” content“:{” Fn :: Join“:[”“, [“ [main] \ n”,“ stack =”,{“ Ref”:“ AWS :: StackId”},“ \ n”,“ region =”,{“ Ref”:“ AWS :: Region”}, “ \ n”]]},“ mode”:“ 000400”,“ owner”:“ root”,“ group”:“ root”},“ /etc/cfn/hooks.d/cfn-auto-reloader.conf “:{” content“:{” Fn :: Join“:[”“,[” [cfn-auto-reloader-hook] \ n“,” triggers = post.update \ n“,” path = Resources.ContainerInstances .Metadata.AWS :: CloudFormation :: Init \ n“,” action = / opt / aws / bin / cfn-init -v“,” --stack“,{” Ref“:” AWS :: StackName“}, “ --resource ContainerInstances”,“ --region”,{“ Ref”:“ AWS :: Region”},“ \ n”,“ runas = root \ n”]]}}}},“服务”:{“ sysvinit“:{”“ cfn-hup”:{“ enabled”:“ true”,“ ensureRunning”:“ true”,“ files”:[“ /etc/cfn/cfn-hup.conf”,“ / etc / cfn /hooks.d/cfn-auto-reloader.conf”]}}}}}}</code></pre>

后:

ContainerInstances: Type: AWS::AutoScaling::LaunchConfiguration Metadata: AWS::CloudFormation::Init: config: commands: 01_add_instance_to_cluster: command: !Sub | #!/bin/bash echo ECS_CLUSTER=${ECSCluster} >> /etc/ecs/ecs.config files: "/etc/cfn/cfn-hup.conf": content: !Sub | [main] stack=${AWS::StackId} region=${AWS::Region} mode: '000400' owner: root group: root "/etc/cfn/hooks.d/cfn-auto-reloader.conf": content: !Sub | [cfn-auto-reloader-hook] triggers=post.update path=Resources.ContainerInstances.Metadata.AWS::CloudFormation::Init action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource ContainerInstances --region ${AWS::Region} runas=root services: sysvinit: cfn-hup: enabled: 'true' ensureRunning: 'true' files: - "/etc/cfn/cfn-hup.conf" - "/etc/cfn/hooks.d/cfn-auto-reloader.conf"

昼夜不同!

我仍然希望它能做什么?

拥有YAML十分壮观,但是我们也想利用该语法的一些更强大的功能。例如, YAML别名{:target =“ _ blank”},因此我们可以利用强大的哈希合并功能!

All that said, 预先tty cool and much needed update!

作者
贾里德·崔特(Jared Short Trek)
贾里德·肖特(Jared Short)