-
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
Merged
mergify
merged 3 commits into
aws:master
from
rli1994hi:rli1994hi/ecsTask-using-ITaskDefinition
Mar 3, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
packages/@aws-cdk/aws-ecs/lib/base/_imported-task-definition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { IRole } from '@aws-cdk/aws-iam'; | ||
import { Construct } from 'constructs'; | ||
import { IEc2TaskDefinition } from '../ec2/ec2-task-definition'; | ||
import { IFargateTaskDefinition } from '../fargate/fargate-task-definition'; | ||
import { Compatibility, NetworkMode, isEc2Compatible, isFargateCompatible } from './task-definition'; | ||
import { Resource } from '@aws-cdk/core'; | ||
|
||
/** | ||
* The properties of ImportedTaskDefinition | ||
*/ | ||
export interface ImportedTaskDefinitionProps { | ||
/** | ||
* The arn of the task definition | ||
*/ | ||
readonly taskDefinitionArn: string; | ||
|
||
/** | ||
* What launch types this task definition should be compatible with. | ||
* | ||
* @default Compatibility.EC2_AND_FARGATE | ||
*/ | ||
readonly compatibility?: Compatibility; | ||
|
||
/** | ||
* The networking mode to use for the containers in the task. | ||
* | ||
* @default Network mode cannot be provided to the imported task. | ||
*/ | ||
readonly networkMode?: NetworkMode; | ||
|
||
/** | ||
* The name of the IAM role that grants containers in the task permission to call AWS APIs on your behalf. | ||
* | ||
* @default Permissions cannot be granted to the imported task. | ||
*/ | ||
readonly taskRole?: IRole; | ||
} | ||
|
||
/** | ||
* Task definition reference of an imported task | ||
*/ | ||
export class ImportedTaskDefinition extends Resource implements IEc2TaskDefinition, IFargateTaskDefinition { | ||
/** | ||
* What launch types this task definition should be compatible with. | ||
*/ | ||
readonly compatibility: Compatibility; | ||
|
||
/** | ||
* ARN of this task definition | ||
*/ | ||
readonly taskDefinitionArn: string; | ||
|
||
/** | ||
* Execution role for this task definition | ||
*/ | ||
readonly executionRole?: IRole = undefined; | ||
|
||
/** | ||
* 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?: IRole; | ||
|
||
constructor(scope: Construct, id: string, props: ImportedTaskDefinitionProps) { | ||
super(scope, id); | ||
|
||
this.compatibility = props.compatibility ?? Compatibility.EC2_AND_FARGATE; | ||
this.taskDefinitionArn = props.taskDefinitionArn; | ||
this._taskRole = props.taskRole; | ||
this._networkMode = props.networkMode; | ||
} | ||
|
||
public get networkMode(): NetworkMode { | ||
if (this._networkMode == undefined) { | ||
throw new Error('This operation requires the networkMode in ImportedTaskDefinition to be defined. ' + | ||
'Add the \'networkMode\' in ImportedTaskDefinitionProps to instantiate ImportedTaskDefinition'); | ||
} else { | ||
return this._networkMode; | ||
} | ||
} | ||
|
||
public get taskRole(): IRole { | ||
if (this._taskRole == undefined) { | ||
throw new Error('This operation requires the taskRole in ImportedTaskDefinition to be defined. ' + | ||
'Add the \'taskRole\' in ImportedTaskDefinitionProps to instantiate ImportedTaskDefinition'); | ||
} else { | ||
return this._taskRole; | ||
} | ||
} | ||
|
||
/** | ||
* Return true if the task definition can be run on an EC2 cluster | ||
*/ | ||
public get isEc2Compatible(): boolean { | ||
return isEc2Compatible(this.compatibility); | ||
} | ||
|
||
/** | ||
* Return true if the task definition can be run on a Fargate cluster | ||
*/ | ||
public get isFargateCompatible(): boolean { | ||
return isFargateCompatible(this.compatibility); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can't this whole implementation just defer to
TaskDefinition.fromTaskDefinitionAttributes()
?Oh I guess it couldn't because the types are different...
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 wonder if it might be helpful to define a private class that represents the
Import
class for all 3 types. This would be a file like: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 comment
The 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
fromTaskDefinitionArn
: It does have the return type of interfaceITaskDefinition
, but internally it's aTaskDefinitionBase
(which implementsITaskDefinition
) rather than aResource
like what the other two import methods return. Don't see the downside of unifying them intoResource
for now tho.