1 00:00:00,000 --> 00:00:01,548 ‫Now let's talk about mapping. 2 00:00:01,548 --> 00:00:03,615 ‫Mappings are fixed variables within 3 00:00:03,615 --> 00:00:04,846 ‫your CloudFormation Template. 4 00:00:04,846 --> 00:00:06,410 ‫They have to be hard coded. 5 00:00:06,410 --> 00:00:08,524 ‫They are very handy if you need to 6 00:00:08,524 --> 00:00:09,890 ‫hard code some values based on 7 00:00:09,890 --> 00:00:11,198 ‫different environments you're in. 8 00:00:11,198 --> 00:00:13,238 ‫So dev vs. prod, or regions 9 00:00:13,238 --> 00:00:15,669 ‫such as AWS regions, or AMI types, etc ... 10 00:00:15,669 --> 00:00:17,038 ‫And as I said all the values must be 11 00:00:17,038 --> 00:00:19,322 ‫written out explicitly in your template. 12 00:00:19,322 --> 00:00:21,384 ‫As an example, here is a mapping. 13 00:00:21,384 --> 00:00:22,712 ‫And this is how you write it. 14 00:00:22,712 --> 00:00:24,446 ‫We have a Mappings section. 15 00:00:24,446 --> 00:00:25,840 ‫Then you have the name of the mapping. 16 00:00:25,840 --> 00:00:26,926 ‫Then you have a key. 17 00:00:26,926 --> 00:00:30,283 ‫Underneath you have key's code name and values. 18 00:00:30,283 --> 00:00:34,560 ‫So it's quite a low-level type of architecture. 19 00:00:34,560 --> 00:00:36,101 ‫To make it more concrete, 20 00:00:36,101 --> 00:00:39,816 ‫we may have a RegionMap to map regions to AMIs. 21 00:00:39,816 --> 00:00:40,649 ‫So we're saying 22 00:00:40,649 --> 00:00:45,200 ‫okay, you're within us-east-1, us-west-1, or eu-west-1. 23 00:00:45,200 --> 00:00:47,496 ‫Based on if you choose a 32-bit 24 00:00:47,496 --> 00:00:49,867 ‫or a 64-bit type of architecture. 25 00:00:49,867 --> 00:00:52,986 ‫Here is the AMI ID you should be using. 26 00:00:52,986 --> 00:00:55,701 ‫So overall, this is just a hard coding saying, 27 00:00:55,701 --> 00:00:58,353 ‫okay, based on where the template is being run, 28 00:00:58,353 --> 00:01:00,894 ‫this is the AMI I want to use. 29 00:01:00,894 --> 00:01:02,910 ‫So when would you use mappings and parameters? 30 00:01:02,910 --> 00:01:04,768 ‫Well, mappings are great when you know in advance 31 00:01:04,768 --> 00:01:06,815 ‫all the values that can be taken. 32 00:01:06,815 --> 00:01:08,305 ‫So for example AMI ID. 33 00:01:08,305 --> 00:01:10,724 ‫You can deduce them from variables such as 34 00:01:10,724 --> 00:01:14,206 ‫region, AZ, AWS Account, environment, etc ... 35 00:01:14,206 --> 00:01:16,057 ‫Whatever you can think of. 36 00:01:16,057 --> 00:01:19,720 ‫To me, they allow safer control over the template. 37 00:01:19,720 --> 00:01:20,758 ‫But if you need 38 00:01:20,758 --> 00:01:22,599 ‫the values to be really user specific, 39 00:01:22,599 --> 00:01:24,638 ‫then the user should input a value and 40 00:01:24,638 --> 00:01:26,322 ‫you don't know in advance what it can be, 41 00:01:26,322 --> 00:01:28,773 ‫then you should use the parameters. 42 00:01:28,773 --> 00:01:30,344 ‫Now to access the mapping values, 43 00:01:30,344 --> 00:01:34,156 ‫there's this function called Fn:FindInMap. 44 00:01:34,156 --> 00:01:37,630 ‫Basically it returns a value from a specific key. 45 00:01:37,630 --> 00:01:38,797 ‫The short-hand 46 00:01:39,701 --> 00:01:41,148 ‫syntax is this one. 47 00:01:41,148 --> 00:01:44,121 ‫We use FindInMap with a little exclamation point. 48 00:01:44,121 --> 00:01:46,705 ‫We have to give the MapName, and the TopLevelKey, 49 00:01:46,705 --> 00:01:48,099 ‫and the SecondLevelKey. 50 00:01:48,099 --> 00:01:50,538 ‫So three parameters right here. 51 00:01:50,538 --> 00:01:52,392 ‫That's something you should know for the exam, 52 00:01:52,392 --> 00:01:54,695 ‫just the syntax of it, okay. 53 00:01:54,695 --> 00:01:58,339 ‫So if we look at this little CloudFormation template, 54 00:01:58,339 --> 00:01:59,775 ‫we can see we have a RegionMap 55 00:01:59,775 --> 00:02:01,431 ‫that we have defined from before. 56 00:02:01,431 --> 00:02:04,414 ‫If we want to create an EC2Instance 57 00:02:04,414 --> 00:02:07,490 ‫and reference, get the right AMI ID, 58 00:02:07,490 --> 00:02:11,364 ‫then we use the FindInMap function for the ImageID. 59 00:02:11,364 --> 00:02:13,458 ‫The first one is a MapName, 60 00:02:13,458 --> 00:02:17,238 ‫so we'll use RegionMap because here it's called RegionMap. 61 00:02:17,238 --> 00:02:19,303 ‫The second is that we wanna reference to 62 00:02:19,303 --> 00:02:20,762 ‫the AWS Region we're in. 63 00:02:20,762 --> 00:02:21,595 ‫So we're going to use 64 00:02:21,595 --> 00:02:23,195 ‫the pseudo-parameter we just talked about. 65 00:02:23,195 --> 00:02:24,467 ‫And the Ref function. 66 00:02:24,467 --> 00:02:26,500 ‫So we reference the AWS Region, 67 00:02:26,500 --> 00:02:28,646 ‫the CloudFormation template it's running in. 68 00:02:28,646 --> 00:02:31,149 ‫For example, we're running in us-east-1, 69 00:02:31,149 --> 00:02:32,899 ‫then we are in this block. 70 00:02:32,899 --> 00:02:35,490 ‫Then we say 32 as the SecondLevelKey. 71 00:02:35,490 --> 00:02:36,845 ‫So we look at the 32 key, 72 00:02:36,845 --> 00:02:38,565 ‫and we get the value from it. 73 00:02:38,565 --> 00:02:40,815 ‫We'll get this amI-6411e20d 74 00:02:41,696 --> 00:02:43,743 ‫This is the one that will be selected. 75 00:02:43,743 --> 00:02:45,846 ‫This is all you should know about mapping. 76 00:02:45,846 --> 00:02:49,109 ‫Just remember the syntax for the FindInMap function, 77 00:02:49,109 --> 00:02:50,803 ‫and the fact that mappings have to be 78 00:02:50,803 --> 00:02:53,081 ‫written out explicitly in your template.