Skip to content

Commit

Permalink
feat(cloud-formation): Preserve existing tags
Browse files Browse the repository at this point in the history
Update the `cloud-formation` deployment type to preserve any existing tags on the CloudFormation stack.
  • Loading branch information
akash1810 committed Sep 26, 2024
1 parent f901012 commit fbfbb34
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
14 changes: 9 additions & 5 deletions magenta-lib/src/main/scala/magenta/tasks/ChangeSetTasks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CreateChangeSetTask(
)
)

val (stackName, changeSetType, existingParameters) =
val (stackName, changeSetType, existingParameters, currentTags) =
stackLookup.lookup(resources.reporter, cfnClient)

val template = processTemplate(
Expand Down Expand Up @@ -106,12 +106,14 @@ class CreateChangeSetTask(
resources.reporter.verbose(s"Using execution role $role")
)

val mergedTags = currentTags ++ stackTags

CloudFormation.createChangeSet(
resources.reporter,
stackLookup.changeSetName,
changeSetType,
stackName,
Some(stackTags),
Some(mergedTags),
template,
awsParameters,
maybeExecutionRole,
Expand Down Expand Up @@ -162,7 +164,7 @@ class CheckChangeSetCreatedTask(
): Unit = {
check(resources.reporter, stopFlag) {
CloudFormation.withCfnClient(keyRing, region, resources) { cfnClient =>
val (stackName, changeSetType, _) =
val (stackName, changeSetType, _, _) =
stackLookup.lookup(resources.reporter, cfnClient)
val changeSetName = stackLookup.changeSetName
val changeSet = CloudFormation.describeChangeSetByName(
Expand Down Expand Up @@ -240,7 +242,8 @@ class ExecuteChangeSetTask(
stopFlag: => Boolean
): Unit = {
CloudFormation.withCfnClient(keyRing, region, resources) { cfnClient =>
val (stackName, _, _) = stackLookup.lookup(resources.reporter, cfnClient)
val (stackName, _, _, _) =
stackLookup.lookup(resources.reporter, cfnClient)
val changeSetName = stackLookup.changeSetName

val changeSet = CloudFormation.describeChangeSetByName(
Expand Down Expand Up @@ -306,7 +309,8 @@ class DeleteChangeSetTask(
stopFlag: => Boolean
): Unit = {
CloudFormation.withCfnClient(keyRing, region, resources) { cfnClient =>
val (stackName, _, _) = stackLookup.lookup(resources.reporter, cfnClient)
val (stackName, _, _, _) =
stackLookup.lookup(resources.reporter, cfnClient)
val changeSetName = stackLookup.changeSetName

CloudFormation.deleteChangeSet(stackName, changeSetName, cfnClient)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class SetStackPolicyTask(
stopFlag: => Boolean
): Unit = {
CloudFormation.withCfnClient(keyRing, region, resources) { cfnClient =>
val (stackName, changeSetType, _) =
val (stackName, changeSetType, _, _) =
stackLookup.lookup(resources.reporter, cfnClient)
val policyDoc =
toPolicyDoc(stackPolicy, () => accountResourceTypes(cfnClient))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class CloudFormationStackMetadata(
def lookup(
reporter: DeployReporter,
cfnClient: CloudFormationClient
): (String, ChangeSetType, List[ExistingParameter]) = {
): (String, ChangeSetType, List[ExistingParameter], Map[String, String]) = {
val existingStack = strategy match {
case LookupByName(name) => CloudFormation.describeStack(name, cfnClient)
case LookupByTags(tags) =>
Expand All @@ -123,7 +123,12 @@ class CloudFormationStackMetadata(
reporter
)

(stackName, changeSetType, stackParameters)
val currentTags: Map[String, String] = existingStack match {
case Some(stack) => stack.tags().asScala.map(t => t.key -> t.value).toMap
case _ => Map.empty
}

(stackName, changeSetType, stackParameters, currentTags)
}
}

Expand Down

0 comments on commit fbfbb34

Please sign in to comment.