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

Amplify add function not creating env variable for AppSync URL

Open KoldBrewEd opened this issue 7 years ago • 7 comments

$amplify --version
1.7.1

Steps:

amplify add api amplify add function Select CRUD permissions to API

Description:

I'm trying to create a function that invokes an AppSync API from the backend however the AppSync URL is not passed as an environment variable. Since this use case demands the use of an HTTP client on lambda, the URL is essential. Refer to the env variables created:

aws lambda get-function-configuration --function-name backendGraphQL-master --region us-east-1
{
    "FunctionName": "backendGraphQL-master",
    "LastModified": "2019-06-07T23:07:43.641+0000",
    "RevisionId": "98d213d1-0a47-440f-9bbb-611eb649cc0f",
    "MemorySize": 128,
    "Environment": {
        "Variables": {
            "REGION": "us-east-1",
            "ENV": "master",
            "API_BACKENDGRAPHQL_GRAPHQLAPIIDOUTPUT": "xxxxxxxxxxxxxxxxx"
        }
    },
    "Version": "$LATEST",
    "Role": "arn:aws:iam::xxxxxxxxxxx:role/backendgraphqlLambdaRolebe936e06-master",
    "Timeout": 25,
    "Runtime": "nodejs8.10",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "CodeSha256": "RfM/bGPeo2CeHFgei6jfy762YkzDF7I84dzv30PnTvM=",
    "Description": "",
    "CodeSize": 7601752,
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:backendGraphQL-master",
    "Handler": "index.handler"
}

KoldBrewEd avatar Jun 07 '19 23:06 KoldBrewEd

There a workaround for this. Go to backend-config.json and under function key find backendGraphQL then under dependsOn => attributes add this value GraphQLAPIEndpointOutput. Repeat this for amplify-meta.json file. This will allow you to access the GraphQL URL in your function CF Stack via apibackendGraphQLGraphQLAPIEndpointOutput. Finally, you can add it to your function environment variables.

eelzinaty avatar Jun 09 '19 10:06 eelzinaty

Thanks, any plans to make it part of the function creation?

KoldBrewEd avatar Jun 10 '19 15:06 KoldBrewEd

@awsed Yes, we’ll add it to the defaults. I’ll update this thread after submitting a PR for the same.

kaustavghosh06 avatar Jun 10 '19 15:06 kaustavghosh06

@eelzinaty if I do the changes you suggested I get this:

UPDATE_ROLLBACK_COMPLETE backend-graphql-master-20190610091720 AWS::CloudFormation::Stack Mon Jun 10 2019 09:28:26 GMT-0700 (Pacific Daylight Time) 
⠋ Updating resources in the cloud. This may take a few minutes...Error updating cloudformation stack
⠋ Updating resources in the cloud. This may take a few minutes...

Following resources failed

✖ An error occurred when pushing the resources to the cloud

Resource is not in the state stackUpdateComplete

If I remove GraphQLAPIEndpointOutput the stack is deployed.

KoldBrewEd avatar Jun 10 '19 16:06 KoldBrewEd

@awsed Added this is a part of PR - #1641 This enhancement would be present in the next version of the CLI.

kaustavghosh06 avatar Jun 10 '19 22:06 kaustavghosh06

I think the problem is related to the parameter name if you show me the error that causes the stack failure I could help to resolve that.

eelzinaty avatar Jun 11 '19 07:06 eelzinaty

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 27 '21 08:05 github-actions[bot]