-
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
fix(events): imported ECS Task Definition cannot be used as target #13293
Changes from 1 commit
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 |
---|---|---|
|
@@ -38,6 +38,16 @@ export interface ITaskDefinition extends IResource { | |
* Return true if the task definition can be run on a Fargate cluster | ||
*/ | ||
readonly isFargateCompatible: boolean; | ||
|
||
/** | ||
* The networking mode to use for the containers in the task. | ||
*/ | ||
readonly networkMode: NetworkMode; | ||
|
||
/** | ||
* The name of the IAM role that grants containers in the task permission to call AWS APIs on your behalf. | ||
*/ | ||
readonly taskRole: iam.IRole; | ||
} | ||
|
||
/** | ||
|
@@ -175,10 +185,55 @@ export interface TaskDefinitionProps extends CommonTaskDefinitionProps { | |
readonly pidMode?: PidMode; | ||
} | ||
|
||
/** | ||
* The common task definition attributes used across all types of task definitions. | ||
*/ | ||
export interface CommonTaskDefinitionAttributes { | ||
/** | ||
* The arn of the task definition | ||
*/ | ||
readonly taskDefinitionArn: string; | ||
|
||
/** | ||
* The networking mode to use for the containers in the task. | ||
* | ||
* @default NetworkMode.BRIDGE | ||
*/ | ||
readonly networkMode?: NetworkMode; | ||
|
||
/** | ||
* The name of the IAM role that grants containers in the task permission to call AWS APIs on your behalf. | ||
* | ||
* @default undefined. | ||
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. While What is more interesting is to write what the BEHAVIOR will be if you leave the value out. In this case something like 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. Will do. |
||
*/ | ||
readonly taskRole?: iam.IRole; | ||
} | ||
|
||
/** | ||
* A reference to an existing task definition | ||
*/ | ||
export interface TaskDefinitionAttributes extends CommonTaskDefinitionAttributes { | ||
/** | ||
* Execution role for this task definition | ||
* | ||
* @default: undefined | ||
*/ | ||
readonly executionRole?: iam.IRole; | ||
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 don't understand why 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. Good catch. We might be able to get rid of
I was originally hoping to give users flexibility to define Thoughts? 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.
Agreed. iirc |
||
|
||
/** | ||
* What launch types this task definition should be compatible with. | ||
* | ||
* @default Compatibility.EC2_AND_FARGATE | ||
*/ | ||
readonly compatibility?: Compatibility; | ||
} | ||
|
||
abstract class TaskDefinitionBase extends Resource implements ITaskDefinition { | ||
|
||
public abstract readonly compatibility: Compatibility; | ||
public abstract readonly networkMode: NetworkMode; | ||
public abstract readonly taskDefinitionArn: string; | ||
public abstract readonly taskRole: iam.IRole; | ||
public abstract readonly executionRole?: iam.IRole; | ||
|
||
/** | ||
|
@@ -207,10 +262,33 @@ export class TaskDefinition extends TaskDefinitionBase { | |
* The task will have a compatibility of EC2+Fargate. | ||
*/ | ||
public static fromTaskDefinitionArn(scope: Construct, id: string, taskDefinitionArn: string): ITaskDefinition { | ||
return TaskDefinition.fromTaskDefinitionAttributes(scope, id, { taskDefinitionArn: taskDefinitionArn }); | ||
} | ||
|
||
/** | ||
* Create a task definition from a task definition reference | ||
*/ | ||
public static fromTaskDefinitionAttributes(scope: Construct, id: string, attrs: TaskDefinitionAttributes): ITaskDefinition { | ||
class Import extends TaskDefinitionBase { | ||
public readonly taskDefinitionArn = taskDefinitionArn; | ||
public readonly compatibility = Compatibility.EC2_AND_FARGATE; | ||
public readonly executionRole?: iam.IRole = undefined; | ||
public readonly taskDefinitionArn = attrs.taskDefinitionArn; | ||
public readonly compatibility = attrs.compatibility ?? Compatibility.EC2_AND_FARGATE; | ||
public readonly executionRole = attrs.executionRole; | ||
|
||
public get networkMode(): NetworkMode { | ||
if (attrs.networkMode == undefined) { | ||
throw new Error('NetworkMode is available only if it is given when importing the TaskDefinition.'); | ||
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. Almost there. This error message makes sense to a user if they are calling However, in 99% of cases the user will be PASSING this TaskDefinition to some other construct or function, and THAT one will be calling // User writes this:
const taskDefintiion = TaskDefintion.fromTaskDefinitionAttributes(...);
startNewService(taskDefinition);
// User gets error:
"NetworkMode is available only if it is given..." And the user's thought will be "where is this NetworkMode coming from?" A more accurate and useful message would be something like:
Or somethign. 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. Got it. Will update. |
||
} else { | ||
return attrs.networkMode; | ||
} | ||
} | ||
|
||
public get taskRole(): iam.IRole { | ||
if (attrs.taskRole == undefined) { | ||
throw new Error('TaskRole is available only if it is given when importing the TaskDefinition.'); | ||
} else { | ||
return attrs.taskRole; | ||
} | ||
} | ||
} | ||
|
||
return new Import(scope, id); | ||
|
@@ -248,7 +326,7 @@ export class TaskDefinition extends TaskDefinitionBase { | |
public defaultContainer?: ContainerDefinition; | ||
|
||
/** | ||
* The task launch type compatiblity requirement. | ||
* The task launch type compatibility requirement. | ||
*/ | ||
public readonly compatibility: Compatibility; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import { Resource } from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import { CommonTaskDefinitionProps, Compatibility, IpcMode, ITaskDefinition, NetworkMode, PidMode, TaskDefinition } from '../base/task-definition'; | ||
import { | ||
CommonTaskDefinitionAttributes, | ||
CommonTaskDefinitionProps, | ||
Compatibility, | ||
IpcMode, | ||
ITaskDefinition, | ||
NetworkMode, | ||
PidMode, | ||
TaskDefinition, | ||
} from '../base/task-definition'; | ||
import { PlacementConstraint } from '../placement'; | ||
|
||
/** | ||
|
@@ -51,6 +61,13 @@ export interface IEc2TaskDefinition extends ITaskDefinition { | |
|
||
} | ||
|
||
/** | ||
* Attributes used to import an existing EC2 task definition | ||
*/ | ||
export interface Ec2TaskDefinitionAttributes extends CommonTaskDefinitionAttributes { | ||
|
||
} | ||
|
||
/** | ||
* The details of a task definition run on an EC2 cluster. | ||
* | ||
|
@@ -62,12 +79,42 @@ export class Ec2TaskDefinition extends TaskDefinition implements IEc2TaskDefinit | |
* Imports a task definition from the specified task definition ARN. | ||
*/ | ||
public static fromEc2TaskDefinitionArn(scope: Construct, id: string, ec2TaskDefinitionArn: string): IEc2TaskDefinition { | ||
return Ec2TaskDefinition.fromEc2TaskDefinitionAttributes( | ||
scope, id, { taskDefinitionArn: ec2TaskDefinitionArn }, | ||
); | ||
} | ||
|
||
/** | ||
* Imports an existing Ec2 task definition from its attributes | ||
*/ | ||
public static fromEc2TaskDefinitionAttributes( | ||
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't this whole implementation just defer to Oh I guess it couldn't because the types are different... 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 wonder if it might be helpful to define a private class that represents the
And it could contain a:
To cut down on the duplication between these 3 methods. If this sounds too daunting, I'm okay leaving it as is. 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. Thanks. Will try. A callout is that, there's a little difference in |
||
scope: Construct, | ||
id: string, | ||
attrs: Ec2TaskDefinitionAttributes, | ||
): IEc2TaskDefinition { | ||
class Import extends Resource implements IEc2TaskDefinition { | ||
public readonly taskDefinitionArn = ec2TaskDefinitionArn; | ||
public readonly taskDefinitionArn = attrs.taskDefinitionArn; | ||
public readonly compatibility = Compatibility.EC2; | ||
public readonly isEc2Compatible = true; | ||
public readonly isFargateCompatible = false; | ||
|
||
public get networkMode(): NetworkMode { | ||
if (attrs.networkMode == undefined) { | ||
throw new Error('NetworkMode is available only if it is given when importing the Ec2 TaskDefinition.'); | ||
} else { | ||
return attrs.networkMode; | ||
} | ||
} | ||
|
||
public get taskRole(): iam.IRole { | ||
if (attrs.taskRole == undefined) { | ||
throw new Error('TaskRole is available only if it is given when importing the Ec2 TaskDefinition.'); | ||
} else { | ||
return attrs.taskRole; | ||
} | ||
} | ||
} | ||
|
||
return new Import(scope, id); | ||
} | ||
|
||
|
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.
Mistake. Should be something like
No network mode can be provided
.