Ein Blick auf die AWS::CloudFormation::StackSet CloudFormation Resource

Als Deployment-Werkzeug der Wahl setzen wir zumeist AWS CloudFormation ein, denn es ist ein sehr bewährter und robuster Service: Da CloudFormation ein Managed Service ist, entstehen keine Kosten fuer Installation, Wartung oder Updates wie z.B. bei Terraform.

Die Grenzen von CloudFormation

Allerdings stößt man mit CloudFormation oft schnell an Grenzen. So waren komplexere Abhängigkeiten/Orchestrierungen bisher nicht nativ darzustellen, zum Beispiel:

  • Orchestrierung von StackSets und Abhängigkeiten über Accounts hinweg, zB “Lege erst X in Account Y” an, dann “Z in Account B”
  • Cross-Region Deployments von einzelnen Resourcen, zB “Lege ACM Certificate fuer CloudFront in us-east-1 an, das S3 Bucket aber in eu-central-1

Oft kommen dann Workarounds über CloudFormation Custom Resources zu tragen, die allerdings wiederum (Folge-)Kosten haben.

Es gibt mit zwar mit CloudFormation StackSets eine native Möglichkeit, CloudFormation Stacks über Regionen, AWS Accounts oder ganze Organizational Units auszurollen, nur konnte man bisher StackSets nicht mit CloudFormation an sich verwalten.

Die Lösung: Die neue AWS::Cloudformation::StackSet Resource

Die AWS::Cloudformation::StackSet Resource aus dem CloudFormation Resource Provider ist hier vielversprechend, denn mit ihr können StackSets nun durch CloudFormation Templates definiert werden. Schauen wir uns ein paar Beispiele an.

Beispiel: ACM Zertifikat in einer anderen Region erzeugen

So erzeugt ihr ein ACM Zertifikat in einer anderen Region als der Ursprungs-Stack. Das kann beispielsweise vorkommen, wenn ihr ein ACM Certificate für CloudFront in us-east-1 erstellen müsst, euer CloudFormation Stack aber in einer anderen Region “lebt”:

Resources:
  CertificateInUsEast1:
    Type: AWS::CloudFormation::StackSet
    Properties:
      StackSetName: !Ref AWS::StackName
      PermissionModel: SELF_MANAGED
      StackInstancesGroup:
        - Regions:
            - us-east-1
          DeploymentTargets:
            Accounts:
              - !Ref AWS::AccountId
      TemplateBody: |
        Resources:
          Certificate:
            Type: AWS::CertificateManager::Certificate
            Properties:
              DomainName: ssl.superluminar.io        

In diesem Beispiel erzeugt CloudFormation über die CertificateInUsEast1 Resource ein StackSet erzeugt, welches in us-east-1 im gleichen AWS Account ein CloudFormation Stack ausrollt. Dieser Stack enthält dann die Certificate Resource.

Hier wird das Permission Model SELF_MANAGED verwendet, d.h. ihr müsst ggf. selbst Rollen für CloudFormation im ausführenden Account (AdministrationRoleARN) sowie für die Sub-Accounts (ExecutionRoleName) bereitstellen. Nutzt ihr AWS Landing Zone oder AWS Control Tower, um AWS Accounts in eurer AWS Organization zu erstellen und zu verwalten, so ist euer AWS Setup bereits vorbereitet, wenn ihr aus eurem AWS Organizations Master Account heraus StackSets anlegen wollt:

  • Bei AWS Landing Zone sind die Defaults für AdministrationRoleARN und ExecutionRoleName bereits korrekt.
  • Nutzt ihr AWS Control Tower, ist für AdministrationRoleARN die Rolle AWSControlTowerStackSetRole, und für ExecutionRoleName ist es AWSControlTowerExecution.

Beispiel 2: Einen Stack über eine bestimmte OU ausrollen

Das folgende Beispiel zeigt, wie ihr Resourcen in die gesamte AWS Organization oder in bestimmte Organizational Units (OU) ausrollen könnt. In diesem Fall wird das Security Hub in jedem Account der OU, welche mit dem Parameter OU übergeben wird, aktiviert:

Parameters:
  OU:
    Type: String

Resources:
  Stackset:
    Type: AWS::CloudFormation::StackSet
    Properties:
      StackSetName: !Sub ${AWS::StackName}
      PermissionModel: SERVICE_MANAGED
      AutoDeployment:
        Enabled: true
        RetainStacksOnAccountRemoval: false
      StackInstancesGroup:
        - Regions:
          - !Ref AWS::Region
          DeploymentTargets:
            OrganizationalUnitIds:
              - !Ref OU
      TemplateBody: |
        Resources:
          SecurityHub:
            Type: AWS::SecurityHub::Hub

Beachtenswertes

  • Man kommt nicht ohne weiteres an die Outputs der per StackSets angelegten Stacks heran. Wollt ihr etwas aus dem Sub-Template referenzieren, so bleibt nur der Weg über das Synthetisieren der Resource-Namen/ARNs. Das ist nicht immer möglich, z. B. bei generierten ARNs wie ACM Zertifikaten.
  • StackSets haben Limits, nämlich 100 StackSets pro Account und Region, diese lassen sich allerdings erhöhen.
  • Die AWS::Cloudformation::StackSet Resource ist als CloudFormation Resource Provider open-source.

Fazit

Die native AWS::Cloudformation::StackSet CloudFormation Resource wird das Verwalten von Resourcen über AWS Accounts, Organizational Units und Regionen erheblich vereinfachen.

photo of Sönke

Sönke is Co-founder Senior Cloud Consultant at superluminar, AWS Partner Ambassador, AWS Community Builder, and AWS Certified Solutions Architect Professional, as well as AWS Certified DevOps Engineer Professional. He writes here primarily about his favourite topics: Professional AWS setups and software development. He can be found under the handle @s0enke on Twitter.