‫So in this lecture
‫we're going to create a CloudFormation template
‫upload it that will create a Lambda function for us.
‫So let's open the lambda-CloudFormation
‫and then the lambda-xray.yaml
‫And let's have a look at this CloudFormation template.
‫So first of all, we see
‫that there are three parameters available to us.
‫The S3 Bucket parameter, The S3 Key parameter
‫and the S3 object version parameter.
‫And these will be helpful to tell CloudFormation
‫where to get the function zip from in Amazon S3.
‫Next we create resources.
‫And the first resource we create is a Lambda execution role.
‫So it's an IAM role.
‫There is a policy document which allows Lambda
‫functions to assume this IAM role
‫as well as the policy itself.
‫And the policy contains multiple statements.
‫The first one is actions on CloudWatch logs
‫The second one is action on x-ray
‫to be able to send traces to x-ray
‫and the last one and finally
‫some allow operations on S3, Get star, List star
‫to be able to read it from Amazon S3.
‫Next to function itself, Lambda with x-ray.
‫So it's a function in which we have to specify the handlers
‫so Index.handler
‫the role which is the Lambda execution role
‫Arn that we get
‫and retrieve thanks to this interesting function
‫get attributes that will be get gotten
‫from this Lambda role right here
‫and
‫the code itself
‫which is having three inputs the S3 buckets
‫the S3 key in the S3 object version.
‫And all of these are references
‫to the parameters we have specified
‫at the beginning of the templates.
‫Okay So we're good to go with this
‫The runtime is Nodejs14.X, the timer is 10 seconds.
‫And then we enable x-ray
‫by having tracing config mode active.
‫So as we can see, the entire configuration
‫of our Lambda function can be done through CloudFormation
‫which is really awesome.
‫So next we need to just set up things to run these
‫CloudFormation templates.
‫So the first thing is that we can go into Amazon is free
‫and I need to create a new bucket.
‫So let's create a bucket and I'll call this one
‫S3 CloudFormation
‫Lambda demos Stephane
‫and
‫in the same region, as I am with my Lambda functions
‫and I will enable buckets versioning so that we
‫have a bucket
‫So we have an object version necessary
‫for uploading this file through confirmation.
‫So, okay.
‫So we're good to go.
‫We create this bucket.
‫It has versioning enabled
‫and I need to just go in it.
‫So let's open this CloudFormation buckets right here
‫and I need to upload my function.zip.
‫So if you're going to go to CloudShell or wherever
‫you were having this, let's go into the Lambda folder
‫and have created a function.zip file.
‫So let's just get the full path to it and I'm going to
‫download it.
‫So I copy this, I do actions, download file
‫and I copy the entire path to my function.zip
‫within my CloudShell environment.
‫So I download this file
‫And the file has been downloaded
‫So now we can reuse this function.zip
‫and I can just directly upload it into my S3 buckets.
‫So it's going to be uploaded
‫upload
‫and we're good to go.
‫So now we are able to reference this function, zip directly
‫from our CloudFormation templates.
‫So very good, now lets go to Cloudformation.
‫So that's open the CloudFormation
‫Console.
‫And create a stack.
‫The template is ready
‫and I will upload the templates
‫and I will choose the templates from before.
‫So let's take the code, go into the lambda section
‫Lambda CloudFormation and Lambda x-ray.yaml.
‫Okay. So next I click on next.
‫This Stack name is demo LambdaCF.
‫And now we need to specify three parameters
‫the buckets parameter
‫the S3 key parameter and the object version.
‫So the bucket name is right here.
‫So let me copy this and paste it.
‫The key is the name of the zip file
‫So function.zip
‫and the object version I can get
‫by just clicking on the object.
‫And because we have enabled versioning before
‫on the buckets
‫the object version should be somewhere around here
‫versions in here, and here is the object version
‫of I'll just copy
‫like this
‫and then paste it here.
‫Okay So everything looks good here.
‫And then I'm going to click on next and I will scroll down
‫click on next and then
‫acknowledge the fact
‫that CloudFormation will create an IAM role for us
‫which is necessary for our Lambda function
‫and then create stack.
‫So my stack creation is in progress
‫and I'm going to wait for it to be done.
‫Okay So my CloudFormation stack is now completed.
‫So my Lambda function was created.
‫So if you go back into Lambda, refresh this
‫I have now nine functions
‫and this one was created by CloudFormation.
‫So really cool though.
‫As soon as we opened this function
‫we get an information message.
‫This function belongs to an application
‫because there was an integration between CloudFormation
‫and Lambda that was detected by the Lambda console.
‫So it knows that it is managed by a CloudFormation template.
‫And so, as we can see
‫the function zip was uploaded correctly
‫and then you could test their function
‫as with a simple events, click on create and then test.
‫And obviously the function would succeed
‫because it has the necessary permissions
‫and returns a list of the buckets.
‫And if you took your time and went into the x-ray console
‫you should see the extra traces as well.
‫Appearing.
‫Thanks to the integration we have with x-ray.
‫As we can see, if we go to configuration
‫we can see that active tracing is enabled
‫for Ireland function.
‫So that's it.
‫You can just leave things as is
‫or if you wanted to clean up, you could take this stack
‫and then delete it to clean up the Lambda role as well
‫as the Lambda function within your accounts.
‫But that's it for this lecture.
‫We've seen a lot of different
‫moving parts in here, but really cool.
‫I will see you in the next lecture.