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 parameter helper functions on GuStack #92

Merged
merged 3 commits into from
Feb 17, 2021
Merged

Conversation

akash1810
Copy link
Member

@akash1810 akash1810 commented Dec 23, 2020

What does this change?

#65, but for parameters.

Helper functions to get/set parameters on the GuStack. An Error is thrown when getting a parameter that hasn't been set; this is to catch the error at compile time.

This is useful for when a construct reads from a parameter but isn't responsible for adding the parameter. For example, reading the DistributionBucketName param in the user data, where DistributionBucketName is added by GuGetDistributablePolicy via GuInstanceRole. One such example can be seen here.

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

No.

How to test

Observe CI.

How can we measure success?

It is simpler to share parameters across constructs.

Have we considered potential risks?

n/a

@akash1810 akash1810 added the needs-new-release Identifying significant changes to the library label Dec 23, 2020
@akash1810 akash1810 requested a review from a team December 23, 2020 12:59
src/constructs/core/stack.ts Show resolved Hide resolved
src/constructs/core/stack.test.ts Show resolved Hide resolved
src/constructs/core/stack.test.ts Show resolved Hide resolved
@akash1810 akash1810 marked this pull request as draft December 23, 2020 14:27
@jorgeazevedo jorgeazevedo self-requested a review December 23, 2020 14:36
Copy link
Contributor

@jorgeazevedo jorgeazevedo left a comment

Choose a reason for hiding this comment

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

Left some comments in the diff

@jorgeazevedo jorgeazevedo self-requested a review December 23, 2020 14:40
@jamie-lynch
Copy link
Contributor

Definitely one for discussion. This is quite similar to something we had right at the start which we then moved away from. I like this general approach in theory but we didn't quite get it right the first time so resorted to just using const parameters = { ... } but if there's now another reason to go back to this then I'd be keen to explore it again.

@akash1810 akash1810 removed the needs-new-release Identifying significant changes to the library label Jan 12, 2021
@akash1810
Copy link
Member Author

akash1810 commented Jan 15, 2021

I think I have a use case for this! 🎉

A UserData construct will look something like this:

#!/bin/bash -ev
aws --region ${this.region} s3 cp s3://${distributionBucketNameParam.valueAsString}/${this.stack}/${this.stage}/${this.app}/${this.app}.deb /tmp/
dpkg -i /tmp/${this.app}.deb

distributionBucketNameParam is a parameter added to the stack from GuGetDistributablePolicy, and we'd need a way of accessing it from within the UserData construct.

@akash1810 akash1810 force-pushed the aa-param-helper branch 3 times, most recently from cba8f4a to 05b9148 Compare January 16, 2021 10:56
@akash1810
Copy link
Member Author

Another use case is then you want to have multiple ASGs in a stack. Currently an ASG will bring an AMI parameter with it. This parameter is called "AMI" by default. If we try to add two ASGs to a stack, then we'll attempt to add the same parameter twice and an exception is thrown. If we have a helper function to manage params, then we can do some validation.

I'll revive this branch today.

@jacobwinch
Copy link
Contributor

Currently an ASG will bring an AMI parameter with it. This parameter is called "AMI" by default.

@akash1810 FYI I think it's possible to override this behaviour.

@akash1810
Copy link
Member Author

@akash1810 FYI I think it's possible to override this behaviour.

Ah! I keep forgetting about this!

I don't think this'll allow a single AMI param to be used in multiple ASGs however as a similar exception noted above will be thrown?

Sharing AMIs across ASGs is fairly common and reasonable; here's an example from Grid the single param AmiId is used in 9 ASGs.

@jacobwinch
Copy link
Contributor

I don't think this'll allow a single AMI param to be used in multiple ASGs however as a similar exception noted above will be thrown?

I haven't tested this, but I think this might work?

const mySharedAmi = new GuAmiParameter(stack, "MyAMI", {})

const myFirstAsg = new GuAutoScalingGroup(stack, "AutoscalingGroup", {
      // firstAsgProps
      imageId: mySharedAmi,
});

const mySecondAsg = new GuAutoScalingGroup(stack, "AutoscalingGroup", {
      // secondAsgProps
      imageId: mySharedAmi,
});

@akash1810
Copy link
Member Author

Oh lovely! Yeah that does work! The tests fail due to InstanceType being declared twice, but we can do a similar trick there too.

@akash1810 akash1810 changed the title add parameter helper functions on GuStack feat: add parameter helper functions on GuStack Feb 12, 2021
@akash1810 akash1810 marked this pull request as ready for review February 12, 2021 17:18
@akash1810 akash1810 requested a review from a team February 12, 2021 19:08
jacobwinch added a commit that referenced this pull request Feb 17, 2021
Helper functions to get/set parameters on the GuStack. An `Error` is thrown when getting a parameter that hasn't been set; this is to catch the error at compile time.
A GuParameter now relies on a function from GuStack, as GuStageParameter and GuStackParameter are automatically added to a GuStack, we cannot explicitly test them now as its recursuve.

Remove them in favour of a slightly implicit test in stack.test.ts.
These can be replaced with the new getParam helper.
@akash1810
Copy link
Member Author

Merging w/out full approval as @jorgeazevedo is currently on leave.

@akash1810 akash1810 merged commit 0f28c1e into main Feb 17, 2021
@akash1810 akash1810 deleted the aa-param-helper branch February 17, 2021 14:55
@akash1810 akash1810 added the needs-new-release Identifying significant changes to the library label Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-new-release Identifying significant changes to the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants