Repository: localstack/localstack

Issue:
ApiGateway, terraform issue 
Hi, I'm trying to set up the ApiGateway Lambda Authorizer but it looks like it doesn't work yet

Terraform code: 
```
resource "aws_api_gateway_authorizer" "demo" {
  name                   = "demo"
  rest_api_id            = "${aws_api_gateway_rest_api.demo.id}"
  authorizer_uri         = "${aws_lambda_function.authorizer.invoke_arn}"
  authorizer_credentials = "${aws_iam_role.invocation_role.arn}"
  type = "TOKEN"
}

resource "aws_api_gateway_rest_api" "demo" {
  name = "auth-demo"
}

resource "aws_iam_role" "invocation_role" {
  name = "api_gateway_auth_invocation"
  path = "/"

  assume_role_policy = <<EOF
...
EOF
}

resource "aws_iam_role_policy" "invocation_policy" {
  name = "default"
  role = "${aws_iam_role.invocation_role.id}"

  policy = <<EOF
...
EOF
}

resource "aws_iam_role" "lambda" {
  name = "demo-lambda"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
...
}

resource "aws_lambda_function" "authorizer" {
  filename      = "${var.lambda_auth}"
  function_name = "api_gateway_authorizer"
  role          = "${aws_iam_role.lambda.arn}"
  handler       = "src/script.lambda_handler"
  runtime       = "python3.7"
  source_code_hash = "${filebase64sha256("${var.lambda_auth}")}"
}
```


I'm getting this on the debug logs:

```
Traceback (most recent call last):
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/werkzeug/serving.py", line 304, in run_wsgi
    execute(self.server.app)
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/werkzeug/serving.py", line 292, in execute
    application_iter = app(environ, start_response)
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/moto/server.py", line 132, in __call__
    return backend_app(environ, start_response)
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/opt/code/localstack/.venv/lib/python3.7/site-packages/moto/core/utils.py", line 142, in __call__
    if len(result) == 3:
TypeError: object of type 'NoneType' has no len()
```

And sometimes this in terraform

`Error: error waiting for Lambda Function (api_gateway_authorizer) creation: unexpected state '', wanted target 'Active'. last error: %!s(<nil>)`

Is this feature available yet ? Thank you


Discussion:
Additional info: Here are the terraform debug logs

```2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: ---[ REQUEST POST-SIGN ]-----------------------------
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: PATCH /restapis/1lqef918vp HTTP/1.1
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Host: localhost:4567
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: User-Agent: aws-sdk-go/1.25.42 (go1.13.4; darwin; amd64) APN/1.0 HashiCorp/1.0 Terraform/0.12.13 (+https://www.terraform.io)
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Content-Length: 78
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Accept: application/json
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Authorization: AWS4-HMAC-SHA256 Credential=mock_access_key/20191202/eu-west-1/apigateway/aws4_request, SignedHeaders=accept;content-length;content-type;host;x-amz-date, Signature=dd388c632331711039f21611b94ffeb7fcef14f6e4316e20a66aa7317482656c
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Content-Type: application/json
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: X-Amz-Date: 20191202T165848Z
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Accept-Encoding: gzip
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4:
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: {"patchOperations":[{"op":"replace","path":"/apiKeySource","value":"HEADER"}]}
2019-12-02T17:58:48.787+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: -----------------------------------------------------
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: 2019/12/02 17:58:48 [DEBUG] [aws-sdk-go] DEBUG: Response apigateway/UpdateRestApi Details:
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: ---[ RESPONSE ]--------------------------------------
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: HTTP/1.1 500 Internal Server Error
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Content-Length: 290
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Access-Control-Allow-Headers: authorization,content-type,content-md5,cache-control,x-amz-content-sha256,x-amz-date,x-amz-security-token,x-amz-user-agent,x-amz-target,x-amz-acl,x-amz-version-id,x-localstack-target,x-amz-tagging
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Access-Control-Allow-Methods: HEAD,GET,PUT,POST,DELETE,OPTIONS,PATCH
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Access-Control-Allow-Origin: *
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Access-Control-Expose-Headers: x-amz-version-id
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Content-Type: text/html
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Date: Mon, 02 Dec 2019 16:58:48 GMT
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: Server: BaseHTTP/0.6 Python/3.7.5
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4:
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4:
2019-12-02T17:58:48.802+0100 [DEBUG] plugin.terraform-provider-aws_v2.40.0_x4: -----------------------------------------------------``` 



Looks like the issue is due to terraform adding api_key_source to the ApiGateway on each re-run 

Resolving patch:
diff --git a/localstack/services/awslambda/lambda_api.py b/localstack/services/awslambda/lambda_api.py
--- a/localstack/services/awslambda/lambda_api.py
+++ b/localstack/services/awslambda/lambda_api.py
@@ -641,7 +641,8 @@ def format_func_details(func_details, version=None, always_add_version=False):
         'MemorySize': func_details.memory_size,
         'LastModified': func_details.last_modified,
         'TracingConfig': {'Mode': 'PassThrough'},
-        'RevisionId': func_version.get('RevisionId')
+        'RevisionId': func_version.get('RevisionId'),
+        'State': 'Active'
     }
     if func_details.envvars:
         result['Environment'] = {


Test patch:
diff --git a/tests/unit/test_lambda.py b/tests/unit/test_lambda.py
--- a/tests/unit/test_lambda.py
+++ b/tests/unit/test_lambda.py
@@ -158,6 +158,7 @@ def test_publish_function_version(self):
             expected_result['LastModified'] = self.LAST_MODIFIED
             expected_result['TracingConfig'] = self.TRACING_CONFIG
             expected_result['Version'] = '1'
+            expected_result['State'] = 'Active'
             expected_result2 = dict(expected_result)
             expected_result2['FunctionArn'] = str(lambda_api.func_arn(self.FUNCTION_NAME)) + ':2'
             expected_result2['Version'] = '2'
@@ -196,6 +197,7 @@ def test_list_function_versions(self):
             latest_version['LastModified'] = self.LAST_MODIFIED
             latest_version['TracingConfig'] = self.TRACING_CONFIG
             latest_version['Version'] = '$LATEST'
+            latest_version['State'] = 'Active'
             version1 = dict(latest_version)
             version1['FunctionArn'] = str(lambda_api.func_arn(self.FUNCTION_NAME)) + ':1'
             version1['Version'] = '1'