这听起来像是 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" : {}
}