こんにちは! JQです。
前回は『CloudFormation編~CloudFormationでVPCパート②~』 ということで、CloudFormationのStackでVPCを構築してみました。
今回は『CloudFormation編~CloudFormationでVPCパート③~』と題して、CloudFormationのStackでVPCにWEBインスタンスを構築してみたいと思います。
Templateの作成
4 インスタンス構築Templateの作成
インスタンス構築のTemplateを作成します。
下記テンプレートではhttpとphpをインストールしてphpinfoのファイルを設置するWEBサーバを起動します。
「Parameters」で指定する項目は以下になります。
InstanceType | インスタンスタイプ |
---|---|
WebKeyName | SSHキー |
SSHLocation | SSHの許可IP(デフォルトは0.0.0.0/0) |
VpcId | VPCのID |
SubnetId | SubnetのID |
「Mappings」はAmazonLinuxのAMIになります。
CloudFormationの「Metadata」を利用してパッケージのインストールと起動にphpinfoファイルの作成を行っております。
※「UserData」で上記のトリガーが行われます。
成功した場合には「Outputs」で次の項目が出力されます。
WebInstanceID | 作成されたインスタンスのInstanceID |
---|---|
AZ | 作成されたインスタンスのゾーン |
URL | 作成されたインスタンスにアクセスする為のURL |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
{ { "AWSTemplateFormatVersion" : "2010-09-09", "Description" : "AWS CloudFormation Template : Build to instance. AmazonLinux. ", "Parameters" : { "InstanceType" : { "Description" : "WebServer EC2 instance type", "Type" : "String", "Default" : "t1.micro", "AllowedValues" : [ "t1.micro","m1.small","m1.medium","m1.large","m1.xlarge","m2.xlarge","m2.2xlarge","m2.4xlarge","m3.xlarge","m3.2xlarge","c1.medium","c1.xlarge","cc1.4xlarge","cc2.8xlarge","cg1.4xlarge"], "ConstraintDescription" : "must be a valid EC2 instance type." }, "WebKeyName" : { "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances", "Type" : "String", "MinLength": "1", "MaxLength": "64", "AllowedPattern" : "[-_ a-zA-Z0-9]*", "ConstraintDescription" : "can contain only alphanumeric characters, spaces, dashes and underscores." }, "SSHLocation" : { "Description" : "The IP address range that can be used to SSH to the EC2 instances", "Type": "String", "MinLength": "9", "MaxLength": "18", "Default": "0.0.0.0/0", "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})", "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x." }, "VpcId" : { "Type" : "String", "Description" : "VpcId of your existing Virtual Private Cloud (VPC)" }, "SubnetId" : { "Type" : "String", "Description" : "SubnetId of an existing Public facing subnet in your Virtual Private Cloud (VPC)" } }, "Mappings" : { "AWSInstanceType2Arch" : { "t1.micro" : { "Arch" : "64" }, "m1.small" : { "Arch" : "64" }, "m1.medium" : { "Arch" : "64" }, "m1.large" : { "Arch" : "64" }, "m1.xlarge" : { "Arch" : "64" }, "m2.xlarge" : { "Arch" : "64" }, "m2.2xlarge" : { "Arch" : "64" }, "m2.4xlarge" : { "Arch" : "64" }, "m3.xlarge" : { "Arch" : "64" }, "m3.2xlarge" : { "Arch" : "64" }, "c1.medium" : { "Arch" : "64" }, "c1.xlarge" : { "Arch" : "64" }, "cc1.4xlarge" : { "Arch" : "64HVM" }, "cc2.8xlarge" : { "Arch" : "64HVM" }, "cg1.4xlarge" : { "Arch" : "64HVM" } }, "AWSRegionArch2AMI" : { "us-east-1" : { "64" : "ami-35792c5c" }, "us-west-1" : { "64" : "ami-687b4f2d" }, "us-west-2" : { "64" : "ami-d03ea1e0" }, "eu-west-1" : { "64" : "ami-149f7863" }, "sa-east-1" : { "64" : "ami-9f6ec982" }, "ap-southeast-1" : { "64" : "ami-14f2b946" }, "ap-southeast-2" : { "64" : "ami-a148d59b" }, "ap-northeast-1" : { "64" : "ami-3561fe34" } } }, "Resources" : { "WebSecurityGroup" : { "Type" : "AWS::EC2::SecurityGroup", "Properties" : { "VpcId" : { "Ref" : "VpcId" }, "GroupDescription" : "Enable SSH access via port 22", "SecurityGroupIngress" : [ { "IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : "219.111.8.217/32" }, { "IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0" } ] } }, "WebHost" : { "Type" : "AWS::EC2::Instance", "Metadata": { "AWS::CloudFormation::Init": { "config": { "packages": { "yum": { "httpd": [], "php" : [] } }, "files" : { "/var/www/html/index.php" : { "content" : { "Fn::Join" : ["", [ "<?php phpinfo(); ?>" ]]}, "mode" : "000644", "owner" : "root", "group" : "root" } }, "services" : { "sysvinit" : { "httpd" : { "enabled" : "true", "ensureRunning" : "true" } } } } } }, "Properties" : { "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },{ "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" },"Arch" ] } ] }, "KeyName" : { "Ref" : "WebKeyName" }, "NetworkInterfaces" : [{ "AssociatePublicIpAddress" : "true", "GroupSet" : [{ "Ref" : "WebSecurityGroup" }], "SubnetId" : { "Ref" : "SubnetId" }, "DeviceIndex" : "0" }], "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [ "#!/bin/bash\n", "yum update -y aws-cfn-bootstrap \n", "# Helper function\n", "function error_exit\n", "{\n", " /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '", { "Ref" : "WebHostHandle" }, "'\n", " exit 1\n", "}\n", "# Install the simple web page\n", "/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackId" }, " -r WebHost ", " --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n", "# All done so signal success\n", "/opt/aws/bin/cfn-signal -e 0 -r \"WebHost setup complete\" '", { "Ref" : "WebHostHandle" }, "'\n" ]]}}, "Tags": [ { "Key" : "Name", "Value": "WebHost" } ] } }, "WebHostHandle" : { "Type" : "AWS::CloudFormation::WaitConditionHandle" }, "WebControllerCondition" : { "Type" : "AWS::CloudFormation::WaitCondition", "DependsOn" : "WebHost", "Properties" : { "Handle" : { "Ref" : "WebHostHandle" }, "Timeout" : "600" } } }, "Outputs" : { "WebInstanceID" : { "Description" : "Web Instance ID", "Value" : {"Ref": "WebHost"} }, "AZ" : { "Description" : "Availability Zone of the newly created EC2 instance", "Value" : { "Fn::GetAtt" : [ "WebHost", "AvailabilityZone" ] } }, "URL" : { "Description" : "URL of the sample website", "Value" : { "Fn::Join" : [ "", ["http://", { "Fn::GetAtt" : ["WebHost", "PublicIp"] }]]} } } } |
いかがでしたでしょうか?
次回は『CloudFormation編~CloudFormationでVPCパート④~』ということで、実際に今回のテンプレートを試してみたいと思います。
お楽しみに!
——————————————————————————————————
ナレコムクラウドのFacebookに『いいね!』をクリックして頂くと
最新のお役立ちレシピが配信されます★
┏━━━━━━━━━━━━━┓
┃ナレコムクラウド Facebook┃
┗━━━━━━━━━━━━━┛
——————————————————————————————————