1 00:00:06,260 --> 00:00:09,851 - So one really useful thing that Terrafrom 2 00:00:09,851 --> 00:00:12,910 has implemented are functions. 3 00:00:12,910 --> 00:00:15,650 Functions allow you to call 4 00:00:15,650 --> 00:00:19,430 out externally to retrieve values 5 00:00:19,430 --> 00:00:23,800 or calculate values outside your config. 6 00:00:23,800 --> 00:00:27,410 Now unfortunately, Terraform does not have the ability 7 00:00:27,410 --> 00:00:31,340 for you to implement your own functions directly 8 00:00:31,340 --> 00:00:33,050 so we'll talk about a little bit more 9 00:00:33,050 --> 00:00:36,740 in some later lessons 10 00:00:36,740 --> 00:00:38,150 if you do have that need 11 00:00:38,150 --> 00:00:43,150 but they have well over 100 built-in functions defined 12 00:00:44,670 --> 00:00:47,930 and they can do just all kinds of different things. 13 00:00:47,930 --> 00:00:50,610 So let's just take a look 14 00:00:50,610 --> 00:00:52,060 at some of the more common ones 15 00:00:52,060 --> 00:00:53,290 that I find myself using 16 00:00:53,290 --> 00:00:55,810 and that you may use as well. 17 00:00:55,810 --> 00:00:59,610 Now, the syntax for calling a function 18 00:00:59,610 --> 00:01:01,890 is simply to embed the name 19 00:01:01,890 --> 00:01:03,630 of the function in your code 20 00:01:03,630 --> 00:01:07,320 and use the parentheses for any parameters. 21 00:01:07,320 --> 00:01:09,130 You have to know the names of these functions. 22 00:01:09,130 --> 00:01:10,683 They're all documented here. 23 00:01:11,520 --> 00:01:13,090 You can just follow this link 24 00:01:13,090 --> 00:01:15,250 and it will take you there. 25 00:01:15,250 --> 00:01:17,330 It'll tell you all the functions. 26 00:01:17,330 --> 00:01:18,380 It has 'em all broken out 27 00:01:18,380 --> 00:01:22,550 by function type like date/time functions, 28 00:01:22,550 --> 00:01:25,810 string functions, mathematical functions, et cetera 29 00:01:25,810 --> 00:01:29,285 and then you can assign these to locals 30 00:01:29,285 --> 00:01:33,710 just like you can assign any other expression. 31 00:01:33,710 --> 00:01:35,463 You can use them interpolations. 32 00:01:36,500 --> 00:01:38,530 You can use a function within another function 33 00:01:38,530 --> 00:01:40,860 so I'll just show you some of the syntax for that. 34 00:01:40,860 --> 00:01:43,000 It's pretty simple to understand. 35 00:01:43,000 --> 00:01:45,610 But it's a really powerful feature. 36 00:01:45,610 --> 00:01:49,810 So let's start off with just some simple date 37 00:01:49,810 --> 00:01:51,640 and time functions. 38 00:01:51,640 --> 00:01:55,500 These are used quite frequently. 39 00:01:55,500 --> 00:01:58,720 This is the current timestamp. 40 00:01:58,720 --> 00:02:00,120 This is used all the time 41 00:02:00,120 --> 00:02:03,270 if you need to make a unique name for an S3 buckets 42 00:02:03,270 --> 00:02:05,010 as our examples were. 43 00:02:05,010 --> 00:02:07,270 This is probably what Terraform uses internally 44 00:02:07,270 --> 00:02:09,640 when they make their default names. 45 00:02:09,640 --> 00:02:11,290 This'll just give you a timestamp 46 00:02:12,642 --> 00:02:16,257 and then you can, formatdate will give you the name 47 00:02:20,260 --> 00:02:23,900 or the month of the year. 48 00:02:23,900 --> 00:02:26,880 And this takes a standard date format string 49 00:02:26,880 --> 00:02:29,230 and you can see we're referencing something 50 00:02:29,230 --> 00:02:30,790 that we already assigned above 51 00:02:30,790 --> 00:02:34,030 so you can reference within this same local block in here 52 00:02:34,030 --> 00:02:35,220 so this takes two arguments. 53 00:02:35,220 --> 00:02:36,690 It takes a format string 54 00:02:36,690 --> 00:02:38,410 and it takes a timestamp. 55 00:02:38,410 --> 00:02:43,120 And then if we wanted to increment the day by one, 56 00:02:43,120 --> 00:02:44,600 we can call formatdate 57 00:02:44,600 --> 00:02:48,130 with telling it that we just want to date to output 58 00:02:48,130 --> 00:02:51,150 and we can call the timeadd function 59 00:02:51,150 --> 00:02:53,580 so with timeadd, we're adding 24 hours 60 00:02:53,580 --> 00:02:56,070 to the timestamp so that's gonna be tomorrow 61 00:02:56,070 --> 00:02:59,173 and this is gonna tell us the day of the month. 62 00:03:03,330 --> 00:03:05,470 There's lots of numeric functions in there. 63 00:03:05,470 --> 00:03:08,743 These aren't used quite as often in my experience. 64 00:03:09,870 --> 00:03:12,070 Here we're using the minimum function. 65 00:03:12,070 --> 00:03:15,630 As I mentioned before in the last section, 66 00:03:15,630 --> 00:03:16,590 we had a bug here 67 00:03:16,590 --> 00:03:18,816 because we don't actually want 68 00:03:18,816 --> 00:03:21,210 to check whether or not this is zero. 69 00:03:21,210 --> 00:03:22,440 What we wanna do is check 70 00:03:22,440 --> 00:03:24,470 whether it's above the minimum. 71 00:03:24,470 --> 00:03:25,610 So the way to do that 72 00:03:25,610 --> 00:03:27,910 is to use this min function. 73 00:03:27,910 --> 00:03:31,280 This is gonna return the lesser of the number 74 00:03:31,280 --> 00:03:34,420 of buckets or the bucket count. 75 00:03:34,420 --> 00:03:36,240 Actually I think we actually want the max there 76 00:03:36,240 --> 00:03:38,410 so we have a bug on top of a bug but you get the idea. 77 00:03:38,410 --> 00:03:41,030 It's a simple numeric function 78 00:03:41,030 --> 00:03:41,990 that compares two numbers 79 00:03:41,990 --> 00:03:45,417 and you can return the min with the minimum function 80 00:03:45,417 --> 00:03:46,740 and the min or the mac. 81 00:03:46,740 --> 00:03:51,740 String functions are used very frequently. 82 00:03:51,940 --> 00:03:56,940 In fact, we used lower earlier in this lesson. 83 00:03:58,170 --> 00:04:03,040 So lower, upper, trimspace will remove any white space 84 00:04:03,040 --> 00:04:05,630 from the beginning or end of a string. 85 00:04:05,630 --> 00:04:10,630 Format, it's the same as the C string formatting functions. 86 00:04:12,180 --> 00:04:14,410 So you give it a format string 87 00:04:14,410 --> 00:04:17,570 and you give it the string to put it in there. 88 00:04:17,570 --> 00:04:20,130 This one's not so useful because the interpolation 89 00:04:20,130 --> 00:04:24,320 in Terraform is actually much more robust than this 90 00:04:24,320 --> 00:04:26,490 but it does have a list form 91 00:04:26,490 --> 00:04:29,710 so this will create hello 92 00:04:29,710 --> 00:04:30,550 and it'll take a list 93 00:04:30,550 --> 00:04:33,700 and it'll create four strings all with that 94 00:04:33,700 --> 00:04:34,900 if that's useful. 95 00:04:34,900 --> 00:04:36,350 That can be useful. 96 00:04:36,350 --> 00:04:38,970 There're other interpolation methods now in Terraform 97 00:04:38,970 --> 00:04:42,740 that may be more appropriate than this for most cases 98 00:04:42,740 --> 00:04:44,770 and we'll talk about those later. 99 00:04:44,770 --> 00:04:46,510 So that's kinda how functions work. 100 00:04:46,510 --> 00:04:48,580 They're really simple to get started in using 101 00:04:48,580 --> 00:04:50,020 by they can be really, really powerful. 102 00:04:50,020 --> 00:04:52,174 You can do things like generate UUIDs 103 00:04:52,174 --> 00:04:54,470 if you need a unique identifier. 104 00:04:54,470 --> 00:04:56,960 That's pretty powerful ability. 105 00:04:56,960 --> 00:04:59,080 You can look up values in a map, 106 00:04:59,080 --> 00:05:01,913 things like that are all implemented in functions.