|
| 1 | +## URL Function |
| 2 | + |
| 3 | +Create a basic AWS Lambda function that will be triggered when you enter a URL in the browser |
| 4 | + |
| 5 | +### Solution |
| 6 | + |
| 7 | +#### Define a function |
| 8 | + |
| 9 | +1. Go to Lambda console panel and click on `Create function` |
| 10 | + 1. Give the function a name like `urlFunction` |
| 11 | + 2. Select `Python3` runtime |
| 12 | + 3. 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" |
| 13 | + 4. In "Policy Templates" select "Simple Microservice Permissions" |
| 14 | + |
| 15 | +1. Next, you should see a text editor where you will insert a code similar to the following |
| 16 | + |
| 17 | +#### Function's code |
| 18 | +``` |
| 19 | +import json |
| 20 | +
|
| 21 | +
|
| 22 | +def lambda_handler(event, context): |
| 23 | + firstName = event['name'] |
| 24 | + return 'Hello ' + firstName |
| 25 | +``` |
| 26 | +2. Click on "Create Function" |
| 27 | + |
| 28 | +#### Define a test |
| 29 | + |
| 30 | +1. Now let's test the function. Click on "Test". |
| 31 | +2. Select "Create new test event" |
| 32 | +3. Set the "Event name" to whatever you'd like. For example "TestEvent" |
| 33 | +4. Provide keys to test |
| 34 | + |
| 35 | +``` |
| 36 | +{ |
| 37 | + "name": 'Spyro' |
| 38 | +} |
| 39 | +``` |
| 40 | +5. Click on "Create" |
| 41 | + |
| 42 | +#### Test the function |
| 43 | + |
| 44 | +1. Choose the test event you've create (`TestEvent`) |
| 45 | +2. Click on the `Test` button |
| 46 | +3. You should see something similar to `Execution result: succeeded` |
| 47 | +4. If you'll go to AWS CloudWatch, you should see a related log stream |
| 48 | + |
| 49 | +#### Define a trigger |
| 50 | + |
| 51 | +We'll define a trigger in order to trigger the function when inserting the URL in the browser |
| 52 | + |
| 53 | +1. Go to "API Gateway console" and click on "New API Option" |
| 54 | +2. Insert the API name, description and click on "Create" |
| 55 | +3. Click on Action -> Create Resource |
| 56 | +4. Insert resource name and path (e.g. the path can be /hello) and click on "Create Resource" |
| 57 | +5. Select the resource we've created and click on "Create Method" |
| 58 | +6. For "integration type" choose "Lambda Function" and insert the lambda function name we've given to the function we previously created. Make sure to also use the same region |
| 59 | +7. Confirm settings and any required permissions |
| 60 | +8. Now click again on the resource and modify "Body Mapping Templates" so the template includes this: |
| 61 | + |
| 62 | +``` |
| 63 | +{ "name": "$input.params('name')" } |
| 64 | +``` |
| 65 | +9. Finally save and click on Actions -> Deploy API |
| 66 | + |
| 67 | +#### Running the function |
| 68 | + |
| 69 | +1. In the API Gateway console, in stages menu, select the API we've created and click on the GET option |
| 70 | +2. You'll see an invoke URL you can click on. You might have to modify it to include the input so it looks similar to this: `.../hello?name=mario` |
| 71 | +3. You should see in your browser `Hello Mario` |
0 commit comments