Skip to content

Commit

Permalink
feat(ecs-patterns): Allow overriding loadBalancer and `taskDefiniti…
Browse files Browse the repository at this point in the history
…on` (#4213)

Add LoadBalancer and TaskDefinition properties to be input to the constructor of all patterns.

BREAKING CHANGES:

* **ecs-patterns**: In order to support TaskDefinition as an input property, moved all properties that are used to create a TaskDefinition into it's own props object (TaskImageOptions) and accept that as input to all ecs-patterns constructs.
* **ecs-patterns**: Only one of TaskDefinition or TaskImageOptions can be passed as input to the constructs. Previously image was required, where as now image or taskDefinition is required, and having both will throw an exception.
  • Loading branch information
piradeepk authored and rix0rrr committed Sep 30, 2019
1 parent 19ae072 commit f2a6d46
Show file tree
Hide file tree
Showing 21 changed files with 710 additions and 357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,11 @@ export interface ApplicationLoadBalancedServiceBaseProps {
readonly vpc?: IVpc;

/**
* The image used to start a container.
*/
readonly image: ContainerImage;

/**
* The port number on the container that is bound to the user-specified or automatically assigned host port.
*
* If you are using containers in a task with the awsvpc or host network mode, exposed ports should be specified using containerPort.
* If you are using containers in a task with the bridge network mode and you specify a container port and not a host port,
* your container automatically receives a host port in the ephemeral port range.
* The properties required to create a new task definition. TaskDefinition or TaskImageOptions must be specified, but not both.
*
* Port mappings that are automatically assigned in this way do not count toward the 100 reserved ports limit of a container instance.
*
* For more information, see
* [hostPort](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_PortMapping.html#ECS-Type-PortMapping-hostPort).
*
* @default 80
* @default none
*/
readonly containerPort?: number;
readonly taskImageOptions?: ApplicationLoadBalancedTaskImageOptions;

/**
* Determines whether the Load Balancer will be internet-facing.
Expand All @@ -72,27 +58,6 @@ export interface ApplicationLoadBalancedServiceBaseProps {
*/
readonly certificate?: ICertificate;

/**
* The environment variables to pass to the container.
*
* @default - No environment variables.
*/
readonly environment?: { [key: string]: string };

/**
* The secret to expose to the container as an environment variable.
*
* @default - No secret environment variables.
*/
readonly secrets?: { [key: string]: Secret };

/**
* Flag to indicate whether to enable logging.
*
* @default true
*/
readonly enableLogging?: boolean;

/**
* The protocol for connections from clients to the load balancer.
* The load balancer port is determined from the protocol (port 80 for
Expand All @@ -102,7 +67,7 @@ export interface ApplicationLoadBalancedServiceBaseProps {
* @default HTTP. If a certificate is specified, the protocol will be
* set by default to HTTPS.
*/
readonly protocol?: ApplicationProtocol;
readonly protocol?: ApplicationProtocol;

/**
* The domain name for the service, e.g. "api.example.com."
Expand All @@ -119,32 +84,74 @@ export interface ApplicationLoadBalancedServiceBaseProps {
readonly domainZone?: IHostedZone;

/**
* The name of the task execution IAM role that grants the Amazon ECS container agent permission to call AWS APIs on your behalf.
* The name of the service.
*
* @default - No value
* @default - CloudFormation-generated name.
*/
readonly executionRole?: IRole;
readonly serviceName?: string;

/**
* The name of the task IAM role that grants containers in the task permission to call AWS APIs on your behalf.
* The period of time, in seconds, that the Amazon ECS service scheduler ignores unhealthy
* Elastic Load Balancing target health checks after a task has first started.
*
* @default - A task role is automatically created for you.
* @default - defaults to 60 seconds if at least one load balancer is in-use and it is not already set
*/
readonly taskRole?: IRole;
readonly healthCheckGracePeriod?: cdk.Duration;

/**
* The container name value to be specified in the task definition.
* The application load balancer that will serve traffic to the service.
*
* [disable-awslint:ref-via-interface]
*
* @default - a new load balancer will be created.
*/
readonly loadBalancer?: ApplicationLoadBalancer;

/**
* Specifies whether to propagate the tags from the task definition or the service to the tasks in the service.
* Tags can only be propagated to the tasks within the service during service creation.
*
* @default - none
*/
readonly containerName?: string;
readonly propagateTags?: PropagatedTagSource;

/**
* The name of the service.
* Specifies whether to enable Amazon ECS managed tags for the tasks within the service. For more information, see
* [Tagging Your Amazon ECS Resources](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-using-tags.html)
*
* @default - CloudFormation-generated name.
* @default false
*/
readonly serviceName?: string;
readonly enableECSManagedTags?: boolean;
}

export interface ApplicationLoadBalancedTaskImageOptions {
/**
* The image used to start a container. Image or taskDefinition must be specified, not both.
*
* @default - none
*/
readonly image: ContainerImage;

/**
* The environment variables to pass to the container.
*
* @default - No environment variables.
*/
readonly environment?: { [key: string]: string };

/**
* The secret to expose to the container as an environment variable.
*
* @default - No secret environment variables.
*/
readonly secrets?: { [key: string]: Secret };

/**
* Flag to indicate whether to enable logging.
*
* @default true
*/
readonly enableLogging?: boolean;

/**
* The log driver to use.
Expand All @@ -154,28 +161,41 @@ export interface ApplicationLoadBalancedServiceBaseProps {
readonly logDriver?: LogDriver;

/**
* The period of time, in seconds, that the Amazon ECS service scheduler ignores unhealthy
* Elastic Load Balancing target health checks after a task has first started.
* The name of the task execution IAM role that grants the Amazon ECS container agent permission to call AWS APIs on your behalf.
*
* @default - defaults to 60 seconds if at least one load balancer is in-use and it is not already set
* @default - No value
*/
readonly healthCheckGracePeriod?: cdk.Duration;
readonly executionRole?: IRole;

/**
* Specifies whether to propagate the tags from the task definition or the service to the tasks in the service.
* Tags can only be propagated to the tasks within the service during service creation.
* The name of the task IAM role that grants containers in the task permission to call AWS APIs on your behalf.
*
* @default - A task role is automatically created for you.
*/
readonly taskRole?: IRole;

/**
* The container name value to be specified in the task definition.
*
* @default - none
*/
readonly propagateTags?: PropagatedTagSource;
readonly containerName?: string;

/**
* Specifies whether to enable Amazon ECS managed tags for the tasks within the service. For more information, see
* [Tagging Your Amazon ECS Resources](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-using-tags.html)
* The port number on the container that is bound to the user-specified or automatically assigned host port.
*
* @default false
* If you are using containers in a task with the awsvpc or host network mode, exposed ports should be specified using containerPort.
* If you are using containers in a task with the bridge network mode and you specify a container port and not a host port,
* your container automatically receives a host port in the ephemeral port range.
*
* Port mappings that are automatically assigned in this way do not count toward the 100 reserved ports limit of a container instance.
*
* For more information, see
* [hostPort](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_PortMapping.html#ECS-Type-PortMapping-hostPort).
*
* @default 80
*/
readonly enableECSManagedTags?: boolean;
readonly containerPort?: number;
}

/**
Expand Down Expand Up @@ -213,26 +233,17 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {
*/
public readonly cluster: ICluster;

/**
* The log driver to use for logging.
*/
public readonly logDriver?: LogDriver;

/**
* Constructs a new instance of the ApplicationLoadBalancedServiceBase class.
*/
constructor(scope: cdk.Construct, id: string, props: ApplicationLoadBalancedServiceBaseProps) {
constructor(scope: cdk.Construct, id: string, props: ApplicationLoadBalancedServiceBaseProps = {}) {
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`);
throw new Error('You can only specify either vpc or cluster. Alternatively, you can leave both blank');
}
this.cluster = props.cluster || this.getDefaultCluster(this, props.vpc);

// Create log driver if logging is enabled
const enableLogging = props.enableLogging !== undefined ? props.enableLogging : true;
this.logDriver = props.logDriver !== undefined ? props.logDriver : enableLogging ? this.createAWSLogDriver(this.node.id) : undefined;

this.desiredCount = props.desiredCount || 1;

const internetFacing = props.publicLoadBalancer !== undefined ? props.publicLoadBalancer : true;
Expand All @@ -242,7 +253,7 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {
internetFacing
};

this.loadBalancer = new ApplicationLoadBalancer(this, 'LB', lbProps);
this.loadBalancer = props.loadBalancer !== undefined ? props.loadBalancer : new ApplicationLoadBalancer(this, 'LB', lbProps);

const targetProps = {
port: 80
Expand Down Expand Up @@ -313,7 +324,7 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {
this.targetGroup.addTarget(service);
}

private createAWSLogDriver(prefix: string): AwsLogDriver {
protected createAWSLogDriver(prefix: string): AwsLogDriver {
return new AwsLogDriver({ streamPrefix: prefix });
}
}
Loading

0 comments on commit f2a6d46

Please sign in to comment.