Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global variables are undefined when running local invoke #380

Closed
alnutile opened this issue May 6, 2018 · 6 comments
Closed

Global variables are undefined when running local invoke #380

alnutile opened this issue May 6, 2018 · 6 comments

Comments

@alnutile
Copy link

alnutile commented May 6, 2018

Resources:
  GithubPushes:
    Type: AWS::Serverless::Function
    DependsOn:
      - Stream
    Properties:
      Runtime: nodejs8.10
      Description: >-
        This will verify that the incoming data is from Github and hashed with the key
        we are using for this environment
      Environment:
        Variables:
          SECRET: !Sub ${Secret}

works fine when I run

SECRET=foo sam local invoke "GithubPushes" -e fixtures/data.json

but if I move that to Globals then this becomes "undefined"

@sanathkr
Copy link
Contributor

sanathkr commented May 8, 2018

This is solved in the latest release - https://github.com/awslabs/aws-sam-cli/releases/tag/v0.3.0.

@sanathkr sanathkr closed this as completed May 8, 2018
@fairct
Copy link

fairct commented Oct 24, 2018

Possibly a regression - but this is not working for me on 0.6.1.

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'

Globals:
  Function:
    Runtime: nodejs8.10
    Environment:
      Variables:
        subVariable: !Sub "myVariable is now ${myVariableValue}"
        refVariable: !Ref myVariableValue

Parameters:
  myVariableValue:
    Type: String
    Default: blue

Resources:
  LambdaDef:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      CodeUri: .
exports.handler = function(event, context, callback) {
  console.log("Sub value: " + process.env.myVariable);
  console.log("Ref value: " + process.env.refVariable);
}
$ echo "{\"stuff\": \"value\"}" | sam local invoke -t template.yml
2018-10-24 10:13:16 Reading invoke payload from stdin (you can also pass it from file with --event)
2018-10-24 10:13:16 Invoking index.handler (nodejs8.10)

Fetching lambci/lambda:nodejs8.10 Docker container image......
2018-10-24 10:13:17 Mounting /Users/cfair as /var/task:ro inside runtime container
START RequestId: 0378e10b-b6de-1ec8-e525-8d6806261cd1 Version: $LATEST
2018-10-24T14:13:18.151Z	0378e10b-b6de-1ec8-e525-8d6806261cd1	Sub value: undefined
2018-10-24T14:13:18.152Z	0378e10b-b6de-1ec8-e525-8d6806261cd1	Ref value: blue
END RequestId: 0378e10b-b6de-1ec8-e525-8d6806261cd1
REPORT RequestId: 0378e10b-b6de-1ec8-e525-8d6806261cd1	Duration: 6.00 ms	Billed Duration: 100 ms	Memory Size: 128 MB	Max Memory Used: 31 MB

@jfuss
Copy link
Contributor

jfuss commented Oct 24, 2018

@fairct What is the regression?

In 0.6.1, we added support for resolving Parameter Refs (not Sub). You can see that refVariable was included in the environment variables. For Environment Variables using other intrinsics, you will need to use the --env-vars Option, just like older versions.

If you still think there is a regression here, please open a new issue and describe what the difference is so we can look deeper into it.

@fairct
Copy link

fairct commented Oct 24, 2018

I see now. I didn't notice that an env var was set in the original issue that was raised here - so thought that a bug came back. Would it be reasonable to open a feature request for this? I haven't dug into the codebase much at all to determine if it's something within my skillset to PR.

@jfuss
Copy link
Contributor

jfuss commented Oct 24, 2018

@fairct For Sub intrinsic support? We have a couple open issues for different Intrinsic support, #528 is one.

Taking a look at the PR for supporting Ref, might be a good place to start. Happy to help in any way, if you are interesting in taking on Sub support.

@ktersius
Copy link

For ref. https://whatibroke.com/2019/01/15/overriding-global-variables-aws-sam-local/

qingchm added a commit to qingchm/aws-sam-cli that referenced this issue Sep 17, 2021
* Sync flow for Step Functions

* Added generator mapping for step functions resources

* Improve documentation and logging

* Improve logging

* Updated loading mechanism and tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants