1 00:00:00,290 --> 00:00:01,470 ‫Okay, so let's practice 2 00:00:01,470 --> 00:00:02,303 ‫environment variables. 3 00:00:02,303 --> 00:00:04,300 ‫So for this, I'm going to author a function 4 00:00:04,300 --> 00:00:06,517 ‫from scratch and I'll call it, 5 00:00:06,517 --> 00:00:10,077 ‫"lambda-config-demo". 6 00:00:10,940 --> 00:00:13,650 ‫I would use Python 3.8 7 00:00:13,650 --> 00:00:15,810 ‫and create my function. 8 00:00:15,810 --> 00:00:17,020 ‫So the idea here is that we want 9 00:00:17,020 --> 00:00:19,260 ‫to play with environment variables. 10 00:00:19,260 --> 00:00:20,970 ‫And so we're going to pass environment variables 11 00:00:20,970 --> 00:00:23,880 ‫in unencrypted form to learn their function. 12 00:00:23,880 --> 00:00:25,230 ‫For the encryption part, 13 00:00:25,230 --> 00:00:27,330 ‫this will be dealt with in the security section 14 00:00:27,330 --> 00:00:28,250 ‫of this course. 15 00:00:28,250 --> 00:00:30,210 ‫And so we want to pass this environment variable 16 00:00:30,210 --> 00:00:32,710 ‫to lambda and have lambda print this environment 17 00:00:32,710 --> 00:00:34,040 ‫variable to the console. 18 00:00:34,040 --> 00:00:36,380 ‫Okay, so first, let's modify the code 19 00:00:36,380 --> 00:00:38,350 ‫of our lambda function. 20 00:00:38,350 --> 00:00:39,900 ‫So let's go into the code source, 21 00:00:39,900 --> 00:00:43,570 ‫and we need to modify it a little bit for it to work. 22 00:00:43,570 --> 00:00:44,500 ‫So the first thing we have to do 23 00:00:44,500 --> 00:00:48,270 ‫is to do "import OS" to import the OS package. 24 00:00:48,270 --> 00:00:50,030 ‫And this is going to allow us 25 00:00:50,030 --> 00:00:53,770 ‫to get access to environment variables. 26 00:00:53,770 --> 00:00:55,520 ‫And then in the return statement, 27 00:00:55,520 --> 00:00:58,140 ‫there's going to "return os.getenv", 28 00:00:58,140 --> 00:00:59,490 ‫get environment, 29 00:00:59,490 --> 00:01:01,010 ‫and then the environment variable name, 30 00:01:01,010 --> 00:01:02,740 ‫so, "ENVIRONMENT_NAME". 31 00:01:02,740 --> 00:01:05,330 ‫So we're going to create this environment variable 32 00:01:05,330 --> 00:01:07,060 ‫called "ENVIRONMENT_NAME" 33 00:01:07,060 --> 00:01:08,830 ‫and we're going to give it a value 34 00:01:08,830 --> 00:01:10,440 ‫and the value of which will be retrieved 35 00:01:10,440 --> 00:01:12,450 ‫by the lambda function and return. 36 00:01:12,450 --> 00:01:15,530 ‫So let's deploy this function to save our changes. 37 00:01:15,530 --> 00:01:18,260 ‫Next, we need to go into the configuration 38 00:01:18,260 --> 00:01:21,120 ‫of our lambda function, which I missed. 39 00:01:21,120 --> 00:01:23,363 ‫So let's go here and go to configuration. 40 00:01:24,350 --> 00:01:26,953 ‫On the left hand side, we have environment variables. 41 00:01:28,000 --> 00:01:31,450 ‫And here I can edit and add an environment variable. 42 00:01:31,450 --> 00:01:34,020 ‫The key is going to be, "ENVIRONMENT_NAME", 43 00:01:34,020 --> 00:01:36,410 ‫and the value is going to be "dev", 44 00:01:36,410 --> 00:01:38,670 ‫but you could add many different environment variables 45 00:01:38,670 --> 00:01:40,720 ‫if you wanted to, okay? 46 00:01:40,720 --> 00:01:41,553 ‫As we can see, 47 00:01:41,553 --> 00:01:43,250 ‫there is an encryption configuration we can have, 48 00:01:43,250 --> 00:01:46,500 ‫but I will do this in the security section of this course. 49 00:01:46,500 --> 00:01:50,130 ‫So for now this environment variable is unencrypted. 50 00:01:50,130 --> 00:01:51,860 ‫So now that we've set the key 51 00:01:51,860 --> 00:01:54,710 ‫and the value to be "dev", now let's test our function. 52 00:01:54,710 --> 00:01:58,230 ‫So if we go in here and test our function, 53 00:01:58,230 --> 00:01:59,910 ‫and we'll create a sample event 54 00:02:01,670 --> 00:02:04,800 ‫and create and now test our function. 55 00:02:04,800 --> 00:02:07,560 ‫As we can see, the response we get is "dev", 56 00:02:07,560 --> 00:02:10,890 ‫which corresponds to the value of the environment, 57 00:02:10,890 --> 00:02:12,850 ‫they're both named "ENVIRONMENT_NAME". 58 00:02:12,850 --> 00:02:15,080 ‫But so now if I go and edit this 59 00:02:15,080 --> 00:02:19,820 ‫and change this from "dev" to "prod" and save my function, 60 00:02:19,820 --> 00:02:22,110 ‫as we can see, the code of the function has not changed, 61 00:02:22,110 --> 00:02:24,110 ‫so the code did not change, 62 00:02:24,110 --> 00:02:26,930 ‫but now if I test my function yet again, 63 00:02:26,930 --> 00:02:29,180 ‫the response is going to be "prod". 64 00:02:29,180 --> 00:02:32,150 ‫And so here we see the impact that environment variables 65 00:02:32,150 --> 00:02:34,210 ‫can have on to the return values 66 00:02:34,210 --> 00:02:36,040 ‫and the behavior of our code, 67 00:02:36,040 --> 00:02:38,680 ‫and this is the whole power behind environment variables. 68 00:02:38,680 --> 00:02:39,513 ‫So that's it. 69 00:02:39,513 --> 00:02:40,560 ‫I hope you liked this lecture, 70 00:02:40,560 --> 00:02:42,510 ‫and I will see you in the next lecture.