CloudFormation学习笔记01

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大致是用来做版本控制和测试集成的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值