'Is it possible to dynamically get graphqlEndpoint in a function cloudformation template?

I am trying to get an AWS Amplify generated graphqlEndpoint into a lambda function in Amplify. Currently I have an environment variable set in the function's cloudformation-template.json. Is there a better way to do this that I missed? Ideally I'd like this to pass dynamically into my amplify functions, and not have to modify each one if there is a change in the graphqlEndpoint.

My current static Environment declaration in {LAMBDA-FN-NAME}-cloudformation-template.json looks like so:

"Environment": {
    "Variables": {
            "ENV": {
              "Ref": "env"
            },
            "REGION": {
              "Ref": "AWS::Region"
            },
            "API_{NAME}_GRAPHQLAPIENDPOINTOUTPUT": "https://{ID}.appsync-api.us-east-1.amazonaws.com/graphql"
    }
}


Solution 1:[1]

I did manage to figure this out. The Amplify CLI provides methods to get the current graphql endpoint as environmental variables inside of Lambda. Following this procedure took care of my problem:

amplify update function
Which setting do you want to update? Resource access permissions
? Select the category api
Api category has a resource called X
? Select the operations you want to permit for X create, read, update, delete

You can access the following resource attributes as environment variables from your Lambda function
    API_X_GRAPHQLAPIENDPOINTOUTPUT
    API_X_GRAPHQLAPIIDOUTPUT
    API_X_GRAPHQLAPIKEYOUTPUT

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gbones