-
Notifications
You must be signed in to change notification settings - Fork 4k
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(ecs): make cluster and vpc optional for higher level constructs #2773
Changes from 12 commits
7b6dece
10708f4
f7896ee
da87dd9
deee9cc
bbeb602
b61209d
8d73f37
c6fdd60
66ee656
a213221
02f9dd9
ea2b992
0f538c5
493fdec
23282d0
d11fd6c
50510ec
4c6855b
d60c06c
d0bd65a
e5ef6de
a892832
ce23026
0498958
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { ICertificate } from '@aws-cdk/aws-certificatemanager'; | ||
import ec2 = require('@aws-cdk/aws-ec2'); | ||
import ecs = require('@aws-cdk/aws-ecs'); | ||
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2'); | ||
import { AddressRecordTarget, ARecord, IHostedZone } from '@aws-cdk/aws-route53'; | ||
|
@@ -16,8 +17,19 @@ export enum LoadBalancerType { | |
export interface LoadBalancedServiceBaseProps { | ||
/** | ||
* The cluster where your service will be deployed | ||
* You can only specify either vpc or cluster. Alternatively, you can leave both blank | ||
* | ||
* @default - create a new cluster; if you do not specify a cluster nor a vpc, a new VPC will be created for you as well | ||
*/ | ||
readonly cluster?: ecs.ICluster; | ||
|
||
/** | ||
* VPC that the cluster instances or tasks are running in | ||
* You can only specify either vpc or cluster. Alternatively, you can leave both blank | ||
* | ||
* @default - use vpc of cluster or create a new one | ||
*/ | ||
readonly cluster: ecs.ICluster; | ||
readonly vpc?: ec2.IVpc; | ||
|
||
/** | ||
* The image to start. | ||
|
@@ -108,11 +120,21 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { | |
|
||
public readonly targetGroup: elbv2.ApplicationTargetGroup | elbv2.NetworkTargetGroup; | ||
|
||
public readonly cluster: ecs.ICluster; | ||
|
||
public readonly logDriver?: ecs.LogDriver; | ||
|
||
constructor(scope: cdk.Construct, id: string, props: LoadBalancedServiceBaseProps) { | ||
super(scope, id); | ||
|
||
if (props.cluster && props.vpc) { | ||
throw new Error(`You can only specify either vpc or cluster. Alternatively, you can leave both blank`); | ||
} else if (props.cluster) { | ||
hoegertn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.cluster = props.cluster; | ||
} else { | ||
this.cluster = new ecs.Cluster(this, 'Cluster', { vpc: props.vpc }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like what you're doing here, but this would better fit in the high level FargateService constructs, not in the BaseService constructs. Cluster should be mandatory for any high level Ec2Service constructs as you'll need to add capacity to the cluster. What you have would work for FargateService constructs, but not Ec2Service constructs. Also, can you use the default cluster here? If one exists use it, otherwise create the default cluster? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 that this makes much more sense for Fargate then for EC2, so I rather optimize for this use case. As for default cluster, I am not aware that we have a concept like that, but should be fairly easy to introduce: function getCreateDefaultCluster(scope: Construct, vpc?: ec2.Vpc): ecs.Cluster {
const DEFAULT_CLUSTER_ID = 'EcsDefaultCluster<plug-in-some-uuid>';
const stack = Stack.of(this);
return stack.tryGetChild(id) as ecs.Cluster || new ecs.Cluster(stack, id, { vpc });
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the properties are set and only available in the base I added this code here. I am open to moving this to the FargateService. As I understood it, the concept of defaults is part of another issue or PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eladb ECS has a concept of a cluster literally called 'default'. The console first-run will create it, as will any ECS-optimized AMI-based instances that don't have another cluster name configured. So, many but not all accounts have a 'default' cluster already. Could we have something like the ImportedVpc and Vpc.fromLookup concepts for ECS clusters? i.e. a way to check if a cluster named 'default' exists in the account/region, and if not, add it to the stack. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_CreateCluster.html - clusterName - The name of your cluster. If you do not specify a name for your cluster, you create a cluster named default. Up to 255 letters (uppercase and lowercase), numbers, and hyphens are allowed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fully agree with this. If there is a great way to have this default cluster I am totally fine with it. But I like if CDK is not calling AWS before creating the template. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the default cluster has a static name 'default', another option could be to always use the 'default' cluster for the Fargate service if the cluster is not provided. Con: this would require the user to create the 'default' cluster manually in the account/region (through console first-run, etc), otherwise the stack creation will fail due to non-existent cluster. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, the VPC construct is already describing stuff about the account, like availability zones There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could implement a context provider for ECS clusters, probably worth to do generally. However, for this use case, since the default cluster must be created manually, I'd argue that we can simply define a CDK default cluster per stack in the method I described, as it will seamlessly work for any user, and will not create any conflicts between stacks/apps/users within the same account/region. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I will implement the default cluster per stack approach. I think one cluster per stack is a reasonable trade-off. |
||
} | ||
|
||
// Create log driver if logging is enabled | ||
const enableLogging = props.enableLogging !== undefined ? props.enableLogging : true; | ||
this.logDriver = enableLogging ? this.createAWSLogDriver(this.node.id) : undefined; | ||
|
@@ -127,7 +149,7 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { | |
const internetFacing = props.publicLoadBalancer !== undefined ? props.publicLoadBalancer : true; | ||
|
||
const lbProps = { | ||
vpc: props.cluster.vpc, | ||
vpc: this.cluster.vpc, | ||
internetFacing | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,23 @@ export = { | |
test.done(); | ||
}, | ||
|
||
'setting vpc and cluster throws error'(test: Test) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add a test here for providing a vpc directly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In this case, you want cluster to be required and ensure that capacity is added, fail if vpc is provided without capacity. |
||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
const vpc = new ec2.Vpc(stack, 'VPC'); | ||
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); | ||
|
||
// WHEN | ||
test.throws(() => new ecsPatterns.LoadBalancedEc2Service(stack, 'Service', { | ||
cluster, | ||
vpc, | ||
loadBalancerType: ecsPatterns.LoadBalancerType.NETWORK, | ||
image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app") | ||
})); | ||
|
||
test.done(); | ||
}, | ||
|
||
'test ECS loadbalanced construct with memoryReservationMiB'(test: Test) { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this logic to
LoadBalancedFargateService
as discussed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure how this should work, as the constructor needs the cluster and I cannot do stuff before calling super...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right. OK, let's leave this here, but make sure that for EC2 cluster is required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And how? The base class does not know about EC2 or Fargate, does it?
Why shouldn't it be possible to do:
I am open for any hint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The base class is abstract, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same when you provide a cluster but do not add capacity to it ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hoegertn is correct. We have other similar cases for “late validation”, like codepipeline (you can create a pipeline with no stages and then add them, but at least one is required). This essentially the only reason we have the “validate” hook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eladb the intent behind the constructs in ecs-patterns is to provide an opinionated approach to creating ecs services following what is considered best practices. I definitely agree that this works, but by relying on late validation, and not requiring capacity to be an input prop, we're not providing an opinion (allowing the user to decide if they want to add capacity when we know that they need to provide capacity), we're straying away from the intentions of the ecs-patterns module. @SoManyHs thoughts?
I would suggest that AddCapacityOptions (if cluster is not provided) or cluster with capacity already added to it should be required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree with this opinion I do not see how this PR changes the current behavior as it currently possible to add clusters without capacity. So I think we could have this PR as is and create an extra issue about late vs. instant validation of capacity whether providing a cluster or not. Any thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm okay with this, can you create an issue and add it?