2

When you complete this tutorial https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html you download the AWS SAM CLI and run the commands in order to create a simple AWS hello-world application. When you run the program it triggers what AWS calls a lambda function and at the end of the tutorial you can open it in your browser in the url window using: http://127.0.0.1:3000/hello, if you see a message here that shows curly braces and the words 'hello-world' that means it is successful.

Running the AWS SAM commands generates a lot of boiler plate code which is a bit confusing. This can all be seen inside a code editor. One of them is called event.json, which of course is a JSON object but why is it there? what does it represent in relation to this program? I am trying to understand what this AWS SAM application is ultimately doing and what the files generated mean and represent here.

Can someone simply break down what AWS SAM is doing and the meaning behind the boiler plate code it generates?

Thank you

Guz_02
  • 41
  • 2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 29 '21 at 10:31

2 Answers2

3

event.json contain the input your lambda function will get in json format. Regardless of how a lambda is triggered, it will always have 2 fixed parameters: Event and Context. Context contains additional information about the trigger like source, while Event Contains any input parameters that your lambda needs to run.

You can test this out by yourself by editing the event.json and giving your own values. If you open the lambda code file you will see this event object being used in the lambda_handler.

Other boilerplate stuff is your template where you can define the configuration of your lambdas as well as any other services you might use like layers or a database or api gateway. You also get a requirements.txt file which contains names of any third party libraries that your function requires. These will be packaged along with the code.

Ninad Gaikwad
  • 4,272
  • 2
  • 13
  • 23
  • Thank you so much. If I understand correctly, a lambda function is event-driven programming. For example, if something happens such as an upload to S3 we could have a lambda function to do something when that happens. And a lambda always takes in two parameters, an event which is an object formatted from a JSON formatted string that presents the action/trigger (like an upload to S3). And context, which provides information about the invocation function and execution environment for the lambda, so you can use it to check things like memory allocation or speed of execution. Is this correct? – Guz_02 Dec 21 '21 at 04:52
  • Correct. Both Event and Context are dictionaries/objects. For s3 it will pass the key, versionID, bucket etc which you can use to get the object that triggered the lambda. – Ninad Gaikwad Dec 21 '21 at 12:02
  • Thanks! I see on my IDE it says that event is of type dict (JSON) and then says API Gateway Lambda proxy Input Format, and contaxt is of type object and then says runtime methods and attributes. I see there are many files such as: events, hello_world, and tests and inside of tests there is a integration and unit file as well as several __ init__.py files (5 of them) could you explain what all this is? – Guz_02 Dec 21 '21 at 15:12
  • __init__.py is more a convinience thing rather than something that is required by sam. You can get rid of it if you want. You can learn more here: https://stackoverflow.com/questions/448271/what-is-init-py-for Unit tests are also optional. You can define your own tests using pytest. It isn't specific to sam and is additional biolerplate stuff that you can get rid of if you want to. Or you can write your unit tests which will run before deploying your app. – Ninad Gaikwad Dec 22 '21 at 04:38
2

Ninad's answer is spot on. I just want to add a practical application of how these json files are used. One way the event.json is used is when you are invoking your lambdas using the command sam local invoke. When you are invoking the lambda locally, you pass the event.json (or what ever you decide to call the file, you will likely have multiples) as a parameter. As Ninad mentioned, the event file has everything your lambda needs to run in terms of input. When the lambdas are hooked up to other services and running live, these inputs would be being feed to your lambda from that service.

James Hill
  • 45
  • 6