In my CDK code there is a low-lavel ecs.CfnTaskDefinition
task definition.
my_task_definition = aws_cdk.ecs.CfnTaskDefinition(
scope=self,
id="my_task_definition",
# rest of the parameters...
)
I want to use this task definition to a create a Ecs service, like this.
my_service = aws_cdk.ecs.Ec2Service(
scope=self,
id="my_service",
cluster=my_cluster,
task_definition=my_task_definition, # NOT COMPATIBLE
desired_count=1,
# rest of the parameters..
)
But as the task_definition
argument of Ec2Service
should be an instance of aws_cdk.aws_ecs.TaskDefinition
; it's not possible to use my_task_definition
here, which is instance of aws_cdk.aws_ecs.CfnTaskDefinition
.
So the question is it possible to get aws_cdk.aws_ecs.TaskDefinition
object from aws_cdk.aws_ecs.CfnTaskDefinition
instance?
CodePudding user response:
Is it possible to get aws_cdk.aws_ecs.TaskDefinition object from aws_cdk.aws_ecs.CfnTaskDefinition instance?
❌ No. You cannot get a L2 Something
construct from a L1 CfnSomething
. You can get a L2 ISomething
interface construct with from_task_definition_arn (see below). But the task_definition
prop won't accept the interface type.
✅ In your case, start with a L2 TaskDefinition
or ECSTaskDefinition
construct. Then, if you need to muck about with the L1 attributes, use escape hatch syntax to modify its underlying CfnTaskDefinition
.
How to get L2 construct instance from L1 (CFN)?
More generally, yes, there are ways to get a L2 ISomething
from a L1 CfnSomething
:
The CDK has "unescape hatch" syntax for this, but only for S3 Buckets and KMS keys, I believe.
cfnBucket = CfnBucket(stack, 'CfnBucket')
bucket: IBucket = Bucket.from_cfn_bucket(cfn_bucket=cfnBucket)
For other constructs, the from_something_arn
reference methods achieve the same result.