aws-amplify / amplify-cli

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

Amplify add function not creating env variable for AppSync URL #1620

Closed KoldBrewEd closed 5 years ago

KoldBrewEd commented 5 years ago
$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"
}
eelzinaty commented 5 years ago

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.

KoldBrewEd commented 5 years ago

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

kaustavghosh06 commented 5 years ago

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

KoldBrewEd commented 5 years ago

@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.

kaustavghosh06 commented 5 years ago

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

eelzinaty commented 5 years ago

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.

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.