aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

Custom lambda resolver - datasource issue #1459

Closed mdwt closed 5 years ago

mdwt commented 5 years ago

Hi there,

I'm having trouble configuring a custom resolver targeting a Lambda function - it seems the Data Source is not being created correctly although the push completes successfully.

I followed this section of the documentation to add a lambda function with a custom resolver

https://aws-amplify.github.io/docs/cli/graphql?sdk=js#add-a-custom-resolver-that-targets-an-aws-lambda-function

I created a function called lambdaSwitch with amplify function add and then I added the following section to the api/<API>/stacks/CustomResources.json file:

"Resources": {
    "CreateUserDataSourceRole": {
        "Type": "AWS::IAM::Role",
        "Properties": {
            "RoleName": {
                "Fn::Sub": [
                    "CreateUserDataSourceRole-${env}",
                    { "env": { "Ref": "env" } }
                ]
            },
            "AssumeRolePolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Effect": "Allow",
                        "Principal": {
                            "Service": "appsync.amazonaws.com"
                        },
                        "Action": "sts:AssumeRole"
                    }
                ]
            },
            "Policies": [
                {
                    "PolicyName": "InvokeLambdaFunction",
                    "PolicyDocument": {
                        "Version": "2012-10-17",
                        "Statement": [
                            {
                                "Effect": "Allow",
                                "Action": [
                                    "lambda:invokeFunction"
                                ],
                                "Resource": [
                                    {
                                        "Fn::Sub": [
                                            "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function::lambdaSwitch-${env}",
                                            { "env": { "Ref": "env" } }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    },
    "CreateUserDataSource": {
        "Type": "AWS::AppSync::DataSource",
        "Properties": {
            "ApiId": {
                "Ref": "AppSyncApiId"
            },
            "Name": "CreateUserFunction",
            "Type": "AWS_LAMBDA",
            "ServiceRoleArn": {
                "Fn::GetAtt": [
                    "CreateUserDataSourceRole",
                    "Arn"
                ]
            },
            "LambdaConfig": {
                "LambdaFunctionArn": {
                    "Fn::Sub": [
                        "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function::lambdaSwitch-${env}",
                        { "env": { "Ref": "env" } }
                    ]
                }
            }
        }
    },
    "QueryCreateUserResolver": {
        "Type": "AWS::AppSync::Resolver",
        "Properties": {
            "ApiId": {
                "Ref": "AppSyncApiId"
            },
            "DataSourceName": {
                "Fn::GetAtt": [
                    "CreateUserDataSource",
                    "Name"
                ]
            },
            "TypeName": "Mutation",
            "FieldName": "createUser",
            "RequestMappingTemplateS3Location": {
                "Fn::Sub": [
                    "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Mutation.createUser.req.vtl",
                    {
                        "S3DeploymentBucket": {
                            "Ref": "S3DeploymentBucket"
                        },
                        "S3DeploymentRootKey": {
                            "Ref": "S3DeploymentRootKey"
                        }
                    }
                ]
            },
            "ResponseMappingTemplateS3Location": {
                "Fn::Sub": [
                    "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Mutation.createUser.res.vtl",
                    {
                        "S3DeploymentBucket": {
                            "Ref": "S3DeploymentBucket"
                        },
                        "S3DeploymentRootKey": {
                            "Ref": "S3DeploymentRootKey"
                        }
                    }
                ]
            }
        }
    },
    "EmptyResource": {
        "Type": "Custom::EmptyResource",
        "Condition": "AlwaysFalse"
    }
},

The mutation

type Mutation {
  createUser(input: CreateUserInput!): CreateUserResponse
}

The amplify api push finishes successfully but when viewing the Data Source in the AppSync console the required fields (Region, Function, Role) are not populated:

datasource

So the mutation cannot be invoked - it returns this error:

"java.lang.IllegalArgumentException: Invalid ARN relative-id [function::lambdaSwitch-dev], function name component is missing"

Is there something that I am missing in my resources file or process?

Windows 10.0.17763 Build 17763 node v10.15.3 npm 6.4.1 amplify 1.6.8

kaustavghosh06 commented 5 years ago

arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function::lambdaSwitch-${env} format is incorrect. It should be arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:lambdaSwitch-${env} Please replace your double colon after function with a single colon.

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.