amazon web services - How to write my cloud formation more reusable instead of renaming resources -


i new cloudformation , writing cfn code infrastructure of project. there issue bugs me time when writing code. let's have following resources create:

resources:  myec2instance:    type: "aws::ec2::instance"   properties:      imageid: "ami-79fd7eee"     keyname: "testkey"     blockdevicemappings:        - devicename: "/dev/sdm"         ebs:            volumetype: "io1"           iops: "200"           deleteontermination: "false"           volumesize: "20"       - devicename: "/dev/sdk"         nodevice: {} 

as see creating resource called myec2instance. lets have environment called stg exact same above easy way use above code different stack name have been told need rename resource name follows:

resources:  myec2instancestg1:    type: "aws::ec2::instance"   properties:      imageid: "ami-79fd7eee"     keyname: "testkey"     blockdevicemappings:        - devicename: "/dev/sdm"         ebs:            volumetype: "io1"           iops: "200"           deleteontermination: "false"           volumesize: "20"       - devicename: "/dev/sdk"         nodevice: {} 

but me not professional since if have 10 environments mean need replicate code 10 times , rename resources. there better way that?

the way handle on team have single shared template used generate stacks each of our environments: dev, staging , production. logical ids between each of environments identical, generated physical ids different.

the 1 caveot on if have different environments stacks in same account, have ensure name properties unique. if aren't required, don't set them, , cloudformation generate them you. in case required find {"fn::sub": "${aws::stackname}-somename"} helpful this, makes each of physical resources names relative environment stack. example, codebuild, project name required, like:

resources:   project:     type: aws::codebuild::project     properties:       name: !sub "${aws::stackname}-slackbotlambda"       ... 

so if i'm on dev stack, makes codebuild project named dev-slackbotlambda.

another strategy employ passing in environmentname parameter, can have resources created on 1 environment , not on another. example, want have build artifacts created in dev , shared staging , production, have buckets created this:

parameters:   environmentname:     type: string     allowedvalues:       - dev       - staging       - production    conditions:     shouldgenerateartifactbucket: !equals [!ref environemntname, dev]    resources:     artifactbucket:       type: aws::s3::bucket       condition: shouldgenerateartifactbucket 

as long stack names unique enough, employ first strategy second make bucket have predictable enough name 1 stack reference bucket artifact first.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -