-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(events): imported ECS Task Definition cannot be used as target (#…
…13293) ### Note Fixes #12811 This change allows creating `EcsTask` from an imported task definition. It does so by changing the `taskDefinition` type in `EcsTaskProps` from concrete class `TaskDefinition` to interface `ITaskDefinition`. ### Testing `buildup` aws-events-targets ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
9 changed files
with
774 additions
and
49 deletions.
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.