amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

CloudFormation stack lambda resolver template bug

Open dbelovol opened this issue 5 years ago • 4 comments

Before opening, please confirm:

  • [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
  • [X] I have searched for duplicate or closed issues.
  • [X] I have read the guide for submitting bug reports.
  • [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

How did you install the Amplify CLI?

sudo npm install -g @aws-amplify/cli

If applicable, what version of Node.js are you using?

15.13.03

Amplify CLI Version

4.46.1

What operating system are you using?

openSUSE Leap 15.1

Amplify Categories

auth, function, api, hosting

Amplify Commands

add, push, update

Describe the bug

I try to set up simple lambda resolver, returning "Hello world" (without payload). Here stated, that to reference appropriate function I should decorate filed in schema like this

   type Query {
  echo: String @function(name: "echofunction-${env}")
}

After deployment with aplify push I get not ${env} construction substituted by env value, instead I have this { "Version": "2012-10-17", "Statement": [ { "Action": [ "lambda:InvokeFunction" ], "Resource": "arn:aws:lambda:eu-central-1:xxxxxxxxxxx:function:reactampifiedhello-{$env}", "Effect": "Allow" } ] } in AppSync policy and this arn:aws:lambda:eu-central-1:xxxxxxxxxxxxx:function:reactampifiedhello-{$env} in AppSync data sources When I look into backend build directory, I see next construction in LambdaFunction nested Stack

"Policies": [
                    {
                        "PolicyName": "InvokeLambdaFunction",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": [
                                        "lambda:InvokeFunction"
                                    ],
                                    "Resource": {
                                        "Fn::If": [
                                            "HasEnvironmentParameter",
                                            {
                                                "Fn::Sub": [
                                                    "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:reactampifiedhello-{$env}",
                                                    {}
                                                ]
                                            },
                                            {
                                                "Fn::Sub": [
                                                    "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:reactampifiedhello-{$env}",
                                                    {}
                                                ]
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                ]

and it's obvious, that env can't be substituted

Expected behavior

I expect, that ${env} should be substituted by its value in AppSync datasource tables and appsync's lambda invoking policies

Reproduction steps

  1. Add lambda function by amplify function add
  2. Insert next code into NodeJs handler
exports.handler = async (event) => {
    const response = "Hello world!"
    return response;
};
  1. Give all api permissions to this function
  2. Insert into schema.graphql this
   type Query {
  echo: String @function(name: "echofunction-${env}")
}
  1. Update api by amplify update api
  2. Push by amplify push
  3. Query from frontend like so
    const lambdaData = await API.graphql(Object.assign(graphqlOperation(echo),{authMode: ath_mode}))
    
  4. Get error in browser console
{"data":null,"errors":[{"path":["echo"],"data":null,"errorType":"Lambda:LambdaException","errorInfo":null,"locations":[{"line":2,"column":3,"sourceName":null}],"message":"1 validation error detected: Value 'arn:aws:lambda:eu-central-1:561211087447:function:reactampifiedhello-{$env}' at 'functionName' failed to satisfy constraint: Member must satisfy regular expression pattern: (arn:(aws[a-zA-Z-]*)?:lambda:)?([a-z]{2}((-gov)|(-iso(b?)))?-[a-z]+-\\d{1}:)?(\\d{12}:)?(function:)?([a-zA-Z0-9-_\\.]+)(:(\\$LATEST|[a-zA-Z0-9-_]+))? (Service: Lambda, Status Code: 400, Request ID: 21fe17fd-afae-4628-82eb-652b067fe9a8, Extended Request ID: null)"}]}

GraphQL schema(s)

# Put schemas below this line
type Query {
  echo: String! @function(name: "reactampifiedhello-{$env}")
}

Log output

# Put your logs below this line


dbelovol avatar Apr 01 '21 17:04 dbelovol

Important additional info. I've manually fixed issue in AWS Console

  1. I've edited AppSync Lambda inviocing policy and set "Resource": "arn:aws:lambda:eu-central-1:xxxxxxxxxxx:function:reactampifiedhello-dev" instead of "Resource": "arn:aws:lambda:eu-central-1:xxxxxxxxxxx:function:reactampifiedhello-{$env}"
  2. I've edited AppSync data sources table and set resource for lambda "Resource": "arn:aws:lambda:eu-central-1:xxxxxxxxxxx:function:reactampifiedhello-dev" instead of "Resource": "arn:aws:lambda:eu-central-1:xxxxxxxxxxx:function:reactampifiedhello-{$env}"

And after that everything gets Ок. PS By the way, when I add new function by amplify function add, it suggests some automatically generated name, which is 21 letter long. I've changed it with some different name, that is also 21 letter long. Then amplify later has generated name for AppSync lambda data source, it exceeded 32 letter long limit for for that name. So when I later attempted to change wrong resource arn (look p.2 before), I've not succeeded. So there is bug in data source autogeneration resource name also.

dbelovol avatar Apr 01 '21 18:04 dbelovol

@dbelovol In the GraphQL schema details section you listed the directive as @function(name: "reactampifiedhello-{$env}") instead of @function(name: "reactampifiedhello-${env}"). Can you confirm that you are using the @function(name: "reactampifiedhello-${env}") syntax?

edwardfoyle avatar Apr 01 '21 20:04 edwardfoyle

Oh, gosh!! Thank you.

dbelovol avatar Apr 01 '21 21:04 dbelovol

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.

github-actions[bot] avatar May 24 '21 21:05 github-actions[bot]