Repository: serverless/serverless (JavaScript)
Date: 2018-02-22 18:58:39+00:00
Task type: Feature

Issue:
Name property not available for cloudwatchEvent


# Add name property to cloudwatchEvent

## Description

Currently if you specify a name property for a cloudwatchEvent, it does not get populated in the CF template. Name is an accepted property for schedule, but not cloudwatchEvent

So this works:
```yaml
- schedule:
          name: my-schedule-name
```
But this does not:
```yaml
- cloudwatchEvent:
          name: my-cloudwatch-event-name
```

* What is the use case that should be solved. The more detail you describe this in the easier it is to understand for us.

We should not have to specify a resource extension to set the name property of the CloudWatch Event, especially since you can currently set the name of a CloudWatch Schedule.

* If there is additional config how would it look

As far as I can tell, the changes to the cloudwatchEvent's index.js would be pretty simple. See [this commit](https://github.com/icj217/serverless/commit/a4de7288f37400722763d1ac3524cee48ce50bdb) on my fork.

## Additional Data

* ***Serverless Framework Version you're using***: 1.23
* ***Operating System***: OSX 
* ***Stack Trace***:
* ***Provider Error messages***:



Issue discussion:

Solution patch:
diff --git a/docs/providers/aws/events/cloudwatch-event.md b/docs/providers/aws/events/cloudwatch-event.md
index 3d75da37476..7e40bbab444 100644
--- a/docs/providers/aws/events/cloudwatch-event.md
+++ b/docs/providers/aws/events/cloudwatch-event.md
@@ -111,3 +111,24 @@ functions:
               state:
                 - pending
 ```
+
+## Specifying a Name
+
+You can also specify a CloudWatch Event name. Keep in mind that the name must begin with a letter; contain only ASCII letters, digits, and hyphens; and not end with a hyphen or contain two consecutive hyphens. More infomation [here](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-name.html).
+
+```yml
+functions:
+  myCloudWatch:
+    handler: myCloudWatch.handler
+    events:
+      - cloudwatchEvent:
+          name: 'my-cloudwatch-event-name'
+          event:
+            source:
+              - "aws.ec2"
+            detail-type:
+              - "EC2 Instance State-change Notification"
+            detail:
+              state:
+                - pending
+```
diff --git a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js
index d8ef7b2e998..2f62e5b0f81 100644
--- a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js
+++ b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.js
@@ -26,6 +26,7 @@ class AwsCompileCloudWatchEventEvents {
             let Input;
             let InputPath;
             let Description;
+            let Name;
 
             if (typeof event.cloudwatchEvent === 'object') {
               if (!event.cloudwatchEvent.event) {
@@ -45,6 +46,7 @@ class AwsCompileCloudWatchEventEvents {
               Input = event.cloudwatchEvent.input;
               InputPath = event.cloudwatchEvent.inputPath;
               Description = event.cloudwatchEvent.description;
+              Name = event.cloudwatchEvent.name;
 
               if (Input && InputPath) {
                 const errorMessage = [
@@ -87,6 +89,7 @@ class AwsCompileCloudWatchEventEvents {
                   "EventPattern": ${EventPattern.replace(/\\n|\\r/g, '')},
                   "State": "${State}",
                   ${Description ? `"Description": "${Description}",` : ''}
+                  ${Name ? `"Name": "${Name}",` : ''}
                   "Targets": [{
                     ${Input ? `"Input": "${Input.replace(/\\n|\\r/g, '')}",` : ''}
                     ${InputPath ? `"InputPath": "${InputPath.replace(/\r?\n/g, '')}",` : ''}


Test patch:
diff --git a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.test.js b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.test.js
index 081bc08c7d3..0dd5123a2d7 100644
--- a/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.test.js
+++ b/lib/plugins/aws/package/compile/events/cloudWatchEvent/index.test.js
@@ -246,6 +246,34 @@ describe('awsCompileCloudWatchEventEvents', () => {
       ).to.equal('test description');
     });
 
+    it('should respect name variable', () => {
+      awsCompileCloudWatchEventEvents.serverless.service.functions = {
+        first: {
+          events: [
+            {
+              cloudwatchEvent: {
+                event: {
+                  source: ['aws.ec2'],
+                  'detail-type': ['EC2 Instance State-change Notification'],
+                  detail: { state: ['pending'] },
+                },
+                enabled: false,
+                input: '{"key":"value"}',
+                name: 'test-event-name',
+              },
+            },
+          ],
+        },
+      };
+
+      awsCompileCloudWatchEventEvents.compileCloudWatchEventEvents();
+
+      expect(awsCompileCloudWatchEventEvents.serverless.service
+        .provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent1
+        .Properties.Name
+      ).to.equal('test-event-name');
+    });
+
     it('should respect input variable as an object', () => {
       awsCompileCloudWatchEventEvents.serverless.service.functions = {
         first: {