これは、CloudFormation の完璧な使用例のように思えます。デモ用のテンプレートを作成しました。使用するには、実行可能ファイルを S3 バケットに配置し、次のテンプレートを使用して新しい CloudFormation スタックを作成します。S3 から実行可能ファイルをダウンロードして実行します。注: テンプレートは、 CloudFormationScriptsが組み込まれた特別なAMIを使用します。
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Spot Autoscaling for installing and running Windows Services.",
"Parameters" : {
"InstanceType" : {
"Description" : "WebServer EC2 instance type",
"Type" : "String",
"Default" : "m1.small",
"AllowedValues" : ["t1.micro", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "c1.medium", "c1.xlarge", "cc1.4xlarge", "cc2.8xlarge", "cg1.4xlarge"],
"ConstraintDescription" : "must be a valid EC2 instance type."
},
"KeyName" : {
"Description" : "The EC2 Key Pair to get Admin password to Instance.",
"Type" : "String"
},
"DeployS3Bucket" : {
"Description" : "The S3 Bucket where deploy files are stored",
"Type" : "String"
},
"DeployS3Key" : {
"Description" : "The exe file that runs on startup",
"Type" : "String"
}
},
"Mappings" : {
"RegionToAMIMap" : {
"us-east-1" : {
"AMI" : "ami-60b90609"
},
"us-west-1" : {
"AMI" : "ami-5bd6f11e"
},
"eu-west-1" : {
"AMI" : "ami-07151573"
},
"ap-southeast-1" : {
"AMI" : "ami-6ab5f538"
},
"ap-northeast-1" : {
"AMI" : "ami-424ff043"
}
}
},
"Resources" : {
"IAMUser" : {
"Type" : "AWS::IAM::User",
"Properties" : {
"Path" : "/",
"Policies" : [{
"PolicyName" : "root",
"PolicyDocument" : {
"Statement" : [{
"Effect" : "Allow",
"Action" : "*",
"Resource" : "*"
}]
}
}]
}
},
"IAMUserAccessKey" : {
"Type" : "AWS::IAM::AccessKey",
"Properties" : {
"UserName" : {
"Ref" : "IAMUser"
}
}
},
"SecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable RDP",
"SecurityGroupIngress" : [{
"IpProtocol" : "tcp",
"FromPort" : "3389",
"ToPort" : "3389",
"CidrIp" : "0.0.0.0/0"
}]
}
},
"RunExecutable" : {
"Type" : "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"files" : {
"c:\\ToRun\\executable.exe" : {
"source" : {
"Fn::Join" : ["/", ["http://s3.amazonaws.com", {
"Ref" : "DeployS3Bucket"
}, {
"Ref" : "DeployS3Key"
}]]
},
"authentication" : "S3AccessCreds"
}
},
"commands" : {
"1-run-executable" : {
"command" : "c:\\ToRun\\executable.exe"
}
}
}
},
"AWS::CloudFormation::Authentication" : {
"S3AccessCreds" : {
"type" : "S3",
"accessKeyId" : {
"Ref" : "IAMUserAccessKey"
},
"secretKey" : {
"Fn::GetAtt" : ["IAMUserAccessKey", "SecretAccessKey"]
},
"buckets" : [{
"Ref" : "DeployS3Bucket"
}]
}
}
},
"Properties" : {
"KeyName" : {
"Ref" : "KeyName"
},
"ImageId" : {
"Fn::FindInMap" : ["RegionToAMIMap", {
"Ref" : "AWS::Region"
}, "AMI"]
},
"SecurityGroups" : [{
"Ref" : "SecurityGroup"
}],
"InstanceType" : {
"Ref" : "InstanceType"
},
"UserData" : {
"Fn::Base64" : {
"Fn::Join" : ["", ["<script>\n", "cfn-init.exe -v -s ", {
"Ref" : "AWS::StackName"
}, " -r RunExecutable ", " --access-key ", {
"Ref" : "IAMUserAccessKey"
}, " --secret-key ", {
"Fn::GetAtt" : ["IAMUserAccessKey", "SecretAccessKey"]
}, "\n", "</script>"]]
}
}
}
}
},
"Outputs" : {}
}