devops-exercises

Hello Function - Solution

Exercise

Create a basic AWS Lambda function that when given a name, will return “Hello "

Solution

Define a function

  1. Go to Lambda console panel and click on Create function
  2. Give the function a name like BasicFunction
  3. Select Python3 runtime
  4. Now to handle function’s permissions, we can attach IAM role to our function either by setting a role or creating a new role. I selected “Create a new role from AWS policy templates”
  5. In “Policy Templates” select “Simple Microservice Permissions”

  6. Next, you should see a text editor where you will insert a code similar to the following

Function’s code

import json


def lambda_handler(event, context):
    firstName = event['name']
    return 'Hello ' + firstName
  1. Click on “Create Function”

Define a test

  1. Now let’s test the function. Click on “Test”.
  2. Select “Create new test event”
  3. Set the “Event name” to whatever you’d like. For example “TestEvent”
  4. Provide keys to test
{
  "name": 'Spyro'
}
  1. Click on “Create”

Test the function

  1. Choose the test event you’ve create (TestEvent)
  2. Click on the Test button
  3. You should see something similar to Execution result: succeeded
  4. If you’ll go to AWS CloudWatch, you should see a related log stream