douglasnaphas / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
0 stars 0 forks source link

Fix OPTIONS handling #1

Open douglasnaphas opened 4 years ago

douglasnaphas commented 4 years ago

Upstream issue 1434, PRs 1464 and 1649.

TODO

douglasnaphas commented 4 years ago

Reference

  1. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-aws-serverless.html
  2. https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html
  3. https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
  4. https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html
  5. https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-api-corsconfiguration.html
  6. https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-integration.html
douglasnaphas commented 4 years ago

Attempting to build and deploy to CloudFormation

Build

After touch requirements.txt to create a blank requirements.txt (since sam build [options] failed without one) with the virtualenv activated:

(venv) $ sam build --profile doug --template cors-swagger-template.yml --debug
Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
'build' command is called
No Parameters detected in the template
2 resources found in the template
Found Serverless function with name='EchoBase64EventBodyFunction' and CodeUri='.'
Building resource 'EchoBase64EventBodyFunction'
Loading workflow module 'aws_lambda_builders.workflows'
Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', application_framework=None)'
Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
Found workflow 'PythonPipBuilder' to support capabilities 'Capability(language='python', dependency_manager='pip', application_framework=None)'
Running workflow 'PythonPipBuilder'
Running PythonPipBuilder:ResolveDependencies
PythonPipBuilder:ResolveDependencies succeeded
Running PythonPipBuilder:CopySource
PythonPipBuilder:CopySource succeeded

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided

Sending Telemetry: {'metrics': [{'commandRun': {'awsProfileProvided': True, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'duration': 249, 'exitReason': 'success', 'exitCode': 0, 'requestId': '046123c6-dba7-41ac-bfad-a886b8a9737e', 'installationId': 'add6fd49-7d12-45c5-92b8-12c8210d5f59', 'sessionId': 'eb78df53-c504-47c2-abe8-1a501a042f2e', 'executionEnvironment': 'CLI', 'pyversion': '3.7.6', 'samcliVersion': '0.40.0'}}]}
HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)

Deploy

(venv) $ ls -a
.                         ..                        .aws-sam                  cors-swagger-template.yml main.py                   requirements.txt          venv

(venv) $ sam local invoke
Invoking main.echo_base64_event_body (python3.6)

Fetching lambci/lambda:python3.6 Docker container image......
Mounting /Users/dougnaphas/repos/play-sam/cors-swagger/.aws-sam/build/EchoBase64EventBodyFunction as /var/task:ro,delegated inside runtime container
START RequestId: 7fa61199-cea0-1215-c80a-99e4f4d0070e Version: $LATEST
'body': KeyError
Traceback (most recent call last):
  File "/var/task/main.py", line 15, in echo_base64_event_body
    "body": event["body"],
KeyError: 'body'

END RequestId: 7fa61199-cea0-1215-c80a-99e4f4d0070e
REPORT RequestId: 7fa61199-cea0-1215-c80a-99e4f4d0070e  Init Duration: 378.60 ms        Duration: 7.30 ms       Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 25 MB

{"errorType":"KeyError","errorMessage":"'body'","stackTrace":["  File \"/var/task/main.py\", line 15, in echo_base64_event_body\n    \"body\": event[\"body\"],\n"]}

(venv) $ sam deploy --profile doug --guided --template .aws-sam/build/template.yaml

Configuring SAM deploy
======================

        Looking for samconfig.toml :  Not found

        Setting default arguments for 'sam deploy'
        =========================================
        Stack Name [sam-app]: cors-swagger-1434-1
        AWS Region [us-east-1]: us-west-2
        #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
        Confirm changes before deploy [y/N]: y
        #SAM needs permission to be able to create roles to connect to the resources in your template
        Allow SAM CLI IAM role creation [Y/n]: y
        Save arguments to samconfig.toml [Y/n]: y

        Looking for resources needed for deployment: Not found.
        Creating the required resources...
        Successfully created!

                Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-1hbwe4gjysnvb
                A different default S3 bucket can be set in samconfig.toml

        Saved arguments to config file
        Running 'sam deploy' for future deployments will use the parameters saved above.
        The above parameters can be changed by modifying samconfig.toml
        Learn more about samconfig.toml syntax at
        https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html

        Deploying with following values
        ===============================
        Stack name                 : cors-swagger-1434-1
        Region                     : us-west-2
        Confirm changeset          : True
        Deployment s3 bucket       : aws-sam-cli-managed-default-samclisourcebucket-1hbwe4gjysnvb
        Capabilities               : ["CAPABILITY_IAM"]
        Parameter overrides        : {}

Initiating deployment
=====================
Uploading to cors-swagger-1434-1/99dd519189e850f7ac7fb9a3d8c3398b  2861 / 2861.0  (100.00%)
Uploading to cors-swagger-1434-1/9d3a059a86b5bb2fd155b4d2913f32f5.template  1145 / 1145.0  (100.00%)

Waiting for changeset to be created..

CloudFormation stack changeset
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Operation                                                                      LogicalResourceId                                                              ResourceType
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Add                                                                          EchoBase64EventBodyFunctionRole                                                AWS::IAM::Role
+ Add                                                                          EchoBase64EventBodyFunction                                                    AWS::Lambda::Function
+ Add                                                                          MyApiDeployment4bfb617884                                                      AWS::ApiGateway::Deployment
+ Add                                                                          MyApidevStage                                                                  AWS::ApiGateway::Stage
+ Add                                                                          MyApi                                                                          AWS::ApiGateway::RestApi
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Changeset created successfully. arn:aws:cloudformation:us-west-2:403958634573:changeSet/samcli-deploy1580667346/2ab5eae5-3473-490c-9564-7855495b3909

Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y

2020-02-02 13:19:00 - Waiting for stack create/update to complete

CloudFormation events from changeset
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus                                             ResourceType                                               LogicalResourceId                                          ResourceStatusReason
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS                                         AWS::IAM::Role                                             EchoBase64EventBodyFunctionRole                            -
CREATE_IN_PROGRESS                                         AWS::IAM::Role                                             EchoBase64EventBodyFunctionRole                            Resource creation Initiated
CREATE_COMPLETE                                            AWS::IAM::Role                                             EchoBase64EventBodyFunctionRole                            -
CREATE_IN_PROGRESS                                         AWS::Lambda::Function                                      EchoBase64EventBodyFunction                                -
CREATE_COMPLETE                                            AWS::Lambda::Function                                      EchoBase64EventBodyFunction                                -
CREATE_IN_PROGRESS                                         AWS::Lambda::Function                                      EchoBase64EventBodyFunction                                Resource creation Initiated
CREATE_IN_PROGRESS                                         AWS::ApiGateway::RestApi                                   MyApi                                                      -
CREATE_COMPLETE                                            AWS::ApiGateway::RestApi                                   MyApi                                                      -
CREATE_IN_PROGRESS                                         AWS::ApiGateway::RestApi                                   MyApi                                                      Resource creation Initiated
CREATE_IN_PROGRESS                                         AWS::ApiGateway::Deployment                                MyApiDeployment4bfb617884                                  -
CREATE_COMPLETE                                            AWS::ApiGateway::Deployment                                MyApiDeployment4bfb617884                                  -
CREATE_IN_PROGRESS                                         AWS::ApiGateway::Deployment                                MyApiDeployment4bfb617884                                  Resource creation Initiated
CREATE_IN_PROGRESS                                         AWS::ApiGateway::Stage                                     MyApidevStage                                              -
CREATE_IN_PROGRESS                                         AWS::ApiGateway::Stage                                     MyApidevStage                                              Resource creation Initiated
CREATE_COMPLETE                                            AWS::ApiGateway::Stage                                     MyApidevStage                                              -
CREATE_COMPLETE                                            AWS::CloudFormation::Stack                                 cors-swagger-1434-1                                        -
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Successfully created/updated stack - cors-swagger-1434-1 in us-west-2
douglasnaphas commented 4 years ago

I think this will get the integration tests passing:

        if method == "OPTIONS" and (method not in route.methods or cors_headers["Access-Control-Allow-Origin"] == "*"):
Screen Shot 2020-02-05 at 9 50 37 PM
douglasnaphas commented 4 years ago

This is probably a better approach:

method, _ = self.get_request_methods_endpoints(request)
        if method == "OPTIONS" and self.api.cors:
            headers = Headers(cors_headers)
            return self.service_response("", headers, 200)
Screen Shot 2020-02-05 at 9 59 40 PM

I need to see if the PR's unit test passes, and probably add a test for the cors case.

douglasnaphas commented 4 years ago

This says something about how httpMethod must be POST.

douglasnaphas commented 4 years ago

OpenAPI path object rules: https://swagger.io/specification/#pathItemObject.