Skip to content
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

ECS: implement IEventRuleTarget #1370

Closed
eladb opened this issue Dec 17, 2018 · 1 comment · Fixed by #1571
Closed

ECS: implement IEventRuleTarget #1370

eladb opened this issue Dec 17, 2018 · 1 comment · Fixed by #1571
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container feature-request A feature should be added or improved.

Comments

@eladb
Copy link
Contributor

eladb commented Dec 17, 2018

The ECS library needs to be able to be used as an event rule target.

@thomasdeml wrote on Gitter:

I have an ECS task definition and want to schedule my task every 5 minutes. I create a scheduled event

var scheduled_event = new EventRule(this, sdk + '_scheduled_event', { 
  scheduleExpression: "rate(5 minutes)"
});

and then I try to attach the task definition to it. I need an IEventRuleTarget object however. I looked around in the docs, but I have no idea where I could get it from.

@eladb eladb added enhancement @aws-cdk/aws-ecs Related to Amazon Elastic Container labels Dec 17, 2018
@eladb
Copy link
Contributor Author

eladb commented Dec 17, 2018

@thomasdeml this is needed a missing feature in our ECS library, in the meantime, it shouldn't be too hard to define a class that implements IEventRuleTarget for ECS:

Here's a sketch (updated and tested by @thomasdeml):

import cdk = require('@aws-cdk/cdk');
import ecs = require('@aws-cdk/aws-ecs');
import iam = require('@aws-cdk/aws-iam');
import events = require('@aws-cdk/aws-events');

// https://github.com/awslabs/aws-cdk/issues/1370
export class TaskEventRuleTarget implements events.IEventRuleTarget {
  private readonly clusterArn: string;

  constructor(private readonly cluster: ecs.ICluster, private readonly task: ecs.TaskDefinition, private readonly count = 1) {
    // https://github.com/awslabs/aws-cdk/issues/1371
    this.clusterArn = cdk.ArnUtils.fromComponents({
      service: 'ecs',
      resource: 'cluster',
      resourceName: this.cluster.clusterName
    });
  }

  public asEventRuleTarget(_ruleArn: string, _ruleUniqueId: string): events.EventRuleTargetProps {
    let role = this.task.findChild('EventRuleRole') as iam.Role;
    if (!role) {
      role = new iam.Role(this.task, 'EventRuleRole', {
        assumedBy: new iam.ServicePrincipal('cloudwatch.amazonaws.com'),
      });

      // https://github.com/awslabs/aws-cdk/issues/1372
      role.addToPolicy(new iam.PolicyStatement()
        .addAction('ecs:RunTask')
        .addResource(`${this.task.taskDefinitionArn}:*`)
        .addCondition('ArnEquals', {
          'ecs:cluster': this.clusterArn
        }));
    }

    return {
      id: this.task.id,
      arn: this.clusterArn,
      ecsParameters: {
        taskCount: this.count,
        taskDefinitionArn: this.task.taskDefinitionArn
      },
      roleArn: role.roleArn
    };
  }
}

@srchase srchase added feature-request A feature should be added or improved. and removed enhancement labels Jan 3, 2019
@rix0rrr rix0rrr added gap and removed gap labels Jan 4, 2019
rix0rrr added a commit that referenced this issue Jan 18, 2019
EC2 task definitions can now be used as CloudWatch event targets.

ALSO IN THIS COMMIT

* Improve hash calculation of Docker images.
* Add `grantPassRole()` method to iam.Role

Fixes #1370.
rix0rrr added a commit that referenced this issue Jan 18, 2019
EC2 task definitions can now be used as CloudWatch event targets.

ALSO IN THIS COMMIT

* Improve hash calculation of Docker images.
* Add `grantPassRole()` method to iam.Role

Fixes #1370.
rix0rrr added a commit that referenced this issue Jan 18, 2019
EC2 task definitions can now be used as CloudWatch event targets.

ALSO IN THIS COMMIT

* Improve hash calculation of Docker images.
* Add `grantPassRole()` method to iam.Role

Fixes #1370.
rix0rrr added a commit that referenced this issue Feb 4, 2019
EC2 task definitions can now be used as CloudWatch event targets.

ALSO IN THIS COMMIT

* Improve hash calculation of Docker images.
* Add `grantPassRole()` method to iam.Role

Fixes #1370.
moofish32 pushed a commit to moofish32/aws-cdk that referenced this issue Feb 5, 2019
EC2 task definitions can now be used as CloudWatch event targets.

ALSO IN THIS COMMIT

* Improve hash calculation of Docker images.
* Add `grantPassRole()` method to iam.Role

Fixes aws#1370.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container feature-request A feature should be added or improved.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants