Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Stack Set support #977

Merged
merged 1 commit into from
Dec 22, 2021
Merged

feat: Add Stack Set support #977

merged 1 commit into from
Dec 22, 2021

Conversation

akash1810
Copy link
Member

@akash1810 akash1810 commented Dec 13, 2021

What does this change?

Create constructs to support the creation of AWS::CloudFormation::StackSet resources. Unfortunately, AWS CDK does not (yet) have a L2 construct for Stack Sets 😢 .

Usage:

// the infrastructure to create in target accounts
class AccountAlertTopic extends GuStackForStackSetInstance {
  constructor(id: string, props: GuStackProps) {
    super(id, props);

    new GuSnsTopic(this, "topic-for-alerts");
  }
}

// the infrastructure to create in the root account
class ParentStack extends GuStackForInfrastructure {
  constructor(scope: App, id: string, props: GuStackProps) {
    super(scope, id, props);

    new GuStackSet(this, `${id}StackSet`, {
      stackSetInstance: new AccountAlertTopic("Alerts", { stack: props.stack }),
      name: "centralised-alarms",
      description: "Provisioning of standard account alerting resources",
      organisationUnitTargets: [ "o-abcde12345" ]
    });
  }
}

// contents of `bin/cdk.ts`
new ParentStack(new App(), "AccountAlarmResources", { stack: "alarms" })

This will produce a CloudFormation template like this:

Resources:
  AccountAlarmResourcesStackSet:
    Type: AWS::CloudFormation::StackSet
    Properties:
      PermissionModel: SERVICE_MANAGED
      StackSetName: centralised-alarms
      AutoDeployment:
        Enabled: true
        RetainStacksOnAccountRemoval: false
      Description: Provisioning of standard account alerting resources
      Parameters: []
      StackInstancesGroup:
        - DeploymentTargets:
            OrganizationalUnitIds:
              - o-abcde12345
          Regions:
            - Ref: AWS::Region
      Tags:
        - Key: gu:cdk:version
          Value: TEST
        - Key: gu:repo
          Value: guardian/cdk
        - Key: Stack
          Value: alarms
        - Key: Stage
          Value: INFRA
      TemplateBody: |-
        {
          "Resources": {
            "topicforalerts57330FBE": {
              "Type": "AWS::SNS::Topic",
              "Properties": {
                "Tags": [
                  {
                    "Key": "gu:cdk:version",
                    "Value": "TEST"
                  },
                  {
                    "Key": "gu:repo",
                    "Value": "guardian/cdk"
                  },
                  {
                    "Key": "Stack",
                    "Value": "alarms"
                  },
                  {
                    "Key": "Stage",
                    "Value": "INFRA"
                  }
                ]
              }
            }
          }
        }

See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudformation-stackset.html.

Note: this does not support provisioning of stack sets to specific accounts as I'm not sure why we'd ever do this.

How to test

See added tests.

How can we measure success?

We get a step closer to a better stack set deployment story as, by provisioning stack sets as just another resource in a CloudFormation template, we're closer to being able to deploy them via RIff-Raff like any other infrastructure.

Have we considered potential risks?

Do the construct names make sense and are they intention revealing enough?

Checklist

  • I have listed any breaking changes, along with a migration path 1
  • I have updated the documentation as required for the described changes 2

Footnotes

  1. Consider whether this is something that will mean changes to projects that have already been migrated, or to the CDK CLI tool. If changes are required, consider adding a checklist here and/or linking to related PRs.

  2. If you are adding a new construct or pattern, has new documentation been added? If you are amending defaults or changing behaviour, are the existing docs still valid?

Create constructs to support the creation of `AWS::CloudFormation::StackSet` resources.

Usage:
```typescript
// the infrastructure to create in target accounts
class AccountAlertTopic extends GuStackForStackSetInstance {
  constructor(id: string, props: GuStackProps) {
    super(id, props);

    new GuSnsTopic(this, "topic-for-alerts");
  }
}

class ParentStack extends GuStackForInfrastructure {
  constructor(scope: App, id: string, props: GuStackProps) {
    super(scope, id, props);

    new GuStackSet(this, `${id}StackSet`, {
      stackSetInstance: new AccountAlertTopic("Alerts", { stack: props.stack }),
      name: "centralised-alarms",
      description: "Provisioning of standard account alerting resources",
      organisationUnitTargets: [ "o-abcde12345" ]
    });
  }
}

// contents of `bin/cdk.ts`
new ParentStack(new App(), "AccountAlarmResources", { stack: "alarms" })
```

This will produce a CloudFormation template like this:
```yaml
Resources:
  AccountAlarmResourcesStackSet:
    Type: AWS::CloudFormation::StackSet
    Properties:
      PermissionModel: SERVICE_MANAGED
      StackSetName: centralised-alarms
      AutoDeployment:
        Enabled: true
        RetainStacksOnAccountRemoval: false
      Description: Provisioning of standard account alerting resources
      Parameters: []
      StackInstancesGroup:
        - DeploymentTargets:
            OrganizationalUnitIds:
              - o-abcde12345
          Regions:
            - Ref: AWS::Region
      Tags:
        - Key: gu:cdk:version
          Value: TEST
        - Key: gu:repo
          Value: guardian/cdk
        - Key: Stack
          Value: alarms
        - Key: Stage
          Value: INFRA
      TemplateBody: |-
        {
          "Resources": {
            "topicforalerts57330FBE": {
              "Type": "AWS::SNS::Topic",
              "Properties": {
                "Tags": [
                  {
                    "Key": "gu:cdk:version",
                    "Value": "TEST"
                  },
                  {
                    "Key": "gu:repo",
                    "Value": "guardian/cdk"
                  },
                  {
                    "Key": "Stack",
                    "Value": "alarms"
                  },
                  {
                    "Key": "Stage",
                    "Value": "INFRA"
                  }
                ]
              }
            }
          }
        }
```

See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudformation-stackset.html.
@akash1810 akash1810 requested a review from a team December 13, 2021 08:52
/**
* A GuStack but designed for Stack Set instances.
*
* In a stack set application, `GuStackForStackSetInstance` is used to represent the infrastructure to provision in target AWS accounts.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this naming very clear 👍

Copy link
Contributor

@jacobwinch jacobwinch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work 💯

@akash1810 akash1810 merged commit ec0552a into main Dec 22, 2021
@akash1810 akash1810 deleted the aa-stack-set-support branch December 22, 2021 18:15
@github-actions
Copy link
Contributor

🎉 This PR is included in version 31.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

akash1810 added a commit that referenced this pull request Dec 14, 2023
Removes supports for Stack Sets (added in #977) as it's no longer used,
because of a lack of CD tooling support for deploying Stack Sets.

Removing unused code means less code to maintain, and reduced complexity.

Should Stack Sets be needed in future, https://github.com/cdklabs/cdk-stacksets offers an alternative approach to creating them in CDK.
akash1810 added a commit that referenced this pull request Dec 20, 2023
Removes supports for Stack Sets (added in #977) as it's no longer used,
because of a lack of CD tooling support for deploying Stack Sets.

Removing unused code means less code to maintain, and reduced complexity.

Should Stack Sets be needed in future, https://github.com/cdklabs/cdk-stacksets offers an alternative approach to creating them in CDK.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants