101 AWS Lambda

Rodrigo Ancavil
4 min readOct 11, 2019

--

First of all, select the button Create function and go to the options Author from scratch. We’ll develop a lambda function triggered by an API Getaway.

We’ll be able to execute the function calling a URL in API Rest fashion.

https://<url-amazon-domain>/<stage>/ms-echo?myname=Tommy

Go to the Basic Information section and complete Function name, for example, ms-echo.

Select the runtime to write the lambda function, in our case we are going to use Python 3.6.

In the section Permissions, we have to select a security role. We’ll set Create a new role with basic Lambda permissions, and push the button Create function, we have to wait for the creation of the function by AWS Lambda.

Once the process of creation finishes, we’ll see the console of our lambda function. To write the code of our function, we have to go to the editor in the Function code section.

Writing the function

In the Editor of Function code, we can write the lambda_handler with the following code.

import jsondef lambda_handler(event, context):
myname = event['queryStringParameters']['myname']
return {
'statusCode': 200,
'body': json.dumps({'hello' : myname})
}

The function receives a query string parameter called myname from a query string through the event parameter.

myname = event['queryStringParameters']['myname']

And, it will return the response using the lambda proxy integration format.

{
'statusCode': 200,
'body': json.dumps({'hello':myname})
}

myname depends on the value passed as a parameter.

Note: the format has the following structure.

{   
"isBase64Encoded" : "boolean",
"statusCode": "number",
"headers": { ... },
"body": "JSON string"
}

We’ll use only statusCode and body.

Testing the function

We can write a test event; push the button Test. The first time, we have to put a name (TestService) and write the test parameters in a JSON; this JSON will be passed as parameters through the event to the lambda_handler when we execute the test.

{
"queryStringParameters" : {"myname":"Tommy the cat"}
}

After writes the test event, we’ll execute it pushing the Test button.

As a result of test execution we’ll get:

Response:
{
"statusCode": 200,
"body": "{
\"hello\": \"Tommy the cat\"
}"
}

Trigger the function

Ready!, we already know how our lambda function is working, now we have to set a trigger to call and execute our lambda. We have a lot of options (API Getaway, AWS IoT, SQS, Kinesis, etc.), the decision depends on us and the characteristics of our lambda function. We’ll use the API Getaway option to trigger our function.

We have to choose the API Getaway trigger and go to the configuration options. We’ll go API textbox and select the option Create a new API. After that, we have to select the security mechanism, in our case, we could choose Open with API Key to protect the endpoint, ready? For now, we’ll leave the Additional setting by default, and push the button Add, and after the button Save. We have created the API Getaway ms-echo-API to trigger the lambda function; now we have to configure it, click on the ms-echo-API and we’ll go to the Amazon API Getaway console.

Configuring the API Getaway

In our AWS API Getaway console go to Method Request (click on) and click on URL Query String Parameters, we have to set the “myname” query string parameter, it is easy, just add query string and put the Name: myname and push the ticket icon at the end of the line.

We are ready to test our lambda function and API getaway. Execute Test, select the GET method, set the value to the query parameter (myname=Tommy the cat) and push the button Test, the result will appear aside.

Request: /ms-echo?myname=Tommy the cat
Status: 200
Latency: 24ms
ResponsBody:
{
"hello": "Tommy the cat"
}

Now, to test using an external tool, like curl or postman, we have to get the URL. To do this, we have to go Stages option in AWS Getaway console and select the GET method, we’ll see the Invoke URL:

https://21rrdpkfhd.execute-api.us-east-2.amazonaws.com/default/ms-echo

But, before we can execute the lambda function, using curl, we have to get the API Key; remember, we have protected our API with a key to control the access.

We have to go to API Keys option in the AWS API Getaway, select the API (ms-echo) and copy the API key (click on Show). After that, we can execute the curl command using the option to set the header x-api-key with our API Key.

$ curl -H "x-api-key: VgDPhdKETa7H9wXP1DtHi3Ty7dC8VAqSam1V4xjA" https://21rrddfg1hc.execute-api.us-east-2.amazonaws.com/default/ms-echo?myname=Johnny

As a result:

{"hello": "Johnny"}

Well, this is a basic recipe to create a simple lambda function triggered by an API Getaway. In AWS we’ll have a lot of options to create and integrate services and components in a Cloud Architecture, this is only a little and basic review.

--

--

Rodrigo Ancavil
Rodrigo Ancavil

Written by Rodrigo Ancavil

IT Architect and Software Engineer

No responses yet