Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k 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.4k points)
edited by

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'

Need help mastering AWS Lambda and other AWS skills? Go through AWS Training Page and become an AWS expert! 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...