aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
88 stars 76 forks source link

Customize StreamingLambda with @searchable #437

Open bokuweb opened 5 years ago

bokuweb commented 5 years ago

Note: If your question is regarding the AWS Amplify Console service, please log it in the official AWS Amplify Console forum

Which Category is your question related to?

AppSync with @searchable

What AWS Services are you utilizing?

appsync elasticsearch service

Provide additional details e.g. code snippets

Hi :) I would like to customize ElasticSearchStreamingLambda?When I add following schema, lambda was created with python code (ElasticSearchStreamingLambdaFunction.zip).

type Test @model @searchable {
  id: ID!
}

So I would like to use nodejs or golang and customize lambda code. Is it possible now?? I can not find better way yet...

Should I edit cloudformation-template.json in the manual?

bokuweb commented 5 years ago

Should I create another function by myself then overwrite ElasticsearchStreamingFunctionName (https://aws-amplify.github.io/docs/cli/graphql#elasticsearchstreamingfunctionname) ?

kaustavghosh06 commented 5 years ago

@bokuweb Yes, you could probably do that as mentioned in https://aws-amplify.github.io/docs/cli/graphql#elasticsearchstreamingfunctionname

bokuweb commented 5 years ago

@kaustavghosh06 Thanks for your quick response :) I'll try it

bokuweb commented 5 years ago

Can I overwrite ElasticSearchStreamingLambdaHandlerName and ElasticSearchStreamingLambdaRuntime too ? It seems that it is not documented.

kaustavghosh06 commented 5 years ago

@bokuweb Were you able to overwrite the parameters?

bokuweb commented 5 years ago

@kaustavghosh06 Sorry, I did not do it yet. I'll try it and write result here :)

kstro21 commented 5 years ago

@bokuweb setting https://aws-amplify.github.io/docs/cli/graphql#elasticsearchstreamingfunctionname will only change the name of the Lambda, but you can not override the code, it will still use the same zip containing the Python code, here is a piece of the template definition

"ElasticSearchStreamingLambdaFunction": {
    "Type": "AWS::Lambda::Function",
    "Properties": {
        "Code": {
            "S3Bucket": {
                "Ref": "S3DeploymentBucket"
            },
            "S3Key": {
                "Fn::Join": [
                    "/",
                    [
                        {
                            "Ref": "S3DeploymentRootKey"
                        },
                        "functions",
                        {
                            "Fn::Join": [
                                ".",
                                [
                                    "ElasticSearchStreamingLambdaFunction",
                                    "zip"
                                ]
                            ]
                        }
                    ]
                ]
            }
        },

Right now, I don't see any doc that references how to use a custom Lambda instead of the auto-generated one.

You can still copy the autogenerated stack SearchableStack.json from the build folder of another project, copy it to the amplify\backend\api\pgaplatform\stacks\ inside yours, then customize the Lambda. It will require to edit a few files in the process, but it will be ideal that amplify support it out of the box.

bokuweb commented 5 years ago

@kstro21 Thanks for your reply :)

will only change the name of the Lambda, but you can not override the code, it will still use the same zip containing the Python code,

Hmmm.It is very disappointing... 😢

You can still copy the autogenerated stack SearchableStack.json from the build folder of another project, copy to the amplify\backend\api\pgaplatform\stacks inside yours, the customize the Lambda. It will require to edit a few files in the process, but it will be ideal that amplify support it out of the box.

Thanks 👍 I'll try and do some investigation.

@kaustavghosh06 @mikeparisstuff Do you have any idea??

mikeparisstuff commented 5 years ago

@bokuweb This is currently not possible although I agree that this is useful and it would not be too difficult to implement. Currently the ElasticSearchStreamingLambdaFunctionName argument changes the name of the function deployed by Amplify but this is only partially useful. We can add a parameter "ElasticSearchStreamingLambdaFunctionArn" parameter that you could use to point to an existing function. Once adding that we need to make a few tweaks to the EventSourceMapping resource here https://github.com/aws-amplify/amplify-cli/blob/767f3a6edce9f8c4109e18afacf648c6d4c7be56/packages/graphql-elasticsearch-transformer/src/resources.ts#L164-L168

I have added this to the backlog and would be happy to review a PR.

bokuweb commented 5 years ago

@mikeparisstuff Thanks for your reply :) What should I do specifically? I will try to fix it if possible.

I have added this to the backlog and would be happy to review a PR. 🎉

kstro21 commented 5 years ago

@mikeparisstuff and @kaustavghosh06 wouldn't be easy to allow for stacks in amplify\backend\api\amplifytest\stacks\ to update stacks with the exact same name in amplify\backend\api\amplifytest\build\stacks\ by doing a deep merge? That will be interesting to see. Just thinking.

ArsSirek commented 1 year ago

I see that this is an old issue, but it's still open and a bit misleading. I contacted AWS support and they pointed out that it is now possible to override the StreamingLambda function code as well

  1. Run “amplify override api” which will create an override.ts file in api// directory.
  2. Edit overrides.ts overriding the streaming function code with the resources.opensearch.OpenSearchStreamingLambdaFunction.code

Something similar to

import { AmplifyApiGraphQlResourceStackTemplate } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyApiGraphQlResourceStackTemplate) {
  resources.opensearch.OpenSearchStreamingLambdaFunction.runtime = 'python3.9';
  resources.opensearch.OpenSearchStreamingLambdaFunction.functionName = 'python_streaming_function';
  resources.opensearch.OpenSearchStreamingLambdaFunction.handler = 'index.lambda_handler';

  resources.opensearch.OpenSearchStreamingLambdaFunction.code = {
    zipFile: `
    ...
 `
    };

not sure if the override handler takes priority over CloudFormationStack parameters, so in the team-provider-info.json you may need to add

"api": {
        "<api_name>": {
          "OpenSearchInstanceType": "t3.small.elasticsearch",
          "OpenSearchStreamingLambdaHandlerName": "index.lambda_handler"
        },