Skip to content

Commit

Permalink
Add LB and TaskDef properties to ecs-patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
piradeepk committed Sep 25, 2019
1 parent fa1633b commit 15b132f
Show file tree
Hide file tree
Showing 20 changed files with 676 additions and 326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +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.
* The properties required to create a new task definition. One of TaskDefinition or TaskImageOptions needs to be defined.
*
* 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.
*
* For more information, see hostPort.
* Port mappings that are automatically assigned in this way do not count toward the 100 reserved ports limit of a container instance.
*
* @default 80
* @default none
*/
readonly containerPort?: number;
readonly taskImageOptions?: ApplicationLoadBalancedTaskImageOptions;

/**
* Determines whether the Load Balancer will be internet-facing.
Expand All @@ -70,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 @@ -100,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 @@ -117,32 +84,58 @@ 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;
}

export interface ApplicationLoadBalancedTaskImageOptions {
/**
* The image used to start a container. One of image or taskDefinition must be specified.
*
* @default - none
*/
readonly containerName?: string;
readonly image: ContainerImage;

/**
* The name of the service.
* The environment variables to pass to the container.
*
* @default - CloudFormation-generated name.
* @default - No environment variables.
*/
readonly serviceName?: string;
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 @@ -152,13 +145,39 @@ 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;

/**
* 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 containerName?: string;

/**
* 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.
*
* For more information, see hostPort.
* Port mappings that are automatically assigned in this way do not count toward the 100 reserved ports limit of a container instance.
*
* @default 80
*/
readonly containerPort?: number;
}

/**
Expand All @@ -183,23 +202,17 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {
*/
public readonly cluster: ICluster;

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 @@ -209,7 +222,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 @@ -274,7 +287,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 15b132f

Please sign in to comment.