amazon-archives / aws-amplify-serverless-plugin

Plugin for the Serverless Framework to output AWS Amplify configuration files.
Apache License 2.0
124 stars 30 forks source link

add support for API Gateway #24

Closed rayjanwilson closed 5 years ago

rayjanwilson commented 5 years ago

one of the nice features of AWS Amplify is clean integration with API Gateway

will look something like this for javascript, don't know about the other formats:

{
    "name": "api",
    "endpoint": `https://${config.APIGateway.URL}.execute-api.${config.APIGateway.Region}.amazonaws.com/${this.stage}`,
    "region": `${config.APIGateway.Region}`
}
adrianhall commented 5 years ago

Currently rejecting the PR.

1) We need a native equivalent (awsconfiguration.json) - I'll amend this issue with the format of the REST API 2) We need to be able to handle multiple API Gateway definitions. I'll see what they do with this and similarly amend this issue.

The code presented is a good fix in the interim - just not complete.

rayjanwilson commented 5 years ago

agreed. will incorporate multiple gateways and native

adrianhall commented 5 years ago

Example of multiple gateways within aws-exports.js:

// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.

const awsmobile =  {
    "aws_project_region": "us-east-1",
    "aws_cognito_identity_pool_id": "us-east-1:250bb149-7ce8-4461-b9dc-e1c2202dea84",
    "aws_cognito_region": "us-east-1",
    "aws_user_pools_id": "us-east-1_aTKtKizVq",
    "aws_user_pools_web_client_id": "619kjmfkq4fr8oqrkes0103shs",
    "aws_cloud_logic_custom": [
        {
            "name": "api9b5831db",
            "endpoint": "https://dyq9lnsmt5.execute-api.us-east-1.amazonaws.com/Prod",
            "region": "us-east-1"
        },
        {
            "name": "api6a6fc1cd",
            "endpoint": "https://0744u7fp83.execute-api.us-east-1.amazonaws.com/Prod",
            "region": "us-east-1"
        }
    ]
};
adrianhall commented 5 years ago

ANDROID does not include anything in the awsconfiguration.json file. However, it generates app/src/main/java/{api-id}/{api-name}.java with the following (example contents)

package api2b6b9807;

import java.util.*;

@com.amazonaws.mobileconnectors.apigateway.annotation.Service(endpoint = "https://hb7v2vacp8.execute-api.us-east-1.amazonaws.com/Prod")
public interface FooandroidbbClient {

    /**
     * A generic invoker to invoke any API Gateway endpoint.
     * @param request
     * @return ApiResponse
     */
    com.amazonaws.mobileconnectors.apigateway.ApiResponse execute(com.amazonaws.mobileconnectors.apigateway.ApiRequest request);

    /**
     * 
     * 
     * @return void
     */
    @com.amazonaws.mobileconnectors.apigateway.annotation.Operation(path = "/items", method = "OPTIONS")
    void itemsOptions();

    /**
     * 
     * 
     * @return void
     */
    @com.amazonaws.mobileconnectors.apigateway.annotation.Operation(path = "/items/{proxy+}", method = "OPTIONS")
    void itemsProxyOptions();

}

In addition, there is a model/RequestSchema and model/ResponseSchema Java classes. However, all of this is not required. I'm proposing the following instead of code gen:

{
    "UserAgent": "aws-amplify-cli/0.1.0",
    "Version": "1.0",
    "IdentityManager": {
        "Default": {}
    },
    "CredentialsProvider": {
        "CognitoIdentity": {
            "Default": {
                "PoolId": "us-east-1:1bae8b65-b2b1-4772-8b28-8cbeed06c1bc",
                "Region": "us-east-1"
            }
        }
    },
    "CognitoUserPool": {
        "Default": {
            "PoolId": "us-east-1_O4slbRA0T",
            "AppClientId": "7iglf1k0dcl6j70e78lch08k6",
            "AppClientSecret": "jorrtj9sl0g4vh3ro28p4ulfd8oa4ih4gn9ug3n0fg9ouo5sa1s",
            "Region": "us-east-1"
        }
    },
    "APIGateway": {
        "api9b5831db": {
            "Endpoint": "https://dyq9lnsmt5.execute-api.us-east-1.amazonaws.com/Prod",
            "Region": "us-east-1"
        },
        "api6a6fc1cd": {
            "Endpoint": "https://0744u7fp83.execute-api.us-east-1.amazonaws.com/Prod",
            "Region": "us-east-1"
        }
    }
}

Each endpoint is defined by the friendly name, with the endpoint and region as arguments. This is then made available via the AWSConfiguration object.

Let's place this on hold for right now until I get some concensus on this format for other libraries.

adrianhall commented 5 years ago

For the codegen, we can use the following:

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html#getSdk-property

To obtain the SDK, then unpack into the appropriate directory. Thus, code-gen is totally doable.

I believe this unblocks this item and I will start working on this tomorrow.

adrianhall commented 5 years ago

Released as v1.1.0