CloudFormationを使ってみた

この記事は公開されてから半年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは!

これまでブログに書かせてもらった内容をCloudFormationを使って、自動で作成してくれるように作ってみました。
図の構成が、自動でできあがります。

cloudformation

以下をテキスト保存して、CloudFormationでお試しください!

テンプレート起動リンク

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "VPC": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": "10.0.0.0/16",
        "InstanceTenancy": "default",
        "EnableDnsSupport": "true",
        "EnableDnsHostnames": "true"
      }
    },
    "subnet01": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "CidrBlock": "10.0.1.0/24",
        "AvailabilityZone": "ap-southeast-1a",
        "VpcId": {
          "Ref": "VPC"
        }
      }
    },
    "subnet02": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "CidrBlock": "10.0.2.0/24",
        "AvailabilityZone": "ap-southeast-1b",
        "VpcId": {
          "Ref": "VPC"
        }
      }
    },
    "inetgw": {
      "Type": "AWS::EC2::InternetGateway",
      "Properties": {
      }
    },
    "dhcpopt": {
      "Type": "AWS::EC2::DHCPOptions",
      "Properties": {
        "DomainName": "ap-southeast-1.compute.internal",
        "DomainNameServers": [
          "AmazonProvidedDNS"
        ]
      }
    },
    "nwacl": {
      "Type": "AWS::EC2::NetworkAcl",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        }
      }
    },
    "routetable": {
      "Type": "AWS::EC2::RouteTable",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        }
      }
    },
    "elbaslb": {
      "Type": "AWS::ElasticLoadBalancing::LoadBalancer",
      "Properties": {
        "Subnets": [
          {
            "Ref": "subnet01"
          },
          {
            "Ref": "subnet02"
          }
        ],
        "HealthCheck": {
          "HealthyThreshold": "2",
          "Interval": "30",
          "Target": "HTTP:80/index.html",
          "Timeout": "5",
          "UnhealthyThreshold": "2"
        },
        "SecurityGroups": [
          {
            "Ref": "sgall"
          }
        ],
        "Listeners": [
          {
            "InstancePort": "80",
            "LoadBalancerPort": "80",
            "Protocol": "HTTP",
            "InstanceProtocol": "HTTP"
          }
        ]
      }
    },
    "asgassg": {
      "Type": "AWS::AutoScaling::AutoScalingGroup",
      "Properties": {
        "AvailabilityZones": [
          "ap-southeast-1b",
          "ap-southeast-1a"
        ],
        "Cooldown": "300",
        "DesiredCapacity": "1",
        "MaxSize": "4",
        "MinSize": "2",
        "HealthCheckGracePeriod": "300",
        "HealthCheckType": "ELB",
        "VPCZoneIdentifier": [
          {
            "Ref": "subnet01"
          },
          {
            "Ref": "subnet02"
          }
        ],
        "LaunchConfigurationName": {
          "Ref": "lcaslc"
        },
        "LoadBalancerNames": [
          {
            "Ref": "elbaslb"
          }
        ]
      }
    },
    "rdsmaster": {
      "Type": "AWS::RDS::DBInstance",
      "Properties": {
        "AutoMinorVersionUpgrade": "true",
        "DBInstanceClass": "db.t1.micro",
        "DBInstanceIdentifier": "master",
        "Port": "3306",
        "AllocatedStorage": "5",
        "BackupRetentionPeriod": "1",
        "DBName": "MyDatabase",
        "Engine": "mysql",
        "EngineVersion": "5.5.33",
        "LicenseModel": "general-public-license",
        "MasterUsername": "rdsuser",
        "MasterUserPassword": "MyPassword",
        "PreferredBackupWindow": "16:35-17:05",
        "PreferredMaintenanceWindow": "mon:14:02-mon:14:32",
        "MultiAZ": "true",
        "VPCSecurityGroups": [
          {
            "Ref": "sgall"
          }
        ],
        "DBSubnetGroupName": {
          "Ref": "dbsubnetmultiazsg"
        },
        "Tags": [
          {
            "Key": "workload-type",
            "Value": "production"
          }
        ]
      }
    },
    "rdsslave0": {
      "Type": "AWS::RDS::DBInstance",
      "Properties": {
        "AutoMinorVersionUpgrade": "true",
        "DBInstanceClass": "db.t1.micro",
        "DBInstanceIdentifier": "slave0",
        "Port": "3306",
        "SourceDBInstanceIdentifier": {
          "Ref": "rdsmaster"
        }
      }
    },
    "lcaslc": {
      "Type": "AWS::AutoScaling::LaunchConfiguration",
      "Properties": {
        "ImageId": "ami-56bee804",
        "InstanceType": "t1.micro",
        "KeyName": "astest",
        "SecurityGroups": [
          {
            "Ref": "sgall"
          }
        ],         "BlockDeviceMappings": [
          {
            "DeviceName": "/dev/sda1",
            "Ebs": {
              "VolumeSize": 8
            }
          }
        ]
      }
    },
    "dbsubnetmultiazsg": {
      "Type": "AWS::RDS::DBSubnetGroup",
      "Properties": {
        "DBSubnetGroupDescription": "for Multi-AZ",
        "SubnetIds": [
          {
            "Ref": "subnet01"
          },
          {
            "Ref": "subnet02"
          }
        ]
      }
    },
    "sgall": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "allow all",
        "VpcId": {
          "Ref": "VPC"
        },
        "SecurityGroupIngress": [
          {
            "IpProtocol": "-1",
            "CidrIp": "0.0.0.0/0"
          }
        ],
        "SecurityGroupEgress": [
          {
            "IpProtocol": "-1",
            "CidrIp": "0.0.0.0/0"
          }
        ]
      }
    },
    "scalingDecreaseGroupSize": {
      "Type": "AWS::AutoScaling::ScalingPolicy",
      "Properties": {
        "AdjustmentType": "ChangeInCapacity",
        "ScalingAdjustment": "-1",
        "AutoScalingGroupName": {
          "Ref": "asgassg"
        }
      }
    },
    "scalingIncreaseGroupSize": {
      "Type": "AWS::AutoScaling::ScalingPolicy",
      "Properties": {
        "AdjustmentType": "ChangeInCapacity",
        "ScalingAdjustment": "1",
        "AutoScalingGroupName": {
          "Ref": "asgassg"
        }
      }
    },
    "alarmawsec2assgCPUUtilizationadd": {
      "Type": "AWS::CloudWatch::Alarm",
      "Properties": {
        "ActionsEnabled": "true",
        "ComparisonOperator": "GreaterThanOrEqualToThreshold",
        "EvaluationPeriods": "1",
        "MetricName": "CPUUtilization",
        "Namespace": "AWS/EC2",
        "Period": "300",
        "Statistic": "Average",
        "Threshold": "50.0",
        "AlarmActions": [
          {
            "Ref": "scalingIncreaseGroupSize"
          }
        ],
        "Dimensions": [
          {
            "Name": "AutoScalingGroupName",
            "Value": "as-sg"
          }
        ]
      }
    },
    "alarmawsec2assgHighCPUUtilizationremove": {
      "Type": "AWS::CloudWatch::Alarm",
      "Properties": {
        "ActionsEnabled": "true",
        "ComparisonOperator": "LessThanThreshold",
        "EvaluationPeriods": "1",
        "MetricName": "CPUUtilization",
        "Namespace": "AWS/EC2",
        "Period": "300",
        "Statistic": "Average",
        "Threshold": "50.0",
        "AlarmActions": [
          {
            "Ref": "scalingDecreaseGroupSize"
          }
        ],
        "Dimensions": [
          {
            "Name": "AutoScalingGroupName",
            "Value": "as-sg"
          }
        ]
      }
    },
    "acl1": {
      "Type": "AWS::EC2::NetworkAclEntry",
      "Properties": {
        "CidrBlock": "0.0.0.0/0",
        "Egress": true,
        "Protocol": "-1",
        "RuleAction": "allow",
        "RuleNumber": "100",
        "NetworkAclId": {
          "Ref": "nwacl"
        }
      }
    },
    "acl2": {
      "Type": "AWS::EC2::NetworkAclEntry",
      "Properties": {
        "CidrBlock": "0.0.0.0/0",
        "Protocol": "-1",
        "RuleAction": "allow",
        "RuleNumber": "100",
        "NetworkAclId": {
          "Ref": "nwacl"
        }
      }
    },
    "subnetacl1": {
      "Type": "AWS::EC2::SubnetNetworkAclAssociation",
      "Properties": {
        "NetworkAclId": {
          "Ref": "nwacl"
        },
        "SubnetId": {
          "Ref": "subnet01"
        }
      }
    },
    "subnetacl2": {
      "Type": "AWS::EC2::SubnetNetworkAclAssociation",
      "Properties": {
        "NetworkAclId": {
          "Ref": "nwacl"
        },
        "SubnetId": {
          "Ref": "subnet02"
        }
      }
    },
    "gw1": {
      "Type": "AWS::EC2::VPCGatewayAttachment",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "InternetGatewayId": {
          "Ref": "inetgw"
        }
      }
    },
    "subnetroute1": {
      "Type": "AWS::EC2::SubnetRouteTableAssociation",
      "Properties": {
        "RouteTableId": {
          "Ref": "routetable"
        },
        "SubnetId": {
          "Ref": "subnet02"
        }
      }
    },
    "subnetroute2": {
      "Type": "AWS::EC2::SubnetRouteTableAssociation",
      "Properties": {
        "RouteTableId": {
          "Ref": "routetable"
        },
        "SubnetId": {
          "Ref": "subnet01"
        }
      }
    },
    "route1": {
      "Type": "AWS::EC2::Route",
      "Properties": {
        "DestinationCidrBlock": "0.0.0.0/0",
        "RouteTableId": {
          "Ref": "routetable"
        },
        "GatewayId": {
          "Ref": "inetgw"
        }
      },
      "DependsOn": "gw1"
    },
    "dchpassoc1": {
      "Type": "AWS::EC2::VPCDHCPOptionsAssociation",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "DhcpOptionsId": {
          "Ref": "dhcpopt"
        }
      }
    }
  },
  "Description": ""
}

コメントを残す

メールアドレスが公開されることはありません。

Time limit is exhausted. Please reload CAPTCHA.