04-16学习记录
Cloudformation用来管理AWS的各种资源,通过模板设置可以自动地创建对应的资源。
WebServer:
Type: AWS::EC2::Instance
Properties:
KeyName:
Ref: KeyName
...
这里建立了一个EC2实例,Ref用来返回属性的值,可作为引用资源的标记。
例如下面:
Resources:
Ec2Instance:
Type: 'AWS::EC2::Instance'
Properties:
SecurityGroups:
- !Ref InstanceSecurityGroup #Ref的另一种简短写法
- MyExistingSecurityGroup
KeyName: mykey #如果没有被定义过则会创建失败
ImageId: ami-7a11e213
InstanceSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: Enable SSH access via port 22
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
如果对应多个属性的话可以用Fn::GetAtt,对应的是资源的逻辑名和要检索的属性名。
同时还可以自定义输入的值,下面是String的参数限制
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access into the WordPress web server
Type: AWS::EC2::KeyPair::KeyName
WordPressUser:
Default: admin
NoEcho: true #对应敏感的参数进行隐藏,不会别人看到
Description: The WordPress database admin account user name
Type: String
MinLength: 1
MaxLength: 16
AllowedPattern: "[a-zA-Z][a-zA-Z0-9]*"
对于 Number 类型,可以有对应的限制:MinValue、MaxValue、Default 和 AllowedValues。
使用Map映射(将AWS对应的region映射到AMI上面):
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
Type: String
Mappings:
RegionMap:
us-east-1:
AMI: ami-76f0061f
us-west-1:
AMI: ami-655a0a20
eu-west-1:
AMI: ami-7fd4e10b
ap-southeast-1:
AMI: ami-72621c20
ap-northeast-1:
AMI: ami-8e08a38f
Resources:
Ec2Instance:
Type: 'AWS::EC2::Instance'
Properties:
KeyName: !Ref KeyName
ImageId: !FindInMap
- RegionMap
- !Ref 'AWS::Region'
- AMI
UserData: !Base64 '80'
通过Fn::Join来拼接所需要的值:
Outputs:
InstallURL:
Value: !Join
- ''
- - 'http://'
- !GetAtt
- ElasticLoadBalancer
- DNSName
- /wp-admin/install.php
Description: Installation URL of the WordPress website
WebsiteURL:
Value: !Join
- ''
- - 'http://'
- !GetAtt
- ElasticLoadBalancer
- DNSName
可以从AMI(Amazon Machine Image)中获取需要的应用程序,配置需要的环境。
最佳实践:
使用 Fn::ImportValue 将堆栈进行合理的功能划分
通过IAM来管理访问限制
使用嵌套堆栈AWS::CloudFormation::Stack
CodePipeline大致是用来做版本控制和测试集成的