Repository: aws/chalice

Issue:
misleading error message when queue name in @app.on_sqs_message has invalid chars
When using @app.on_sqs_message, I'm unexpectedly receiving "The provided execution role does not have permissions to call ReceiveMessage on SQS".

After a lengthy investigation I determined the error occurred because I had specified the full queueUrl instead of just the queue name.

Chalice framework should validate the queue name in order to avoid generating this incorrect error message.

Discussion:

Patch:
diff --git a/.changes/next-release/37309442039-enhancement-SQS-77311.json b/.changes/next-release/37309442039-enhancement-SQS-77311.json
new file mode 100644
index 0000000..9201f24
--- /dev/null
+++ b/.changes/next-release/37309442039-enhancement-SQS-77311.json
@@ -0,0 +1,5 @@
+{
+  "type": "enhancement",
+  "category": "SQS",
+  "description": "Validate queue name is used and not queue URL or ARN (#1388)"
+}
diff --git a/chalice/deploy/validate.py b/chalice/deploy/validate.py
index fcc974d..afbfff5 100644
--- a/chalice/deploy/validate.py
+++ b/chalice/deploy/validate.py
@@ -47,6 +47,7 @@ def validate_configuration(config):
     validate_feature_flags(config.chalice_app)
     validate_endpoint_type(config)
     validate_resource_policy(config)
+    validate_sqs_configuration(config.chalice_app)
 
 
 def validate_resource_policy(config):
@@ -231,3 +232,26 @@ def _get_all_function_names(chalice_app):
         yield event.name
     for function in chalice_app.pure_lambda_functions:
         yield function.name
+
+
+def validate_sqs_configuration(chalice_app):
+    # type: (app.Chalice) -> None
+    for event in chalice_app.event_sources:
+        if not isinstance(event, app.SQSEventConfig):
+            continue
+        if not _is_valid_queue_name(event.queue):
+            raise ValueError("The 'queue' parameter for the "
+                             "'@app.on_sqs_message()' handler must be the "
+                             "name of the queue, not the queue URL or the "
+                             "queue ARN.  Invalid value: %s" % event.queue)
+
+
+def _is_valid_queue_name(queue_name):
+    # type: (str) -> bool
+    if queue_name.startswith(('https:', 'arn:')):
+        return False
+    # We're not validating that the queue has only valid chars because SQS
+    # won't let you create a queue with that name in the first place.  We just
+    # want to detect the case where a user puts the queue URL/ARN instead of
+    # the name.
+    return True


Test patch:
diff --git a/tests/unit/deploy/test_validate.py b/tests/unit/deploy/test_validate.py
index 8e9ab08..0ea1466 100644
--- a/tests/unit/deploy/test_validate.py
+++ b/tests/unit/deploy/test_validate.py
@@ -319,3 +319,15 @@ def test_valid_minimum_compression_size(sample_app):
     config = Config.create(chalice_app=sample_app,
                            minimum_compression_size=1)
     assert validate_configuration(config) is None
+
+
+def test_validate_sqs_queue_name(sample_app):
+
+    @sample_app.on_sqs_message(
+        queue='https://sqs.us-west-2.amazonaws.com/12345/myqueue')
+    def handler(event):
+        pass
+
+    config = Config.create(chalice_app=sample_app)
+    with pytest.raises(ValueError):
+        validate_configuration(config)