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

refactor(GuLogShippingPolicy)!: make GuLogShippingPolicy a singleton #337

Merged
merged 3 commits into from
Mar 19, 2021

Conversation

akash1810
Copy link
Member

@akash1810 akash1810 commented Mar 19, 2021

What does this change?

This is ultimately part of #332.

This policy is now a singleton as we only ever want one per stack. If there are multiple roles defined in this stack that require log shipping permissions, a single policy will be defined and attached to the roles.

This DRYness helps keep within the file size limits of cloudformation too.

Also included in this change is a new GuLoggingStreamNameParameter, also written as a singleton for the same reasons.

To make the review easier, each commit can be read in sequence as they have been crafted to tell the story.

Usage hasn't really changed as this policy will never really be used by consumer stacks as they'l be using GuInstanceRole instead, which adds this policy by default. However, usage looks roughly like this:

const stack = new GuStack();
const myRole = new GuRole(stack);

const policy = GuLogShippingPolicy.getInstance(stack);
policy.attachToRole(myRole);

It's worth noting that #92 was a naive implementation of singleton parameters and this new style removes the need to have the getParam helper function on the GuStack.

BREAKING CHANGE:

  • The API for GuLogShippingPolicy has changed. It is now a singleton and direct instantiation is not possible.

Does this change require changes to existing projects or CDK CLI?

Yes. They'll need a version bump by as GuLogShippingPolicy is provided through the GuInstanceRole, this should be a no-op.

How to test

See updated and new tests. Either by running them or observing CI.

How can we measure success?

A much simpler API and ground work for supporting multiple apps (for example a backend api and frontend web app microservice) within a single stack.

Have we considered potential risks?

n/a

This parameter is written as a singleton as we only ever want one per stack - if there are multiple apps defined in the stack, they'll all share the same parameter (as opposed to the AMI parameter which is app specific).
…ter` parameter

Also extend `GuAllowPolicy` to stay DRY.
This policy is now a singleton as we only ever want one per stack. If there are multiple roles defined in this stack that require log shipping permissions, a single policy will be defined and attached to the roles. This DRYness helps keep within the file size limits of cloudformation.
@akash1810 akash1810 requested a review from a team March 19, 2021 06:19
@@ -4,10 +4,10 @@ import type { Stack } from "@aws-cdk/core";
import { Tags } from "@aws-cdk/core";

// IAM Policies need to be attached to a role, group or user to be created in a stack
export const attachPolicyToTestRole = (stack: Stack, policy: Policy, app: string = "testing"): void => {
const role = new Role(stack, "TestRole", {
export const attachPolicyToTestRole = (stack: Stack, policy: Policy, id: string = "TestRole"): void => {
Copy link
Member Author

Choose a reason for hiding this comment

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

The default value of the app param was never overridden, so (safely) removed it.

Comment on lines +16 to +18
Type: "AWS::SSM::Parameter::Value<String>",
Default: "/account/services/logging.stream.name",
Description: "SSM parameter containing the Name (not ARN) on the kinesis stream",
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

@stephengeller stephengeller 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! Clear changes that make sense and thanks for the PR making it clear how to understand and review this!

@akash1810 akash1810 merged commit 51ea983 into main Mar 19, 2021
@akash1810 akash1810 deleted the aa-log-shipping-param branch March 19, 2021 08:47
@github-actions
Copy link
Contributor

🎉 This PR is included in version 4.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

akash1810 added a commit that referenced this pull request Mar 24, 2021
Following #337 and part of a bigger bit of work in #332, this makes `GuDistributionBucketParameter` a singleton as we only ever want one of them in a stack.

BREAKING CHANGE:
* The removal of the static `parameterName` field means `GuDistributionBucketParameter.parameterName` becomes `GuDistributionBucketParameter.getInstance(stack).id`
akash1810 added a commit that referenced this pull request Mar 24, 2021
A `GuInstanceRole` provides the basic permissions needed by EC2, it grants access to:
* SSM
* Describing tags
* Writing to kinesis for log shipping

#326 started the work to support the declaration of multiple apps in a stack, where an app is a collection of ASG, LB etc. However, when actually attempting to use the updated constructs in reality, the synth step failed due to multiple constructs using the same ID.

This change follows #341 and #337 and converts `GuSSMRunCommandPolicy` and `GuDescribeEC2Policy` to singletons. This means these policies will only get created once regardless of how many roles use them - there will be one policy with multiple roles attached to it. This helps keep the stack DRY and also reduces the chance of reaching the max file size limits of cloudformation.

The commits have been crafted to be standalone, reviewing them one by one might make the overall review easier.

Requires #341.

BREAKING CHANGE:
* `GuSSMRunCommandPolicy` and `GuDescribeEC2Policy` can no longer be directly instantiated
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