AWS CloudFormation练习
AWS CloudFormation练习

AWS CloudFormation练习

内容纲要

拓展2 AWS CloudFormation练习

使用YAML模板,创建一个VPC和一个公共子网,并创建一个路由表关联。再创建对应的EC2实例资源,要求是能够连接公共网络。

---
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyVPC:
    Type: 'AWS::EC2::VPC'
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true

  MyInternetGateway:
    Type: 'AWS::EC2::InternetGateway'

  MyVPCGatewayAttachment:
    Type: 'AWS::EC2::VPCGatewayAttachment'
    Properties:
      VpcId: !Ref MyVPC
      InternetGatewayId: !Ref MyInternetGateway

  MyRouteTable:
    Type: 'AWS::EC2::RouteTable'
    Properties:
      VpcId: !Ref MyVPC

  MyPublicSubnet:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: 10.0.0.0/24
      MapPublicIpOnLaunch: true

  MySubnetRouteTableAssociation:
    Type: 'AWS::EC2::SubnetRouteTableAssociation'
    Properties:
      SubnetId: !Ref MyPublicSubnet
      RouteTableId: !Ref MyRouteTable

  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: ami-0ff8a91507f77f867
      InstanceType: t2.micro
      NetworkInterfaces:
      - AssociatePublicIpAddress: true
        DeviceIndex: 0
        SubnetId: !Ref MyPublicSubnet
      KeyName: my-key-pair

Outputs:
  PublicIP:
    Value: !GetAtt MyEC2Instance.PublicIp
    Description: The public IP of the EC2 instance

说明:

  • ImageId 是镜像 ID,你可以替换成自己的镜像ID
  • KeyName 是密钥对名称
  • CidrBlock 是 VPC 和子网的网络范围
  • MapPublicIpOnLaunch 在子网上自动分配公共 IP不需要则为false
  • NetworkInterfaces 中 AssociatePublicIpAddress 设置为 true,表示连接公共网络
  • MyRouteTable 创建路由表
  • MySubnetRouteTableAssociation 为公共子网创建路由表关联。

使用上述模板之前,你需要确保已经有一个合法的镜像 ID 和密钥对。

需要注意的是,默认路由表中是没有默认路由的,需要额外的创建默认路由指向 Internet Gateway. 可以在路由表的Properties中添加:

  MyRouteTable:
    Type: 'AWS::EC2::RouteTable'
    Properties:
      VpcId: !Ref MyVPC
      Routes:
        - DestinationCidrBlock: 0.0.0.0/0
          GatewayId: !Ref MyInternetGateway

添加上述配置后,路由表将会有一条默认路由指向 Internet Gateway, 这样 EC2 实例就可以访问公共网络了。

但是根据上述,如果你不想在 MyPublicSubnet 上自动分配公共 IP,而是给 EC2 实例分配公共 IP,可以在 EC2 实例的 NetworkInterfaces 属性中添加 AssociatePublicIpAddress: true,并在 SubnetId 中引用 MyPublicSubnet。

具体配置如下:

  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: ami-0ff8a91507f77f867
      InstanceType: t2.micro
      NetworkInterfaces:
      - AssociatePublicIpAddress: true
        DeviceIndex: 0
        SubnetId: !Ref MyPublicSubnet
      KeyName: my-key-pair

这样 EC2 实例就会自动分配一个公共 IP,并且连接到 MyPublicSubnet 中,可以访问公共网络。

请注意,如果你想让实例拥有公共 IP ,你需要同时确保你的 VPC 中有 Internet Gateway 并且你的 VPC route table 中有默认路由指向 Internet Gateway, 这样才能保证实例可以访问到公共网络。

在 CloudFormation 模板中,Outputs 是一种可选的部分,用于将某些信息从模板的执行结果中暴露出来。这些信息可以在模板执行后通过 AWS CloudFormation 控制台、AWS CLI 或 SDK 访问。

Outputs 中的具体内容是一个键值对,如下面这个例子:

Outputs:
  PublicIP:
    Value: !GetAtt MyEC2Instance.PublicIp
    Description: The public IP of the EC2 instance
  • PublicIP 是输出的名称,你可以自定义。
  • Value 是输出的值,这里是通过 !GetAtt 获取 EC2 实例的公共 IP。
  • Description 是输出的描述信息,这里是 EC2 实例的公共 IP。

通过输出可以更方便的获取部署后的信息, 比如EC2的公共IP, 数据库的连接地址等信息。

在 CloudFormation 模板中,Outputs 是一种可选的部分,如果不需要暴露出来任何信息,你可以不写 Outputs 部分。

如果你不需要在模板执行后获取任何信息,或者可以通过其它方式获取这些信息,可以不写 Outputs 部分。

另外,如果你想在部署后获取更多的信息,可以在 Outputs 中添加更多的键值对来获取。

总之,是根据具体的需求来决定是否需要在模板中添加 Outputs 部分。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注