Check out a free preview of the full Build Go Apps That Scale on AWS course
The "Deploying the Lambda" Lesson is part of the full, Build Go Apps That Scale on AWS course featured in this preview video. Here's what you'd learn in this lesson:
Melkey deploys the lambda to AWS using the CDK CLI. The function and environment are bootstrapped, and the code is packaged in a zip file. The `cdk diff` command can compare the current environment with what's being deployed.
Transcript from the "Deploying the Lambda" Lesson
[00:00:00]
>> Okay, so, we have, everything defined for a CDK, we have everything defined for Go code. Now, I have to handle this interesting portion of deployment code. So you may think, OK, let's just create another executable and then send it over. Because we have to provide the runtime, it gets a little bit more involved.
[00:00:18]
So here, we're actually going to type in, Go operating software, is gonna be Linux, okay? Go arch, arch, by the way, is gonna be AMD 64, okay? And now we're gonna do go build-output and you have to call it bootstrap. This is a hard requirement on the provisioned native AWS Linux system, that's what the AL portion stands for in the runtime, AL Amazon Linux, okay?
[00:00:46]
If you run this, now we have this bootstrap executable. You can go ahead and remove that main if you want. Cool, I almost had a heart attack, I thought I just removed all of main.go, luckily I didn't. So now we just have to bootstrap, but we're not done because this also expects us to pass in a zip file.
[00:01:15]
We can't just pass in the binary, we have to zip it. So you can do zip function.zip and the name of the binary bootstrap. So now we have an executable that we can run and the zip version of it. And the name that function.zip is essentially random, but it has to map to where we defined our go_cdk code here, function.zip.
[00:01:40]
Okay, this is the kind of thing like telling CDK, hey, this is where we put your code, take it. Okay, so we zipped it, now, we can CD back out to the root of our project. And this is where we're gonna run our first CDK command. I'm gonna clear this, I'm gonna cdk diff.
[00:02:00]
And like we saw earlier, cdk diff is basically gonna compare a snapshot in time of the previous deployed version of our stack and application with the new version of what we wanna deploy. Luckily for us, we don't have that snapshot, so there's gonna be all net new, which is why it's Green positives, right?
[00:02:21]
We're asking, hey, it recognized that we are going to create a new lambda, it's gonna give it an associate role for the lambda and the actual lambda function, right? It recognized myLambdaFunction, that's what we call that construct, that's what we call that piece of code in the resource.
[00:02:40]
It's actually right here, my Lambda function. So it recognized it, it picked it up. So with cdk diff out of the way, we can now run CDK deploy. It's gonna synthesize, right? We know what synthesizing is. To make it native to Cloudformation, some magic under the hood here.
[00:03:04]
And one thing I want to add while we syndicate deploy, it's gonna give us this kind of prop of like, hey, there's some changes here. Do you want to deploy these changes? And it's not just me telling you, hey, yeah, some infrastructure is more expensive Expensive, more costly to deploy, it's a well-known thing in cloud infrastructure that if you deploy something incorrectly, it's kind of out of, it's only in your hands, right?
[00:03:30]
Because it could be expensive, it could be a long time for it to deploy. That's why there's these levels of, are you sure you want to deploy this? Double-check everything. Make sure it's working. What'd you actually expect it to do?Right. So here you can see we have that summary.
[00:03:45]
Okay, the lambda function looks good to me a policy change for a new function. This was good to me you wish to deploy these changes? Yes, I do. Question.
>> Did get a error on CDK bootstrap So here, I think there was a prerequisite in getting started with CDK, where I believe it does have, so install this AWS CDK CLI, and there's like a bootstrap environment.
[00:04:13]
I'm going to assume maybe CERN users didn't do this step. Maybe I didn't communicate. This correctly for everyone, my apologies. But it'd be good to just follow that and bootstrap your environment. That does take some time. Infrastructure is slow. And you'll realize like iteration speed when creating new infra is a slow task and so it There's a lot of that goes into it to make sure we do it correctly.
[00:04:42]
And so Boots Shop is one of those aspects where it's able to basically create all the artifacts for you to run the C DK deploys to run the CDK diffs and map it to your account. Curious
>> On the line 21 there?
>> Yeah.
>> With the string name.
[00:04:57]
Can you talk a little bit about Abservability and sort of troubleshooting when it comes to that name. Is that the key that you try to trace throughout your system if something's going wrong? << Yes, but yes, the answer is yes. This is kind of, because you can have multiple Lambda functions, right?
[00:05:17]
So let's say we have one. So it's kind of easy in this or. Architecture. Okay,there's a problem in Lambda. I know where it is. Lemme just go in there and fix it up. But what if you had like 13 crazy example, bear with me. We need to identify each Lambda function with this unique identifier so that when we get errors upstream from our clients, or even in the code itself, we'll actually have logs that say, This Lambda, your My Lambda function or Lambda 123 or Register User Lambda, experience an error, and that's where you can go trace your actual Lambda code.
[00:05:48]
>> I've been burned by trying to make my own zip files for Lambda before by using the Mac OS Zip function. It ends up creating a folder with all your stuff in it. If you have your stuff Stuff in a folder, Lambda will fail. So command line version, very tidy way to do it.
[00:06:06]
>> Yes, yeah, yeah, yeah. So thank you for bringing that up. Everything I try to do is command line. There's gonna be portions where we're gonna like, down the road, where we deploy multiple routes for a function. We can test these routes on the UI, no Going to use straight up curl requests to do it the way we're supposed to.
[00:06:23]
So I'm a big fan of doing as much as I can and see a lot and not bring in third parties or packages when I don't need to.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops