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

Serverless invoke test environment variables undefined #6773

Closed
smartserveteam opened this issue Sep 30, 2019 · 2 comments
Closed

Serverless invoke test environment variables undefined #6773

smartserveteam opened this issue Sep 30, 2019 · 2 comments
Assignees

Comments

@smartserveteam
Copy link

@smartserveteam smartserveteam commented Sep 30, 2019

Bug Report

When using serverless invoke test, environment variables (such as AWS_REGION) are not set.

Description

When using serverless invoke test, environment variables (such as AWS_REGION) are not set.
However, when using serverless offline start, environment variables are set properly.

Running this from my test file: console.log("process.env.AWS_REGION:", process.env.AWS_REGION); returns process.env.AWS_REGION: undefined

Do I need to do anything to initialize env vars?

  1. What's the content of your serverless.yml file?
service: compliance-auth
frameworkVersion: ">=1.1.0 <2.0.0"
custom:
  dynamodb:
    stages:
      - ${self:provider.stage}
    start:
      port: 8000
      inMemory: true
      migrate: true
    migration:
      dir: offline/migrations
provider:
  name: aws
  runtime: nodejs10.x
  region: eu-central-1
  stage: dev
  environment:
    USERS_DYNAMODB_TABLE: ${self:service}-users-${opt:stage, self:provider.stage}
    ORGANIZATIONS_DYNAMODB_TABLE: ${self:service}-organizations-${opt:stage, self:provider.stage}
    AWS_USER_POOL_ID: eu-central-1_0Jxxx8KP8
    AWS_USER_POOL_URL: https://cognito-idp.${self:provider.region}.amazonaws.com/${self:provider.environment.AWS_USER_POOL_ID}
    AWS_USER_POOL_APP_CLIENT_ID: 1qjdqf802d2v3k1ncp1t4vxxxx
    AWS_IDENTITY_POOL_ID: ${self:provider.region}:38c9e7c8-023d-47a0-xxxx-9a7dc0b692b3
    AWS_API_VERSION: 2016-04-19
    AWS_KEYS_URL: ${self:provider.environment.AWS_USER_POOL_URL}/.well-known/jwks.json
  profile: default
  logRetentionInDays: 30
  timeout: 30
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.USERS_DYNAMODB_TABLE}"
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.ORGANIZATIONS_DYNAMODB_TABLE}"
functions:
  login:
    handler: handler.login
    events:
      - http:
          path: login
          method: post
          cors: true
  getOrganizations:
    handler: handler.getOrganizations
    events:
      - http:
          path: organizations
          method: get
          cors: true
  createOrganization:
    handler: handler.createOrganization
    events:
      - http:
          path: organizations
          method: post
          cors: true
  getOrganization:
    handler: handler.getOrganization
    events:
      - http:
          path: organizations/{id}
          method: get
          cors: true
  getUsers:
    handler: handler.getUsers
    events:
      - http:
          path: users
          method: get
          cors: true
  registerUser:
    handler: handler.registerUser
    events:
      - http:
          path: users
          method: post
          cors: true
  confirmRegistrationCode:
    handler: handler.confirmRegistrationCode
    events:
      - http:
          path: codes/confirm
          method: post
          cors: true
  resendRegistrationCode:
    handler: handler.resendRegistrationCode
    events:
      - http:
          path: codes/resend
          method: post
          cors: true
  forgotPassword:
    handler: handler.forgotPassword
    events:
      - http:
          path: users/password/forgot
          method: post
          cors: true
  confirmPassword:
    handler: handler.confirmPassword
    events:
      - http:
          path: users/password/confirm
          method: post
          cors: true
  verifyToken:
    handler: handler.verifyToken
    events:
      - http:
          path: tokens/verify
          method: post
          cors: true
  refreshToken:
    handler: handler.refreshToken
    events:
      - http:
          path: tokens/refresh
          method: post
          cors: true
resources:
  Resources:
    AuthDynamoDbTable:
      Type: "AWS::DynamoDB::Table"
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: "${self:provider.environment.USERS_DYNAMODB_TABLE}"
    OrgDynamoDbTable:
      Type: "AWS::DynamoDB::Table"
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: "${self:provider.environment.ORGANIZATIONS_DYNAMODB_TABLE}"
    CognitoUserPool:
      Type: "AWS::Cognito::UserPool"
      Properties:
        MfaConfiguration: OFF
        UserPoolName: complyhub-user-pool
        UsernameAttributes:
          - email
        Policies:
          PasswordPolicy:
            MinimumLength: 6
            RequireLowercase: False
            RequireNumbers: True
            RequireSymbols: False
            RequireUppercase: True
    CognitoUserPoolClient:
      Type: "AWS::Cognito::UserPoolClient"
      Properties:
        ClientName: complyhub-pool-client
        GenerateSecret: false
        UserPoolId:
          Ref: CognitoUserPool
    ApiGatewayAuthorizer:
      DependsOn:
        - ApiGatewayRestApi
      Type: AWS::ApiGateway::Authorizer
      Properties:
        Name: cognito-authorizer
        IdentitySource: method.request.header.Authorization
        RestApiId:
          Ref: ApiGatewayRestApi
        Type: COGNITO_USER_POOLS
        ProviderARNs:
          - Fn::GetAtt: [CognitoUserPool, Arn]
plugins:
  - serverless-dynamodb-local
  - serverless-offline
  - serverless-mocha-plugin

Similar or dependent issues:

@medikoo medikoo added the question label Oct 1, 2019
@medikoo

This comment has been minimized.

Copy link
Member

@medikoo medikoo commented Oct 1, 2019

@smartserveteam framework doesn't set (and never did) AWS_REGION, although it resolves region internally (and defaults it to us-east1) for all internal AWS requests.

Therefore if you want to have AWS region, exposed atAWS_REGION you need to ensure it in on your own.

@smartserveteam

This comment has been minimized.

Copy link
Author

@smartserveteam smartserveteam commented Oct 3, 2019

Thanks for the clarification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.