amazon web services - Reusing AWS::CloudFormation::Init (and userdata?) for multiple instances -


is possible reuse same bootstrapping config aws::cloudformation::init (and/or userdata) multiple ec2::instances in template?

i need set content of 3 files , run 3 commands bootstrap servers, metadata block 30 lines long (and grow). each server instance has different set of tags, have more tags others.

ideally, think should able declare aws::cloudformation::init resource, , refer multiple ec2::instances, don't think possible.

i thought (as newbie) aws::cloudformation::customresource might appropriate, don't think is.

i'm thinking of using aws::cloudformation::stack import shared instance template, need somehow pass tags parameter each resource in stack template instance template. question - if best approach, enter in 3 locations have ?????

(bonus credit - what's difference between userdata , init block?)

stack.template

... "resources" : {   "server1" : {     "type": "aws::cloudformation::stack",     "properties": {       "parameters": {         "instancetype": "m1.medium",         ...         "tags": { ???? }       },       "templateurl": "https://s3.amazonaws.com/mybucket/instance.template"     } 

instance.template

... "parameters" : {    "instancetype" : {...}    "keyname": {...}    ...    "tags": {        ????    } },  "resources" : {     "instance" : {       "type" : "aws::ec2::instance",       "metadata" : {         "aws::cloudformation::init" : {           "config" : {             "files" : {               "/etc/apt/apt.conf.d/99auth" : {                 "content" : "apt::get::allowunauthenticated yes;"               },               "/etc/apt/sources.list.d/my-repo.list" : {                 "content" : "deb  http://my-repo/repo apt/"               },           },             "commands" : {               "01-apt-get update" : {                 "command" : "apt-get update"               },               "02-apt-get install puppet" : {                 "command" : "apt-get install puppet my-puppet-config"               },               "03-puppet apply" : {                 "command" : "puppet apply"               }             }           }         }       },       "properties" : {         "instancetype" : {"ref" : "instancetype"},         "imageid" : "ami-84a333be",         "keyname" : {"ref" : "keyname"},         "subnetid" : {"ref" : "subnetid"},         "securitygroupids" : [ { "ref" : "securitygroupid"] } ],         "tags" : [           ????         ]       }     } } 

is possible reuse same bootstrapping config aws::cloudformation::init (and/or userdata) multiple ec2::instances in template?

no, not possible aws::ec2::instance resource in single template. however, there resource type aws::autoscaling::launchconfiguration, today resource applicable auto scaling groups. ideally, aws provide similar resource type can applied multiple aws::ec2::instance resources. being said, there lot of value in using auto scaling groups.

here simple example allow single launch configuration , multiple auto scaling group resources in single template. auto scaling typically configured more 1 instance, using minsize , maxsize of 1 mirror configuration going after aws::ec2::instance resource type. though using single instance auto scaling group, still gain benefits of auto scaling single instance resiliency. if instance becomes unhealthy, auto scaling automatically replace instance.

{   "awstemplateformatversion": "2010-09-09",   "resources" : {     "launchconfig" : {       "type" : "aws::autoscaling::launchconfiguration",       "properties" : {         "instancetype" : { "ref" : "instancetype" },         "imageid" : "ami-84a333be",         "keyname" : { "ref" : "keyname" },         "securitygroupids" : [{"ref" : "securitygroupid"}],         "userdata" : { "fn::base64" : { "fn::join" : [ "", [           "#!/bin/bash -v\n",            "# run cfn-init\n",           "/opt/aws/bin/cfn-init -v ",           "    -stack ", { "ref": "aws::stackname" },           "    -resource launchconfig ",           "    --region ", { "ref" : "aws::region" }, "\n",            "# signal success\n",           "/opt/aws/bin/cfn-signal -e $? '", { "ref" : "waitconditionhandle" }, "'\n"         ]]}}       },       "metadata" : {         "aws::cloudformation::init" : {           "config" : {             "files" : {               "/etc/apt/apt.conf.d/99auth" : {                 "content" : "apt::get::allowunauthenticated yes;"               },               "/etc/apt/sources.list.d/my-repo.list" : {                 "content" : "deb  http://my-repo/repo apt/"               }             },             "commands" : {               "01-apt-get update" : {                 "command" : "apt-get update"               },               "02-apt-get install puppet" : {                 "command" : "apt-get install puppet my-puppet-config"               },               "03-puppet apply" : {                 "command" : "puppet apply"               }             }           }         }       }     },        "asg1" : {       "type" : "aws::autoscaling::autoscalinggroup",       "properties" : {         "availabilityzones" : [ { "ref" : "az" } ],         "vpczoneidentifier" : [ { "ref" : "subnetid" } ],         "launchconfigurationname" : { "ref" : "launchconfig" },         "maxsize" : "1",         "minsize" : "1",         "tags" : [           { "key" : "name", "value": "server1", "propagateatlaunch" : "true" },           { "key" : "version", "value": "1.0", "propagateatlaunch" : "true" }         ]       }     },     "asg2" : {       "type" : "aws::autoscaling::autoscalinggroup",       "properties" : {         "availabilityzones" : [ { "ref" : "az" } ],         "vpczoneidentifier" : [ { "ref" : "subnetid" } ],         "launchconfigurationname" : { "ref" : "launchconfig" },         "maxsize" : "1",         "minsize" : "1",         "tags" : [           { "key" : "name", "value": "server2", "propagateatlaunch" : "true" },           { "key" : "version", "value": "1.0", "propagateatlaunch" : "true" }         ]       }     },     "waitconditionhandle" : {       "type" : "aws::cloudformation::waitconditionhandle"     },     "waitcondition" : {       "type" : "aws::cloudformation::waitcondition",       "properties" : {         "handle" : { "ref" : "waitconditionhandle" },         "timeout" : "300"       }     }   } } 

this incomplete example , there more can auto scaling, wanted give simple example sharing launch configuration multiple instances. each auto scaling group define own set of tags.


i'm thinking of using aws::cloudformation::stack import shared instance template, need somehow pass tags parameter each resource in stack template instance template. question - if best approach, enter in 3 locations have ?????

i recommend solution described above, answer questions on how use tags nested stack approach.

stack.template

{   "awstemplateformatversion": "2010-09-09",   "resources" : {     "server1" : {       "type": "aws::cloudformation::stack",       "properties": {         "templateurl": "https://s3.amazonaws.com/mybucket/instance.template",         "parameters": {           "instancetype": "m1.medium",           "tagname": "server1"           "tagversion": "1.0"         }       }     },     "server2" : {       "type": "aws::cloudformation::stack",       "properties": {         "templateurl": "https://s3.amazonaws.com/mybucket/instance.template",         "parameters": {           "instancetype": "m1.medium",           "tagname": "server2"           "tagversion": "1.0"         }       }     }   } } 

instance.template

{   "awstemplateformatversion": "2010-09-09",   "parameters" : {      "instancetype" : {...},      "keyname": {...}      "tagname": {        "description" : "the name tag applied each instance",        "type" : "string"      },      "tagversion": {        "description" : "the version tag applied each instance",        "type" : "string"      }   },    "resources" : {     "instance" : {       "type" : "aws::ec2::instance",       "metadata" : {         "aws::cloudformation::init" : {           ...         }       },       "properties" : {         "instancetype" : { "ref" : "instancetype" },         "imageid" : "ami-84a333be",         "keyname" : { "ref" : "keyname" },         "subnetid" : { "ref" : "subnetid" },         "securitygroupids" : [ { "ref" : "securitygroupid"] } ],         "tags" : [           { "key" : "name", "value": { "ref" : "tagname" } },           { "key" : "version", "value": { "ref" : "tagversion" } },         ]       }     }   } } 

there not standard passing entire array of tags parameter, can see broke each tag out own parameter , passed these on nested stacks.


(bonus credit - what's difference between userdata , init block?)

userdata allows pass arbitrary data instance @ first boot. shell script can automate tasks when instance starts. example, run yum update.

"userdata" : { "fn::base64" : { "fn::join" : [ "", [   "#!/bin/bash\n"   "yum update -y", "\n" ]]}} 

userdata becomes more useful when combined aws::cloudformation::init metadata allows structure bootstrapping configuration. in case, userdata used invoke cfn-init script executes aws::cloudformation::init metadata. i've included pattern in first example above using launch configuration. important note userdata section executed once during first boot of instance. important keep in mind when thinking how want handle updates instances.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -