Search code examples
c#.net-coreaws-lambdaaws-api-gatewayaws-amplify

Access Lambda environment variables from .env file


I am running .net core 3.1 AWS Lambda function but I am unable to access the environment variable from the AWS Lambda Console in c# using:

var envVariable = Environment.GetEnvironmentVariable("myVariableName");

The environment variables are stored in a .env file in the root of my lambda function. Does the .env file need to be copied into the build folder for it to be accessible in the lambda function?. How can I access the AWS Lambda environment variable from the .env file?

Application Structure

Application Structure

cloudformation-template.json refers to .env file.

enter image description here

update: 5/31/2020:

I removed the .env file and added the variables in the cloudformation-template.json directly as individual variables. When I try to push the same to aws using amplify publish command, I get a

Resource is not in the state stackUpdateComplete
An error occured during the push operation: Resource is not in the state stackUpdateComplete error

Solution

  • I am able to solve this issue by adding environment variables directly to the cloudformation-template.json and running the amplify publish command to push to aws.

    "Environment": {
                        "Variables": {                      
                            "VUE_APP_STRIPE_PUBLISHABLE_KEY": "<someRandomString>",
                            "VUE_APP_STRIPE_SECRET_KEY": "<someRandomString>"
                        }
                    },