AWS: Lambda functions  —  an overview, and integration with AWS API Gateway
Tags: aws, todayilearned, tutorial, devops
Published: 2021-08-18 10:24:34

---
title: AWS: Lambda functions  —  an overview, and integration with AWS API Gateway
published: true
date: 2021-08-18 05:56:20 UTC
tags: aws,todayilearned,tutorial,devops
canonical_url: https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/
---

### AWS: Lambda functions  —  an overview, and integration with AWS API Gateway

![](https://cdn-images-1.medium.com/max/365/1*6dXEkU-sfm3IrnC9WoL1QA.png)

[AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) allows running a code without the need to create and manage servers, also known as the _serverless approach._

AWS Lambda will determine how much CPU and memory is needed to run a function, and when it’s necessary it will perform autoscaling.

A code to be running is organized in _lambda functions_ and can be triggered with _triggers_. Results can be checked using CloudWatch Logs.

As a trigger, you can use almost any AWS service such as API Gateway, SQS, Application LoadBalancer, CloudFront, Kinesis, or an external event, for example, a webhook from Github.

In this post, we will create a simple AWS Lambda function, will check its control panel, available options, and features, and then will create an AWS API Gateway which will forward HTTP requests to an AWS Lambda function.

### Contents

- [AWS Lambda — use cases](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#AWS_Lambda_-_use_cases)
- [Components and concepts](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Components_and_concepts)
- [Creating a “Hello, World” Lambda function](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Creating_a_Hello_World_Lambda_function)
- [Create a function](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Create_a_function)
- [Monitoring](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Monitoring)
- [Configuration](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Configuration)
- [General configuration](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#General_configuration)
- [Triggers](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Triggers)
- [Permissions](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Permissions)
- [Destinations](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Destinations)
- [Environment variables](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Environment_variables)
- [VPC](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#VPC)
- [Monitoring and operations tools](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Monitoring_and_operations_tools)
- [Concurrency](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Concurrency)
- [Asynchronous invocation](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Asynchronous_invocation)
- [Database proxies](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Database_proxies)
- [File system](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#File_system)
- [Aliases](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Aliases)
- [Versions](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Versions)
- [AWS Lambda, and AWS API Gateway — an integration example](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#AWS_Lambda_and_AWS_API_Gateway_-_an_integration_example)
- [Create a Lambda function](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Create_a_Lambda_function)
- [Create an AWS API Gateway](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/#Create_an_AWS_API_Gateway)

### AWS Lambda — use cases

In general, Lambda in AWS can do really a lot of things. Such a “silver bullet”, allowing to do things that are not realized by the AWS Console itself.

Example use cases could be:

- a website: a javascript-based frontend in AWS S3 with Static hosting, frontend will receive requests via an AWS API Gateway to a database via a Lambda function
- log analyzes: a good example is [AWS WAF Security Automations](https://aws.amazon.com/ru/solutions/implementations/aws-waf-security-automations/), when all incoming HTTP requests are sent to the AWS Kinesis, it will forward them to the Lambda, it will perform some checks, and if needed will block a client’s IP
- backups automation: AWS SNS can send an event, for example when too much disk space is used in an AWS S3 bucket, to a Lambda function, that will delete some old backups
- data processing: for example, when a new file is uploaded to an AWS S3, it will generate an event that will trigger a Lambda function that will perform a video file encoding
- serverless cronjobs: with CloudWatch Events generate an event by a schedule, that will trigger a Lambda function

### Components and concepts

Let’s take a short overview of the AWS Lambda main concepts:

- **_function_** _:_ is a code to run in a function. Can be as a zip file or a Docker image (_deployment package)._ See [Configuring AWS Lambda functions](https://docs.aws.amazon.com/lambda/latest/dg/lambda-functions.html).
- **_trigger_** : an AWS resource that will trigger a function. Such an event includes an AWS service and _event mapping_. See [Invoking AWS Lambda functions](https://docs.aws.amazon.com/lambda/latest/dg/lambda-invocation.html) и [Using AWS Lambda with other services](https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html).
- **_event_** : a JSON object that contains data for a Lambda function for processing
- **_execution environment_** _:_ secured environment to execute a function. See [AWS Lambda execution environment](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html).
- **_deployment package_** _:_ a Lambda function code to be running. Can be as a zip archive or a Docker image. See [Lambda deployment packages](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-package.html).
- **_runtime_** : a working environment to run a function. See [Lambda runtimes](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html)
- **_layer_** : a zip archive with additional code to run a function, for example — external library. See [Creating and sharing Lambda layers](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html).
- **_extensions_** : AWS Lambda allows to use of extensions that can be used to integrate a function with external services such as monitoring. See [Using Lambda extensions](https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html).
- **_concurrency_** : a number of a Lambda function instances to be running at the same time to process incoming data. See [Managing concurrency for a Lambda function](https://docs.aws.amazon.com/lambda/latest/dg/configuration-concurrency.html).
- **_qualifier_** : a “pointer” to a version or an alias. See [Lambda function versions](https://docs.aws.amazon.com/lambda/latest/dg/configuration-versions.html).
- **_destination_** : an AWS resource where a function will send processed data. See [Configuring destinations for asynchronous invocation](https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html#invocation-async-destinations).

### Creating a “Hello, World” Lambda function

At first, let’s create the simplest Lambda function to see how it’s working and what do we have there.

#### Create a function

Go to the AWS Lambda, click on the _Create function_:

![](https://cdn-images-1.medium.com/max/1024/0*v9Y6u3Mv_P4mhKZ4.png)

For now, let’s use an existing template. Choose the _Use a blueprint_, find a _hello-world-python_:

![](https://cdn-images-1.medium.com/max/961/0*Hh1Mg9nOM9mHiXT_.png)

Click on the _Configure_:

![](https://cdn-images-1.medium.com/max/1024/0*CQtYQ7ikSUeiuHd1.png)

Set a function’s name, for example, _example-hello_, leave a default IAM role — it will allow our function to use CloudWatch Logs, and check the code that will be used:

![](https://cdn-images-1.medium.com/max/885/0*IRcu5HPJGyEI4kmP.png)

Click on the _Create function_:

![](https://cdn-images-1.medium.com/max/1024/0*9Ykanr-0rSlSLRrF.png)

Switch to the _Test_ tab:

![](https://cdn-images-1.medium.com/max/1024/0*9WeMBmBg_f_XWxut.png)

Here, we can pass a JSON with data to be processed by our function.

Run it:

![](https://cdn-images-1.medium.com/max/1024/0*lYtlS0Jp9ol2QgT7.png)

Now, let’s go to see what AWS Console suggests to use for Lambda function management.

#### Monitoring

The first thing is monitoring. Here we can use AWS CloudWatch metrics and logs, calls tracing with AWS X-Ray, Lambda insights, and AWS CodeGuru:

![](https://cdn-images-1.medium.com/max/1024/0*ZD5eUg8atqb6b195.png)

#### Configuration

#### General configuration

- memory settings: a maximum RAM allowed to be used when running a function. Also, depending on the memory settings, Lambda will provide a CPU limit: for every 1769 MB, a one vCPU will be. See [Configuring function memory (console)](https://docs.aws.amazon.com/lambda/latest/dg/configuration-function-common.html#configuration-memory-console).
- execution timeouts: maximum can be set to 900 seconds, after that, a function execution will be stopped. Remember, that this will affect costs. See [Timeout](https://docs.aws.amazon.com/whitepapers/latest/serverless-architectures-lambda/timeout.html).
- IAM role: includes IAM policies with permissions to AWS resources

![](https://cdn-images-1.medium.com/max/892/0*_75cHkiYPIgjtsTY.png)

#### Triggers

Well, triggers that will trigger our function.

Can be almost any AWS service:

![](https://cdn-images-1.medium.com/max/892/0*zm7yg-NaeoAJ4P9S.png)

For example, we can create a trigger from an AWS Application LoadBalancer that will accept connections to a specific URI and will forward it to a Lambda function:

![](https://cdn-images-1.medium.com/max/836/0*97y7G5nBBWO4DCSm.png)

#### Permissions

Here, you can view and adjust IAM role and policies that will configure a function’s permissions:

![](https://cdn-images-1.medium.com/max/1024/0*FLBEiTI1yy4yfAI0.png)

#### Destinations

Where to send a function’s execution results.

For example, can be an AWS SNS topic that will forward it to Opsgenie that will send a message to a Slack channel:

![](https://cdn-images-1.medium.com/max/887/0*PkgSc8u6zxJ1Ez7h.png)

#### Environment variables

Variables that can be used in our function. Sensitive data can be encrypted with AWS Key Management Service (KMS):

![](https://cdn-images-1.medium.com/max/890/0*MiNHMGUMn6IZO-51.png)

#### VPC

A function can be placed to a dedicated AWS Virtual Private Cloud to limit its network access:

![](https://cdn-images-1.medium.com/max/890/0*msnk5vkRv8nRLq_K.png)

#### Monitoring and operations tools

Monitoring settings, where you can enable or disable additional services such as AWS X-Ray, CloudWatch Lambda Insights, and Amazon CodeGuru Profiler:

![](https://cdn-images-1.medium.com/max/904/0*2uFHihqpyeS9kXnD.png)

In the _Extentions,_ you can choose from a list of already existing solutions or create your own:

![](https://cdn-images-1.medium.com/max/1024/0*kLKO_248Kc13Pj8T.png)

#### Concurrency

A maximum number of a function’s instances that can be run simultaneously. See [Managing concurrency for a Lambda function](https://docs.aws.amazon.com/lambda/latest/dg/configuration-concurrency.html).

Can be one of the two types:

- **_reserved concurrency_** : reserve a number from the generally available for the whole account (1000 by default) and is applied to the whole function including all its versions and aliases
- **_provisioned concurrency_** : will prepare an already initialized runtime environments so during scaling your function will not have to wait for it, applies to a specific version or an alias

![](https://cdn-images-1.medium.com/max/1024/0*BDtz6bObh0E3lA9W.png)

#### Asynchronous invocation

Settings for the event queue — lifetime, number of retries in case of errors, errors notifications, etc.

See [Asynchronous invocation](https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html?icmpid=docs_lambda_help):

![](https://cdn-images-1.medium.com/max/831/0*QIhHI2Ih8Ev7ZYaN.png)

See also [Synchronous invocation](https://docs.aws.amazon.com/lambda/latest/dg/invocation-sync.html) and [Asynchronous invocation](https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html).

#### Database proxies

An [Amazon RDS Proxy](https://aws.amazon.com/ru/rds/proxy/) configuration to be used with your function. RDS Proxy used to decrease a number of connections to a database server:

![](https://cdn-images-1.medium.com/max/831/0*6auM_fX1glBwqrfZ.png)

#### File system

You can mount an AWS Elastic File System directory inside your function:

![](https://cdn-images-1.medium.com/max/848/0*DIG93NK9Ek9xQ9Lk.png)

#### Aliases

An alias is a kind of pointer to a specific version of your function’s code that can be used in its ARN later.

Also, you can have a couple of aliases and distribute requests between them. See [Lambda function aliases](https://docs.aws.amazon.com/lambda/latest/dg/configuration-aliases.html):

![](https://cdn-images-1.medium.com/max/839/0*09jcm3by31_EEhsJ.png)

#### Versions

AWS Lambda allows using a code and part of settings versioning. Can be useful when testing a new code on a Dev environment, for example by creating a dedicated _alias_:

![](https://cdn-images-1.medium.com/max/1024/0*kDZ7jfu2Lwbv4UGU.png)

### AWS Lambda, and AWS API Gateway — an integration example

So, we’ve checked what AWS Lambda is and what it has in its settings.

Now, let’s create an AWS API Gateway that will forward requests to an AWS Lambda function.

API Gateway will accept requests to the _/test_ URI and will use its event to send them to our Lambda.

#### Create a Lambda function

At this time, choose _Author from scratch_, in the Runtime use _Python_:

![](https://cdn-images-1.medium.com/max/1024/0*uJUgjqlWjO2pmepc.png)

Leave the default code:

![](https://cdn-images-1.medium.com/max/1024/0*LoCxT1PgOOUgnMhp.png)

Here, the `lambda_handler()` is a default function to be called when a function is called. It accepts two arguments:

1. `event`: an API Gateway event, see [Using AWS Lambda with other services](https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html)
2. `context`: allowed methods and parameters to run a function, see [AWS Lambda context object in Python](https://docs.aws.amazon.com/lambda/latest/dg/python-context.html)

#### Create an AWS API Gateway

Create a new gateway, set its type to the _HTTP API_:

![](https://cdn-images-1.medium.com/max/1019/0*Z4fkGqy8SifHIWSC.png)

Add an integration:

![](https://cdn-images-1.medium.com/max/1012/0*jPZAoxwODR_9qqKl.png)

Choose Lambda, an AWS Region, and a function to be called:

![](https://cdn-images-1.medium.com/max/1024/0*a4Kjp214dLzpWgpA.png)

Set the URI as _/test_:

![](https://cdn-images-1.medium.com/max/1024/0*7oqKFxsZ-q4hTxu6.png)

Leave the default [stage](https://docs.aws.amazon.com/apigateway/latest/developerguide/stages.html):

![](https://cdn-images-1.medium.com/max/1024/0*798HA4_2vOxnHo4r.png)

In less than a minute your Gateway is ready. Copy its URL:

![](https://cdn-images-1.medium.com/max/1024/0*3WMtdCNiUKAl-mHO.png)

And try with the `curl`:

```
$ curl [https://fwu399qo70.execute-api.us-east-2.amazonaws.com/test](https://fwu399qo70.execute-api.us-east-2.amazonaws.com/test)
“Hello from Lambda!”
```

To check the full content of the event, print it with the [`json.dumps()`](https://docs.python.org/3/library/json.html):

```
import json

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps(event)
    }
```

After making a code changes, press on the _Deploy_:

![](https://cdn-images-1.medium.com/max/589/0*5tl_XBNqVpoh1dFx.png)

Try it:

```
$ curl [https://fwu399qo70.execute-api.us-east-2.amazonaws.com/test](https://fwu399qo70.execute-api.us-east-2.amazonaws.com/test)
{“version”: “2.0”, “routeKey”: “ANY /test”, “rawPath”: “/test”, “rawQueryString”: “”, “headers”: {“accept”: “*/*”, “content-length”: “0”, “host”: “fwu399qo70.execute-api.us-east-2.amazonaws.com”, “user-agent”: “curl/7.78.0”, “x-amzn-trace-id”: “Root=1–611bbaa4–3cb7c28e4e3181dd647f1030”, “x-forwarded-for”: “194. ***.***.29”, “x-forwarded-port”: “443”, “x-forwarded-proto”: “https”}, “requestContext”: {“accountId”: “534 ***385”, “apiId”: “fwu399qo70”, “domainName”: “fwu399qo70.execute-api.us-east-2.amazonaws.com”, “domainPrefix”: “fwu399qo70”, “http”: {“method”: “GET”, “path”: “/test”, “protocol”: “HTTP/1.1”, “sourceIp”: “194.***.***.29”, “userAgent”: “curl/7.78.0”}, “requestId”: “ENoZriIrCYcEPWg=”, “routeKey”: “ANY /test”, “stage”: “$default”, “time”: “17/Aug/2021:13:33:24 +0000”, “timeEpoch”: 1629207204179}, “isBase64Encoded”: false}
```

Now, let’s see how we can use environment variables in the function.

Add a new one:

![](https://cdn-images-1.medium.com/max/857/0*IOBmNPAkNV-OPplT.png)

And print its value with the `os.getenv()`:

```
import os
import json

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': os.getenv('Env')
    }
```

Check it:

```
$ curl [https://fwu399qo70.execute-api.us-east-2.amazonaws.com/test](https://fwu399qo70.execute-api.us-east-2.amazonaws.com/test)
test
```

Also, you can change the default handler.

Rename the `lambda_handler` to the `main_handler`:

```
import os
import json

def main_handler(event, context):
    return {
        'statusCode': 200,
        'body': os.getenv('Env')
    }
```

If you’ll try to access the function now, you’ll get the **_Internal Server Error_** :

```
$ curl [https://fwu399qo70.execute-api.us-east-2.amazonaws.com/test](https://fwu399qo70.execute-api.us-east-2.amazonaws.com/test)
{“message”:”Internal Server Error”}
```

Scroll down to the _Runtime settings_:

![](https://cdn-images-1.medium.com/max/1024/0*sDfO2UUC15Mm3GGA.png)

And change the _Handler_:

![](https://cdn-images-1.medium.com/max/860/0*dzg6Aek33qJQ_MhH.png)

Run it again:

```
$ curl [https://fwu399qo70.execute-api.us-east-2.amazonaws.com/test](https://fwu399qo70.execute-api.us-east-2.amazonaws.com/test)
test
```

Done.

_Originally published at_ [_RTFM: Linux, DevOps, and system administration_](https://rtfm.co.ua/en/aws-lambda-functions-and-overview-and-integration-with-aws-api-gateway/)_._

* * *