AWS CloudFormation入门


最重要的东西放最前面,多看官方文档:What is AWS CloudFormation? - AWS CloudFormation

CloudFormation是什么?

CloudFormation是在AWS上实践基础设施即代码 (Infrastructure as Code, 简称IaC) 的重要服务之一。

使用该服务,我们能够使用模板定义创建、配置云服务资源的操作,利用模板进行资源的创建能够减少重复的劳动,提高效率,比如:

  • 使用一份CloudFormation模板为开发、测试、类生产、生产环境分别创建一致的基础设施
  • 使用一份CloudFormation模板在新的Region为拓展的业务创建与已有Region一致的基础设施

官方描述:

AWS CloudFormation is a service that helps you model and set up your AWS resources so that you can spend less time managing those resources and more time focusing on your applications that run in AWS. You create a template that describes all the AWS resources that you want (like Amazon EC2 instances or Amazon RDS DB instances), and CloudFormation takes care of provisioning and configuring those resources for you. You don’t need to individually create and configure AWS resources and figure out what’s dependent on what; CloudFormation handles that.

IaC - Infrastructure as Code

IaC 可以看成是一种拓扑图(部分组件之间有依赖关系,例如创建InternetGateway依赖于先创建的VPC),用一个可执行文档来描述各个组件(例如:VPC,Subnet,Gateway,RouteTable, Route等) 。

AWS提供的IaC服务是AWS CloudFormation,类似地,微软Azure云提供了Azure Resource Manager, 以及谷歌云提供了Cloud Deployment Manager。其他IaC工具还有, Chef, Puppet, SaltStack 以及 Hashicorp Terraform等。

IaC 的好处

  1. 可以轻松解决部署应用程序所需要的基础网络架构和组件(例如,部署LAMP应用程序,需要webservers, 负载均衡,数据库实例,网络安全组,传统的方式是逐个创建,耗时且容易出错,使用 IaC 可以通过模板脚本一键创建所需资源)
  2. IaC 简化了重复性和频繁任务的基础架构管理
  3. 即使对于不那么频繁的任务,也可以用模板格式节省时间和精力
  4. 一个公司通常会有一个模板库,涵盖最常见的应用场景。碰到新需求时,可以重用或修改现有脚本即可。

基本术语和缩略词

术语 含义
VPC Virtual Private Cloud
IaC Infrastructure as Code
IGW InternetGateway
stack 它是CloudFormation的一个最基本的执行单位。包括 创建的资源 + 资源的状态。有点类似于docker image.
template 它是对AWS资源的一个声明,这些资源构成了一个stack. 类似于Dockerfile.

模板编写

常见创建资源的类型:

  • AWS::EC2::VPC
  • AWS::EC2::InternetGateway
  • AWS::EC2::NatGateway
  • AWS::EC2::Subnet
  • AWS::EC2::RouteTable
  • AWS::EC2::Route
  • AWS::EC2::SubnetRouteTableAssociation
  • AWS::EC2::EIP

创建VPC

Resources:
MyVPC: # name of the resource and can be referred to in this script
Type: AWS::EC2::VPC
DeletionPolicy: Delete # 这个选项:当删除了这个stack后,会自动删除创建的VPC资源
Properties: { CidrBlock: "192.168.0.0/16"}

创建EC2

# EC2 instance
Resources:
MyInstance:
Type: AWS::EC2::Instance
DeletionPolicy: Delete
Properties:
AvailabilityZone: us-east-1a
ImageId: ami-02e136e904f3da870
InstanceType: t2.micro

创建Security Group

Resources:
MyFirstSG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow HTTP and SSH
# VpcId: 如果没有指定VpcId的话,那么使用的是默认的VPC
# Fn::ImportValue: !Ref VPC
SecurityGroupIngress:
# allow HTTP from public space
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
# allow SSH from public space
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: DemoSG

使用Parameter

# Parameters section
Parameters:
CidrBlock:
Type: String
Default: 10.10.0.0/16
Description: VPC IP address space
AllowedPattern: "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}/[0-9]{1,2}"
# Resources section
Resources:
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref CidrBlock
EnableDnsSupport: 'true'
EnableDnsHostnames: 'true'
Tags:
- Key: Name
Value: MyFirstVPC

使用Output

可以将一个Stack定义的资源,作为输出,然后在另外一个stack中使用。

# script 1
# MyFirstSG 定义同上
# Outputs section
Outputs:
MySecurityGroup:
Description: The security group ID
Value: !Ref MyFirstSG # 这里的MyFristSG就是前面定义的资源名字
Export:
Name: MyFirstSG # 这个导出的名字,也可以通过参数来指定. 使用格式:!Ref ParamName

使用Condition

TODO.

Nested Stack

TODO.

S3 Store

可以通过上传一个yaml文件,也可以给定一个S3的文件链接来执行template去创建资源。

非常好的参考

  1. https://github.com/kennyk65/aws-vpc-cloud-formation/blob/master/base-vpc-example.template.yml
    这个示例中,它创建了基本的VPC,4个子网(两个公有,两个私有),还有NAT网关。VPC 和 子网可以导出给其他stack使用。
  2. AWS Cloudformation - Cross Stack 学习总结 - 代码天地

文章作者: IT神助攻
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 IT神助攻 !
  目录