Repository: Miserlou/Zappa

Issue:
AWS_IAM authentication
I have an API I'd like to put behind AWS_IAM authentication. I see that `apigateway.put_method` is always called with `authorizationType='none'`. If there's no in-flight change to add support for IAM authentication, I'll submit a PR.



Discussion:
Right now the closes thing we have is API-key secured endpoints, IAM would be a welcome addition. 

https://github.com/Miserlou/Zappa#enabling-secure-endpoints-on-api-gateway


Resolving patch:
diff --git a/zappa/cli.py b/zappa/cli.py
--- a/zappa/cli.py
+++ b/zappa/cli.py
@@ -166,6 +166,7 @@ def handle(self, argv=None):
                 self.app_function = vargs['app_function']
 
             self.api_key_required = self.stage_config.get('api_key_required', False)
+            self.authorization_type = self.stage_config.get('authorization_type', 'NONE')
 
         # from letsencrypt import get_cert_and_update_domain
         # get_cert_and_update_domain(
@@ -272,7 +273,12 @@ def deploy(self):
         if self.use_apigateway:
             # Create and configure the API Gateway
             api_id = self.zappa.create_api_gateway_routes(
-                self.lambda_arn, self.lambda_name, self.api_key_required, self.integration_content_type_aliases)
+                self.lambda_arn,
+                self.lambda_name,
+                self.api_key_required,
+                self.integration_content_type_aliases,
+                self.authorization_type,
+            )
 
             # Deploy the API!
             cache_cluster_enabled = self.stage_config.get('cache_cluster_enabled', False)
diff --git a/zappa/zappa.py b/zappa/zappa.py
--- a/zappa/zappa.py
+++ b/zappa/zappa.py
@@ -614,7 +614,7 @@ def delete_lambda_function(self, function_name):
     ##
 
     def create_api_gateway_routes(self, lambda_arn, api_name=None, api_key_required=False,
-                                  integration_content_type_aliases=None):
+                                  integration_content_type_aliases=None, authorization_type='NONE'):
         """
         Creates the API Gateway for this Zappa deployment.
 
@@ -658,7 +658,7 @@ def create_api_gateway_routes(self, lambda_arn, api_name=None, api_key_required=
         if not root_id: # pragma: no cover
             return False
         self.create_and_setup_methods(api_id, root_id, lambda_arn, progress.update, api_key_required,
-                                      integration_content_type_aliases)
+                                      integration_content_type_aliases, authorization_type)
 
         parent_id = root_id
         for i in range(1, self.parameter_depth):
@@ -672,12 +672,12 @@ def create_api_gateway_routes(self, lambda_arn, api_name=None, api_key_required=
             parent_id = resource_id
 
             self.create_and_setup_methods(api_id, resource_id, lambda_arn, progress.update, api_key_required,
-                                          integration_content_type_aliases) # pragma: no cover
+                                          integration_content_type_aliases, authorization_type) # pragma: no cover
 
         return api_id
 
     def create_and_setup_methods(self, api_id, resource_id, lambda_arn, report_progress, api_key_required,
-                                 integration_content_type_aliases):
+                                 integration_content_type_aliases, authorization_type):
         """
         Sets up the methods, integration responses and method responses for a given API Gateway resource.
 
@@ -686,11 +686,11 @@ def create_and_setup_methods(self, api_id, resource_id, lambda_arn, report_progr
         """
         for method in self.http_methods:
             response = self.apigateway_client.put_method(
-                    restApiId=api_id,
-                    resourceId=resource_id,
-                    httpMethod=method,
-                    authorizationType='none',
-                    apiKeyRequired=api_key_required
+                restApiId=api_id,
+                resourceId=resource_id,
+                httpMethod=method,
+                authorizationType=authorization_type,
+                apiKeyRequired=api_key_required
             )
             report_progress()
 


Test patch:
diff --git a/tests/tests.py b/tests/tests.py
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -454,6 +454,7 @@ def test_cli_aws(self, session):
         zappa_cli = ZappaCLI()
         zappa_cli.api_stage = 'ttt888'
         zappa_cli.api_key_required = True
+        zappa_cli.authorization_type = 'NONE'
         zappa_cli.load_settings('test_settings.json', session)
         zappa_cli.zappa.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'
         zappa_cli.deploy()
@@ -464,14 +465,13 @@ def test_cli_aws(self, session):
         zappa_cli.unschedule()
         zappa_cli.undeploy(noconfirm=True, remove_logs=True)
 
-
     @placebo_session
     def test_cli_aws_status(self, session):
         zappa_cli = ZappaCLI()
         zappa_cli.api_stage = 'ttt888'
         zappa_cli.load_settings('test_settings.json', session)
         zappa_cli.api_stage = 'devor'
-        zappa_cli.lambda_name = 'baby-flask-devor'        
+        zappa_cli.lambda_name = 'baby-flask-devor'
         zappa_cli.zappa.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution'
         resp = zappa_cli.status()