Skip to content

feat(ecs): allow specifying whether an imported task definition ARN includes a revision - #38633

Open
lemon0333 wants to merge 1 commit into
aws:mainfrom
lemon0333:feat/imported-taskdef-no-revision
Open

feat(ecs): allow specifying whether an imported task definition ARN includes a revision#38633
lemon0333 wants to merge 1 commit into
aws:mainfrom
lemon0333:feat/imported-taskdef-no-revision

Conversation

@lemon0333

Copy link
Copy Markdown

Issue # (if applicable)

Closes #32485.

Reason for this change

fromFargateTaskDefinitionAttributes / fromEc2TaskDefinitionAttributes / fromTaskDefinitionAttributes cannot record whether an imported task definition ARN is fully-qualified (...:task-definition/family:123) or revision-less (...:task-definition/family).

When an EcsTask EventBridge target scopes down its ecs:RunTask permission (the case from #30390 / #31615), it must grant the exact ARN when a revision is present, or append a :* revision wildcard when it is not. Today that decision is made by string-inspecting the ARN:

if (!cdk.Token.isUnresolved(this.taskDefinition.taskDefinitionArn)) {
  const hasRevision = /:[0-9]+$/.test(this.taskDefinition.taskDefinitionArn);
  needsRevisionWildcard = !hasRevision;
}

This breaks when the ARN is only known at deploy time (e.g. Fn.importValue, or a CloudFormation parameter): the ARN is an unresolved token, so no revision can be inferred, and the :* wildcard is silently omitted even for a revision-less ARN. The maintainer (@ashishdhingra) confirmed this is a valid extension of #30390 / #31615.

Description of changes

  • Added an optional, documented, jsii-friendly taskDefinitionArnIncludesRevision?: boolean to CommonTaskDefinitionAttributes (so TaskDefinitionAttributes, FargateTaskDefinitionAttributes, and Ec2TaskDefinitionAttributes all inherit it) in packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts.
  • Exposed it as an optional readonly property on the ITaskDefinition interface and threaded it through ImportedTaskDefinition and all three from*TaskDefinitionAttributes import methods.
  • Consumed it in createEventRolePolicyStatements() in packages/aws-cdk-lib/aws-events-targets/lib/ecs-task.ts: when the flag is set it authoritatively decides whether the :* wildcard is needed; otherwise it falls back to the existing token/string inspection, so the default behavior is unchanged.

Public API shape

Per my note on the issue, the preferred shape wasn't specified, so I went with a boolean flag (taskDefinitionArnIncludesRevision) rather than a separate no-revision ARN field, because it maps directly onto the single decision the grant logic makes and keeps the import surface minimal. I'm happy to rename it or switch to a separate field if a maintainer prefers a different shape — feedback welcome.

Describe any new or updated permissions being added

No new permissions. This changes how the resource scope of the existing ecs:RunTask permission is computed for the EventBridge EcsTask target: a revision-less imported ARN whose value is a deploy-time token is now correctly scoped with a :* revision wildcard when taskDefinitionArnIncludesRevision: false is supplied.

Description of how you validated changes

  • Unit tests — added 3 tests in aws-events-targets/test/ecs/event-rule-target.test.ts (tokenized ARN + false -> :* wildcard; tokenized ARN + true -> ARN as-is; concrete ARN with explicit override) and 1 test in aws-ecs/test/fargate/fargate-task-definition.test.ts (attribute is recorded / defaults to undefined). Full suites pass: 39/39 (events-targets ecs) and 67/67 (ecs task-definition).
  • Integ test — updated integ.ecs-imported-task-def (which imports a family-only ARN built from region/account tokens) to set taskDefinitionArnIncludesRevision: false, and regenerated the snapshot. The only functional snapshot change is the ecs:RunTask resource going from .../task-definition/TaskDefinitionA to .../task-definition/TaskDefinitionA:*.
  • Buildtsc compiles the affected packages with 0 errors.
  • README — documented the new attribute under "Run an ECS Task" in aws-events-targets/README.md.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

…ncludes a revision

Adds an optional `taskDefinitionArnIncludesRevision` boolean to
`TaskDefinitionAttributes` (and the Fargate/EC2 variants via
`CommonTaskDefinitionAttributes`) and threads it through
`ImportedTaskDefinition` and the `ITaskDefinition` interface.

When an imported task definition ARN is only known at deploy time
(e.g. `Fn.importValue` or a CloudFormation parameter), its revision
cannot be inferred from the string. The EventBridge `EcsTask` target's
`ecs:RunTask` grant logic now uses this flag to decide between the exact
ARN and a `:*` revision wildcard, falling back to the existing string
inspection when the flag is not provided (preserving current behavior).

Closes aws#32485.
@lemon0333
lemon0333 deployed to automation August 22, 2026 07:12 — with GitHub Actions Active
@lemon0333
lemon0333 deployed to automation August 22, 2026 07:12 — with GitHub Actions Active
@github-actions github-actions Bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 labels Aug 22, 2026
@lemon0333
lemon0333 deployed to automation August 22, 2026 07:12 — with GitHub Actions Active
@aws-cdk-automation
aws-cdk-automation requested a review from a team August 22, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(ecs): Allow specifying that an imported TaskDefinition does not have a revision specified

1 participant