- 1. AWS CloudFormation - Section Introduction
- 2. CloudFormation Overview
- 3. CloudFormation Create Stack Hands On
- 4. CloudFormation Update and Delete Stack Hands On
- 5. YAML Crash Course(YAML速成班)
- 6. CloudFormation Resources
- 7. CloudFormation Parameters
- 8. CloudFormation Mappings
- 9. CloudFormation Outputs(popular exam questions)
- 10. CloudFormation Conditions
- 11. CloudFormation Intrinsic Functions
- 12. CloudFormation Rollbacks
- 13. CloudFormation ChangeSets, Nested Stacks & StackSet
1. AWS CloudFormation - Section Introduction
2. CloudFormation Overview
Infrastructure as Code
What is CloudFormation
CloudFormation is a declarative way of outlining your AWS Infrastructure, for any resources:CloudFormation
是一种声明式的方式,用于概述您的 AWS 基础架构,适用于任何资源。
CloudFormation可以以正确的顺序和正确的配置创建
Benifit of AWS CloudFormation
How CloudForamtion Works
- Templates have to be uploaded in S3 and then referenced in CloudFormation:模板必须上传到S3中,然后在CloudFormation中引用。
- 要更新一个模板,我们不能编辑以前的模板。我们必须重新上载一个新版本的模板到AWS
- 堆栈由一个名称来标识
- 删除堆栈会删除CloudFormation创建的每一个artifact。
Deploying CloudFormation templates
CloudFormation Building Blocks(CloudForm构建模块)
- 少动手,明白CloudFormation怎么工作
- 考试要求你会读,不需要写
3. CloudFormation Create Stack Hands On
Introductory Example
- forget about the code syntax(忘记code的语法)
We’ll see how in no-time, we are able to get started with CloudFormation(如何在短时间内,学会使用CloudFormation)
Hands-On
Step1:查看之前创建的CloudFormation
点击进入第一个BeanStalk中创建的CloudFormation,
点击 资源
然后发现通过Beanstalk创建了16个资源
然后点击Template
会发现有大量JSON格式的代码
然后点击右上角的“在设计器中查看”
可以看到整个设计的UI和对应的JSON、YAML文件Step2:创建自己的CloudFormation
首先切换到US-East-1
在弗吉尼亚创建自己的新的CloudFormation
然后依次设置
文件选择
然后创建Stack details
后面保持默认设置
后面就建立好了,
打开EC2会发现创建的EC2里有三个tag
打开这个CloudFormation,点击template会发现Code形式创建
然后点击进入View in Designer
就可以看见这个Simple Template
2020-11-244. CloudFormation Update and Delete Stack Hands On
默认设置一些文件
回到 CloudFormation 中,点击Update
然后点击替换现有的template,然后点击上传template file,并选择上方截图的file
上传这个文件
然后设置一个SG description
后续的都默认
在后面的审查中,可以看到上传“1-ec2-with-sg-eip.yaml”后,EC2发生了改变,并且新加了EIP,SG的内容,都可以在这个change-set中看到
然后来到 first-stack 中
发现code书写顺序和实际创建顺序不一样,那是由 CloudFormation 自己决定的
一切OK后点击“删除”
它会自动决定删除的顺序
会先删除EIP,然后删除EC2….
然后逐渐全部删除
5. YAML Crash Course(YAML速成班)
YAML files有很对 键 和 值,被称为键值对
- nested objects:嵌套对象,是由嵌套对象组成的
- supported arrays:支持的数组
6. CloudFormation Resources
What are resource?
mandatory:强制的
- Resource 是 CloudFormation 模板的核心
- 224种resources
- types indentifiers:
- AWS::aws-product-name::data-type-name
How do I find resources documentation?
进入EC2 instance的resource的官方文档中
找到其对应的YAML文件
FAQ for resources
- Can I create a dynamic amout of resources?
- 我可以创建一个动态的资源数量吗?
- 不能
7. CloudFormation Parameters
What are parameter?
When should you use a parameter?
- 如果某个资源在后期的设置会进行修改则需要使用parameter
-
Parameters Settings
How to Reference a Parameter(如何引用一个参数)
使用Fn::Ref 缩写是!Ref
Concept:Pseudo Parameters(伪参数)
8. CloudFormation Mappings
What are mappings?
mappings 是固定变量
- mappings 是非常易于去区分不同的环境,地区,AMI 等等
- 所有的值都是在模板中硬编码的
When would you use mappings vs parameters?
- Mappings are great when you know in advance all the values that can be taken and that they can be reduced from variables
- 当你事先知道所有可以取的值,并且这些值可以从变量中还原出来时,Mappings是非常好的。
Fn:: FindInMap Accessing Mapping Values
上图是使用方式
代码中红框框出来的,结合语法分析
- MapName对应RegionMap
- TopLevelKey对应一个伪参数“AWS::Region”默认的对应的值是us-east-2
- SecondLevelKey 对应 TopLevelKey里的第一个参数,这里对应的是32和里面的值
9. CloudFormation Outputs(popular exam questions)
What are outputs?
- The outputs section declares _option _ouputs values that we can import into other stacks
- 输出部分声明了(可选的)输出值,我们可以将其导入其他堆栈。
- 如果outputs堆栈被别人使用了,则不能删除
Output Example
Cross Stack Reference
在其他堆栈使用ImportValue 来使用别的堆栈中输出的结果
outputs和export是在考试中出现很多的
10. CloudFormation Conditions
What are conditions used for?
- Conditions are used to control the creation of resources or outputs based on a condition
- 条件用于控制基于条件的资源或输出的创建。
How to define a condition?
Using a condition
11. CloudFormation Intrinsic Functions
CloudFormation Must Know Intrisic Functions
Fn::Ref
- Fn::Ref 方法是用来调用引用
- Parameters 中会返回parameter的值
- Resources 中会返回物理ID
- 缩写是 !Ref
Fn::GetAtt
Fn:: FindInMap Accessing Mapping Values
Fn::ImportValue
Fn::Join
- join values with a delimiter:分隔符连接值
Function Fn::Sub
- !Sub 可以替代变量为一个文本
- 和可以将Fn::Sub 和Pseudo variable结合
- 字符串必须包含${VariableName}
Condition Functions
12. CloudFormation Rollbacks
如果disable rollback的话就可以去看哪里有问题
13. CloudFormation ChangeSets, Nested Stacks & StackSet
ChangeSets
- When you update a stack, you need to know what changes before it happens for greater confidence:
- 当你更新一个堆栈时,你需要在它发生之前就知道有什么变化,以获得更大的信心
Nested stacks
- They allow you to isolate repeated patterns / common components in separate stacks and call them from other stacks
- 它们允许你在单独的堆栈中隔离重复的模式/共同的组件,并从其他堆栈中调用它们。
CloudFormation - Cross vs Nested stacks
Cross Stacks
Helpful when stacks have different lifecycles:当堆栈有不同的生命周期时有帮助。
就是当一者结束了然后另一者才用它,叫不同的生命周期