0 votes
1 view
in AWS by (19.2k points)

I am using the aws-node template. I want to do something like this:

sls deploy URL='https://postman-echo.com/post'

Where URL is the environment variable. I am trying to pass this env variable to my serverless.yml

provider:

  name: aws

  runtime: nodejs8.10

  stage: dev

  region: us-west-2

  environment:

    URL: ${env:URL}

Then access it in my handler.js

const axios = require('axios');

module.exports.hello = async (event, context) => {

  console.log("Lambda invoked\n")

  await axios.post(

    process.env.URL // Accessing the environment variable.

  ).then(function (response) {

      console.log(`Status: ${response.status}`)

    })

    .catch(function (error) {

      console.log(`Error`)

      console.error(error);

    });

  return {

    statusCode: 200,

    body: JSON.stringify({

      message: 'Function executed successfully!',

      input: event

    }),

  };

};

I suspect that the issue is in how I am passing the environment variable to the program but my research has been to no avail.

1 Answer

0 votes
by (44.6k points)

Pass URL as an environment variable and not as an argument within it.

Do something like this:

$ URL='https://postman-echo.com/post' sls deploy

This will not work:

$ sls deploy URL='https://postman-echo.com/post'

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...