‫Okay so let's explore this
‫serverless application repository and this is a way for us
‫to share and deploy serverless applications quickly
‫so we have our own application but also we have
‫public applications so public applications
‫are applications that verified authors will publish
‫on the SAR or the serverless application repository
‫for us to use.
‫For example, there is a hello world function right here
‫that I can click on and deploys.
‫So if I click on it I can deploy hello world version 1.0.4
‫and it gives me the template, the source code URL,
‫the author which is AWS which is also AWS verified author
‫that makes a lot of sense,
‫the templates of the function itself,
‫as well as a read me file and the application settings.
‫So all of these can be used to deploy public applications.
‫So for example, this one, I'll ask demo identity names
‫so for the hello world, and I click on deploy.
‫And here we go, our application is being deployed
‫so as we can see we have a deployment
‫and the deployment is in progress so from seven seconds ago
‫and it comes from the same template
‫so this creates logical resources
‫so if I go into the same templates
‫this is a CloudFormation stack
‫so click on CloudFormation stack and I am taken
‫to the CloudFormation UI where I can see
‫that's my application is being created
‫in progress right now and now the create is complete.
‫So that means that my application is good to go.
‫We have a hello world and within the hello world,
‫we have a Lambda function and if I click on it
‫I can see that this application has been deployed.
‫It belongs to an application which was deployed
‫using the serverless repository.
‫And I could go ahead and test this application
‫so for this, I will scroll down, click on tests,
‫new events, and tests, and my function succeeded
‫and it says value one.
‫So this is great.
‫We've deployed our first Lambda function
‫from the serverless application repository.
‫And that could be enough, but I want to show you
‫how to publish your own private application
‫on the serverless application repository.
‫So to do so let's open CloudShell
‫because we'll create a SAM application directly
‫from CloudShell.
‫Okay the other thing is that's all the commands I have
‫are in the code of the course under zero steps.
‫So let me go ahead and put the windows next to each other.
‫And so first we're going to initialize a SAM application
‫using the runtime Python and let's do 3.8, 3.8 okay?
‫So I'm going to copy this and paste it and press enter.
‫Now the SAM (indistinct) is by default
‫installed on CloudShell so we don't need to
‫install it beforehand.
‫So I use a quick start template.
‫It'll be zip and I'll press enter for the SAM app name
‫and then we'll have a hello world example
‫so just one, one, one, one, and we're good to go.
‫Okay so now if I do LS we have a SAM app okay?
‫And so what I'm going to do is just go in it,
‫in this SAM app and let's explore.
‫We have events, hello world, template.yaml, and tests.
‫Now we need to edit this template.yaml file
‫so I will do nano template.yaml so it's not installed
‫so sudo yam install -Y nano to install the nano package.
‫And then let's try again so nano template.yaml
‫and here we go we can update this file.
‫So now we just need to go ahead and add a few blocks.
‫So to do this, I'm going to go into
‫my modifications.yaml file
‫and we're going to add a few sections.
‫The first one is metadata, and this is metadata
‫around the serverless application repository
‫so we'll have to specify it's name, it's description,
‫who is the author, as well as the semantic version.
‫So I copy this and then I paste it, paste,
‫and the block has been added.
‫And then the other thing is we need to add
‫a parametric block to specify a parameter
‫for the template itself and the lambda function.
‫So I'm going to paste this and we have one parameter
‫called custom name with type string.
‫And then finally we need to edit our Lambda function.
‫And so for the lambda function
‫we need to add some environment variables
‫under the properties.
‫So under the properties right here
‫I'm going to add this block of environment variable
‫which is going to set an environment variable
‫named custom name that is referencing the parameter
‫custom name from CloudFormation so I'll copy this
‫and under properties right here under the runtime,
‫I'm going to add a new line, spaces,
‫and then I'm going to paste this in, paste,
‫and this is perfect.
‫So control X Y enter, and we're good to go
‫so we can make sure that our template.yaml is correct
‫by just cutting it and as we can see
‫we have the environment and we have the metadata
‫so this is good to go.
‫Next we can go to this more steps.
‫Okay so once we've done this we need to create
‫an S3 buckets to deploy our applications
‫so I'll make an S3 bucket
‫I'll call it stephane-sar-demo and 2021
‫and it's been created.
‫And next we're going to put a bucket policy
‫using the file policy.json in here
‫so we need to create that file
‫so nano.policy.json to create this file
‫and you can find the file contents right here.
‫So it's a bucket policy which allows us to
‫allow the serverless application repository
‫to get objects from this S3 buckets
‫so to get the code that we're going to deploy.
‫And then we need to specify the bucket name
‫which I did forget obviously so let's go back and find it.
‫Stephane-sar-demo-2021 so let's do it again.
‫I'll copy it and I'll just edit in here
‫so stephane-sar-demo-2021 paste this and we are good to go.
‫So now this file is created.
‫So we have the right bucket names, stephane-sar-demo-2021
‫as well as the right policy.
‫So we're going to apply this policy on our S3 bucket
‫by using an API call.
‫So we'll use the S3 API put bucket policy.
‫We need again to update the bucket name here
‫and then the policy file and paste press enter
‫and it's been applied.
‫Okay, so next then now that
‫the serverless application repository can access
‫our S3 buckets we need to package the application.
‫So let's just package our application
‫so we're going to copy this command
‫to package our application
‫and we specify again the name of the S3 buckets at the end.
‫And this is going to create a package.yaml file.
‫And this package.yaml as we know is what is going to be
‫a transformed CloudFormation template okay?
‫So as we can see, we can deploy the function
‫as we've done from before using this command
‫but we don't wanna do so.
‫We wanna publish now this application
‫into our serverless application repository.
‫So to do so we're going to run the sam-publish commands.
‫So we're gonna just run the sam-publish command right here
‫paste it and make sure you change the region
‫if you are not in US East one.
‫And so this is saying that this does not have
‫access to the buckets so one second.
‫And so the instructions are here
‫and I know already what is wrong
‫so I look at the instructions I scroll down and here
‫I need you edit the S3 bucket name
‫but also the source accounts for the S3 buckets
‫so my policy is not perfect so I'm can just go and edit it.
‫So I'll do again the nano policy.json and here
‫the AWS source account has to be changed
‫and I need to edit this and insert my account number
‫which is right here.
‫So I'm going to copy this account number, paste it here,
‫exit and save.
‫Yes and then I'm going to apply the policy again
‫so using the S3 API command.
‫It's been applied. Clear.
‫And so now if I rerun the publish command
‫it should work because I have a correct,
‫yes, S3 bucket policy.
‫Perfect.
‫So as we can see, we can now click on this link
‫to view the application in the console.
‫So if I open this link right here
‫within our published applications
‫and I'm going to close this, we have my app
‫so let me show you.
‫Under my application we have my app
‫and under available application it's also referenced under
‫private applications so very cool.
‫Now we could go ahead and deploy this application
‫so I click on it and I can sorry
‫go into available applications, private applications
‫here it is and here we're going to publish it.
‫So it is perfect.
‫We can look at the read me file we have none.
‫The application is my-app, and then we're going to enter
‫custom name Stephane to greet myself.
‫So I'll deploy this.
‫And I forgot to obviously update the application code
‫which is a cool mistake to do, because I can show you
‫how to update your serverless repo app.
‫So anyway first if we look at the end points and we go to it
‫and we do slash hello, we're going to get
‫message hello world so that means that
‫our serverless application has been deployed
‫and it says hello world
‫but we want it to say hello world Stephane.
‫So to do so we need to just go ahead
‫and update it for the code
‫so let's go back into CloudShell
‫which is somewhere around here, there we go.
‫And within my app so in the hello world,
‫there is an app.py and I need to just edit app.py.
‫I'm going to just delete this file entirely.
‫I'm going to remove app.py to make it simple,
‫touch app.py to create it and nano app.py to edit it
‫and I'm just going to take the application
‫from hello world in here, copy this entirely, paste it
‫and paste it.
‫So this is, it's very simple lambda function
‫which takes some inputs and says, hello world
‫plus the environ variable custom name.
‫It will say hello world Stephane.
‫So that's perfect. It's been packaged.
‫Now I need to go one level up.
‫I will go into my template.yaml file
‫so I'm going to edit my template.yaml file.
‫And this time I'm going to update my application
‫and the semantic version is going to be 0.0.2.
‫Great.
‫Now we have to run the same command to package
‫our application so let's take it from here.
‫We're going to package the SAM package.
‫Let's try to find it in here
‫'cause we have the right bucket name.
‫So SAM package, here we go, and we get a new packaged file
‫and then a SAM publish command to publish our new version.
‫Okay, excellent so now the version 0.0.2 has been published
‫so let's go back into our serverless application repository.
‫I can go into my available applications
‫which are right here.
‫This one, my app, my app is now on version
‫so if we look at it, a 0.0.2 it's going to show
‫somewhere around here so if, version 0.0.2 here we go.
‫And I can just redeploy it so I'm going to deploy it
‫and this time I will say, Stephane deploy
‫and the deployment has now started.
‫So there is a new deployment, and this is an update
‫because we are using the same app name
‫and so it's using the same CloudFormation stack
‫so if we go into CloudFormation
‫now we are in update in progress
‫so we are updating our application.
‫And when the Lambda function has been deployed
‫when we go to this URL, it should say,
‫hello world Stephane.
‫So let's wait a little bit.
‫So my application is now updated and if I refresh I get
‫hello world Stephane so everything is working as expected
‫and this is amazing and I've been showing you as well
‫how to update a serverless application repository in SAR.
‫So to clean up this hands-on fairly easy
‫we just need to do it a few CloudFormation templates
‫and include some applications so to do so,
‫let's go into the Lambda console right here
‫and I'm going to go into the functions first of all
‫so it is going to the Lambda console here
‫and we're going to get our applications
‫and the two applications the hello world application
‫we need to go to the application stack
‫so let's go through the shortcut here.
‫So we're gonna go into CloudFormation.
‫We're going to delete this one
‫and we're going to delete that one as well
‫so both are going to be deleted directly
‫from CloudFormation.
‫CloudFormation is done
‫so I can go into serverless application
‫I can look at this application and I can delete it entirely
‫so to do so I'm gonna go into the published application
‫on the left hand side, click on this one, my app,
‫and then delete application
‫to delete the serverless application.
‫And finally, if you wanted to do a perfect cleanup
‫you would go into Amazon S3
‫we would delete everything that we don't need.
‫So we go into Amazon S3, we look at SAR,
‫here's my buckets.
‫As we can see, we have two deployment packages
‫so I can just delete this deployment packages
‫from my bucket so I'll type permanently delete in here
‫and then finally you could delete the bucket itself
‫so you go to the buckets in here and delete
‫and then I type its name, and I'm good to go.
‫So that's it for this demo on SAR.
‫We've seen how to deploy a public application
‫but also how to package and publish our own applications
‫and update them from the console.
‫So I hope you like this
‫and I will see you in the next lecture.