Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DevOps Lifecycle
  • DevOps Roadmap
  • Docker Tutorial
  • Kubernetes Tutorials
  • Amazon Web Services [AWS] Tutorial
  • AZURE Tutorials
  • GCP Tutorials
  • Docker Cheat sheet
  • Kubernetes cheat sheet
  • AWS interview questions
  • Docker Interview Questions
  • Ansible Interview Questions
  • Jenkins Interview Questions
Open In App
Next Article:
How To Set Environment Variables In Jenkins ?
Next article icon

How to Set Environment Variable in AWS Lambda ?

Last Updated : 24 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

AWS Lambda is a managed serverless deployment service of AWS that can be used for the implementation of various applications in the cloud. AWS lambda may use various environment variables for the execution of different operations. eg. Database Credentials, API Keys .etc. There are various ways we can set these variables in AWS lambda. let's understand how we can set environment variables in AWS lambda.

What is AWS Lambda?

AWS Lambda Functions are serverless components that execute code only when the particular trigger is invoked. the trigger for functions has to be configured as required. various triggers such as HTTP, S3 object upload, message queues are supported by AWS Lambda. AWS Lambda reduces the cost of idle infrastructure.

Understanding Of Primary Terminologies

  • AWS Lambda Function: It is a serverless compute component that allows the execution of code only when invoked without the provision of servers.
  • Function Trigger: It is an event that invokes a lambda function and executes it. E.g. HTTP Trigger, Message queues etc.
  • Lambda handler: It is a function that accepts events that invoke the lambda function and executes code mentioned in the function.
  • Environment Variables : These are properties that are used by the lambda function to perform one or more tasks in the function.

Setting Environment Variables in AWS Lambda

1. Setting AWS Lambda Environment Variables via Management Console.

  • Create and set a lambda function with the preferred configuration. Once the function is created go to the overview page of the function.
  • On this page scroll to view function options like below.
  • Click on the configuration tab and from the left pane select environment variables. Click on add.
  • Specify key and value for the environment variables and click save. The environment variables will be set for the function.
Provide Configurations In Environment Variables

2. Configuring AWS Lambda Environment Variables Using AWS CLI.

  • If you are using AWS CLI then we can easily set environment variables using below command.
  • Once the function is created you can update the function with environment variables using below command.
aws lambda update-function-configuration --function-name <NAME-OF-FUNCTION> --environment Variables={KEY1=VALUE1,KEY2=VALUE2}
  • In the above command specify the function name in place of <NAME-OF-FUNCTION>.
  • Add your environment variables as key=value in the end. Once done run the command the function will be updated with the environment variables.

3. Configuring AWS Lambda Environment Variables Using IAAC script.

  • For this article lets see how we can add environment variables in terraform script for creating lambda function.
  • In the terraform file add environment variables as below after the configuration of lambda function.
environment {
variables = {
KEY1 = "VALUE1"
KEY2 = "VALUE2"
}
}
  • The entire script for lambda function will look like below
resource "aws_lambda_function" <RESOURCE_NAME> {
function_name = <YOUR_LAMBDA_FUNCTION_NAME>
handler = "lambda_function.handler"
runtime = "nodejs14.x"
role = aws_iam_role.iam_for_lambda.arn

environment {
variables = {
KEY1 = "VALUE1"
KEY2 = "VALUE2"
}
}
}
  • Replace Resource name and function name in above script along with environment variables specified as json below.
  • Once done Run the script and function will be created.
terraform apply
  • The environment variables will also be added to the function.

Conclusion

Thus, we have seen different ways to set environment variables in Lambda . Using AWS console is the most common way to setup environment variables in lambda. For Infrastructure as a Code setting variables in IAAC code script is the best option.


Next Article
How To Set Environment Variables In Jenkins ?

D

deepcodr
Improve
Article Tags :
  • Amazon Web Services
  • DevOps

Similar Reads

  • How to add environment variable in MacOS?
    While programmers work on different Programming Languages, they use to declare Variables to work on any certain piece of code. The Variables in general terms are used to store some values that can be again accessed in the future. There are some Operating System Variables present that are known as En
    3 min read
  • How To Set Environment Variables In Jenkins ?
    Jenkins is a widely used open-source automation server that facilitates continuous integration and continuous delivery (CI/CD) processes. One powerful feature of Jenkins is its capability to configure environment variables, which are important for various functions like defining parameters, paths, a
    6 min read
  • How to Read Environment Variables in Scala?
    Scala stands for scalable language. It is an object-oriented language that provides support for functional programming approach as well. Everything in scala is an object e.g. - values like 1,2 can invoke functions like toString(). Scala is a statically typed language although unlike other statically
    3 min read
  • Edit and set environment variables in Postman
    In Postman, the "environment" refers to a set of key-value pairs or a collection of variables used to customize requests. Environments allow you to define variables that can be reused across multiple requests, making it easier to manage different configurations for testing APIs or web services. Feat
    2 min read
  • Environment Variables in Java
    In Java, Environment variables are widely used by operating systems to deliver configuration data to applications. Environment variables are key/value pairs with both the key and the value being strings, similar to properties in the Java platform. What are Environment Variables in Java?In Java, Envi
    5 min read
  • How To Configure And Use Environment Variables in NestJS?
    Environment variables are an important part of application development, allowing developers to configure applications in different environments (development, staging, production) without hardcoding sensitive or environment-specific information into the application code. In this article, we'll walk t
    2 min read
  • How to Delete an Exported Environment Variable in Shell in Linux
    A variable is the basic building block that stores a value in a specific memory location, and any operation performed on the variable affects that memory location. Values ​​stored in variables can be changed or deleted during program execution. Environment variables are the variables whose value is
    3 min read
  • Next.js Environment Variables
    Environment variables are a fundamental aspect of modern web development, allowing developers to configure applications based on the environment they are running in (development, testing, production, etc.). In Next.js, environment variables provide a flexible and secure way to manage configuration s
    3 min read
  • Next.js Environment Variables
    In this article, we are going to see how to use environment variables in Next.js. Environment variables in Next.js are a way to set configuration values that are used by your application. They can be used to store data such as the name of your company, the port your application will run on, or any o
    2 min read
  • Access environment variable values in Python
    An environment variable is a variable that is created by the Operating System. Environment variables are created in the form of Key-Value pairs. To Access environment variables in Python's we can use the OS module which provides a property called environ that contains environment variables in key-va
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences