version: 2.1
jobs:
  deviceapi_build:
    docker:
    - image: circleci/node:latest
    steps:
    - checkout
    - run:
        name: Build
        command: |
          git config --global user.email "test@gmail.com"
          git config --global user.name "Your Name"
          npm version 1.0.${CIRCLE_BUILD_NUM}
          npm publish
  lambda_build:
    docker:
    - image: circleci/node:latest
    steps:
    - checkout
    - run:
        name: Build
        command: |
          npm --prefix ./lambda_function install ./lambda_function
          zip -r /tmp/lambdaFunc.zip ./lambda_function/
    - save_cache:
        key: npm-dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
        paths:
        - /tmp/lambdaFunc.zip


  lambda_deploy:
    docker:
    - image: hashicorp/terraform
    steps:
    - checkout
    - restore_cache:
        key: npm-dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
    - run:
        name: Deploy
        command: |
          terraform init terraform
          terraform apply -auto-approve terraform



workflows:
  version: 2
  build_and_deploy:
    jobs:
    - deviceapi_build
    - lambda_build
    - lambda_deploy:
        requires:
          - lambda_build
        context: org-global