1. 前言
本文主要记录了使用CloudFormation设计器创建模板、使用模板创建堆栈和堆栈集的过程。CloudFormation是在AWS上实践基础设施即代码的重要服务之一,其功能类似OpenStack中的Heat服务,使用该服务,我们能够使用模板定义创建、配置云服务资源的操作,利用模板进行资源的创建能够减少重复的劳动,提高效率,比如:
- 使用一份CloudFormation模板为开发、测试、类生产、生产环境分别创建一致的基础设施
- 使用一份CloudFormation模板在新的Region为拓展的业务创建与已有Region一致的基础设施
AWS关于CloudFormation的介绍如下:
AWS CloudFormation 是一种让开发人员和企业都能轻松创建相关 AWS 资源集合,并按顺序和可预测的方式对其进行预配置的服务。
AWS提供的与基础设施自动化、自动化部署相关的服务除了CloudFormation外,还有OpsWorks、Elastic Beanstalk、CodeDeploy等:
服务名称 | 功能描述 | 为了方便理解而进行的不太恰当的功能类比 |
---|---|---|
CloudFormation | AWS CloudFormation 是一种让开发人员和企业都能轻松创建相关 AWS 资源集合,并按顺序和可预测的方式对其进行预配置的服务。 | OpenStack Heat |
OpsWorks | AWS OpsWorks for Chef Automate 可以提供一个完全托管的 Chef 服务器和全套自动化工具,让您可以通过工作流自动化来实现连续部署以及合规性与安全性的自动测试,并能通过一个用户界面查看各种节点及其状态。Chef 服务器可以处理软件与操作系统配置、软件包安装和数据库设置等多种操作任务,让您实现全栈自动化。Chef 服务器可以集中存储您的配置任务,并将其提供给任意规模的计算环境中的每个节点,节点数量既可以是几个也可以是数千个。OpsWorks for Chef Automate 与 Chef 社区中提供的各种工具和说明书完全兼容,并会将新的节点自动注册到您的 Chef 服务器。 | 结合CMDB和统一部署工具 |
Elastic Beanstalk | AWS Elastic Beanstalk 让开发人员能够更加轻松地在 AWS 云中快速部署和管理应用程序。开发人员只需上传应用程序,Elastic Beanstalk 将自动处理容量预配置、负载均衡、Auto Scaling 和应用程序运行状况监控的部署细节。 | OpenStack Murano,看描述感觉功能很强大,但可能是以加大上传的应用的开发难度为代价的 |
CodeDeploy | AWS CodeDeploy 是一项可以将代码自动部署到任意实例中的服务,这些实例包括 Amazon EC2 实例和在本地运行的实例。AWS CodeDeploy 让您能够轻松快速地推出新功能,可帮助您避免在部署过程中停机,并解决您应用程序更新时的复杂性问题。您可以使用 AWS CodeDeploy 自动执行部署,这样不必进行容易出错的手动操作,该服务可以随您的基础设施进行扩展,因此,您可以轻松地部署一个或上千个实例。 | Jenkins、Pipeline |
2. 使用过程记录

2.1 使用设计器创建模板

构造一个简单的Web应用栈:

保存并导出:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Metadata": {
"AWS::CloudFormation::Designer": {
"0424b5f8-3912-41f0-80bf-8364f292c448": {
"size": {
"width": 190,
"height": 140
},
"position": {
"x": 60,
"y": 210
},
"z": 0,
"embeds": [
"57547db9-a9c4-46a1-81df-e4dde281a0f0"
]
},
"57547db9-a9c4-46a1-81df-e4dde281a0f0": {
"size": {
"width": 60,
"height": 60
},
"position": {
"x": 100,
"y": 260
},
"z": 1,
"parent": "0424b5f8-3912-41f0-80bf-8364f292c448",
"dependson": [
"e0171dc3-5d5c-40c5-884b-075abffa1e72",
"0424b5f8-3912-41f0-80bf-8364f292c448"
]
},
"6ac923a1-a7c5-42ff-8ebc-ad2c4941e681": {
"size": {
"width": 60,
"height": 60
},
"position": {
"x": 100,
"y": 70
},
"z": 0,
"isassociatedwith": [
"57547db9-a9c4-46a1-81df-e4dde281a0f0"
]
},
"e0171dc3-5d5c-40c5-884b-075abffa1e72": {
"size": {
"width": 60,
"height": 60
},
"position": {
"x": 310,
"y": 240
},
"z": 0
},
"43d45fa7-f43d-4fe2-a7f2-eab1bc03a302": {
"size": {
"width": 60,
"height": 60
},
"position": {
"x": -190,
"y": 70
},
"z": 0,
"dependson": [
"c9c16990-1a3f-4c67-9bb1-da1ff54e50ee"
]
},
"c9c16990-1a3f-4c67-9bb1-da1ff54e50ee": {
"size": {
"width": 60,
"height": 60
},
"position": {
"x": -40,
"y": 70
},
"z": 0,
"dependson": [
"6ac923a1-a7c5-42ff-8ebc-ad2c4941e681"
]
},
"da946692-7f81-46d9-b0c8-ce5fe80074de": {
"source": {
"id": "6ac923a1-a7c5-42ff-8ebc-ad2c4941e681"
},
"target": {
"id": "57547db9-a9c4-46a1-81df-e4dde281a0f0"
},
"z": 9
}
}
},
"Resources": {
"EC2VPC12APX": {
"Type": "AWS::EC2::VPC",
"Properties": {},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "0424b5f8-3912-41f0-80bf-8364f292c448"
}
}
},
"EC2I2HFKL": {
"Type": "AWS::EC2::Instance",
"Properties": {},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "57547db9-a9c4-46a1-81df-e4dde281a0f0"
}
},
"DependsOn": [
"RDSDBI23JH3",
"EC2VPC12APX"
]
},
"EC2EIP5ABTI": {
"Type": "AWS::EC2::EIP",
"Properties": {
"InstanceId": {
"Ref": "EC2I2HFKL"
}
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "6ac923a1-a7c5-42ff-8ebc-ad2c4941e681"
}
}
},
"RDSDBI23JH3": {
"Type": "AWS::RDS::DBInstance",
"Properties": {},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "e0171dc3-5d5c-40c5-884b-075abffa1e72"
}
}
},
"R53RRE32JFJ": {
"Type": "AWS::Route53Resolver::ResolverEndpoint",
"Properties": {},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "43d45fa7-f43d-4fe2-a7f2-eab1bc03a302"
}
},
"DependsOn": [
"R53RRR4FTA1"
]
},
"R53RRR4FTA1": {
"Type": "AWS::Route53Resolver::ResolverRule",
"Properties": {},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "c9c16990-1a3f-4c67-9bb1-da1ff54e50ee"
}
},
"DependsOn": [
"EC2EIP5ABTI"
]
}
}
}

2.2 使用模板创建堆栈
使用提前创建好的功能是创建一个安全组的模板创建堆栈
模板代码
{
"AWSTemplateFormatVersion": "2010-09-09",
"Metadata": {
"AWS::CloudFormation::Designer": {
"96a17388-3a69-4091-8cce-3ee31e479d13": {
"size": {
"width": 60,
"height": 60
},
"position": {
"x": 180,
"y": 120
},
"z": 0,
"embeds": []
}
}
},
"Resources": {
"EC2SG4OEEM": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"VpcId": "vpc-4bdad223",
"GroupName": "CreatedByCloudFormation",
"GroupDescription": "CreatedByCloudFormation"
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "96a17388-3a69-4091-8cce-3ee31e479d13"
}
}
}
}
}
创建堆栈


在配置堆栈选项的过程中,可以看到CloudFormation能够结合标签服务、IAM、CloudWatch、SNS服务一起使用。






开始创建


问题解决
因为创建模板时没有填写一些必要的参数,提交时系统也没有校验,因此模板虽然能够创建成功,但是使用模板创建堆栈却会失败。报错信息提示缺少GroupDescription属性,需要更新模板,补充必须的参数,参考官方指南进行。创建安全组,至少需要指定其名称和所属的VPC,更新模板后使用新的模板再次创建堆栈:

验证资源

堆栈前安全组列表

删除堆栈


状态机分析


2.3 创建堆栈集
利用堆栈集,您可使用一个 AWS CloudFormation 模板在 AWS 账户中跨区域创建堆栈。每个堆栈中包含的所有资源由堆栈集的 AWS CloudFormation 模板定义。在创建堆栈集时,您可指定要使用的模板以及模板所需的任何参数和功能。
创建堆栈集




账号需要填入账户ID


开始创建

网友评论