Skip to content

Commit 6ef0a80

Browse files
authored
add sample code for lambda function with fuction url (#136)
* add sample code for lambda function with fuction url * add a makefile
1 parent a212df1 commit 6ef0a80

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed

lambda-function-urls/Makefile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export AWS_ACCESS_KEY_ID ?= test
2+
export AWS_SECRET_ACCESS_KEY ?= test
3+
export AWS_DEFAULT_REGION = us-east-1
4+
5+
usage: ## Show this help
6+
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
7+
8+
install: ## Install dependencies
9+
@which awslocal || pip install awscli-local
10+
11+
run: ## Build, deploy, and invoke the Lambda function URL
12+
npm i -g zip
13+
zip function.zip index.js
14+
awslocal lambda create-function \
15+
--function-name localstack-lamba-url-example \
16+
--runtime nodejs14.x \
17+
--zip-file fileb://function.zip \
18+
--handler index.handler \
19+
--role cool-stacklifter
20+
21+
awslocal lambda create-function-url-config \
22+
--function-name localstack-lamba-url-example \
23+
--auth-type NONE
24+
25+
start:
26+
localstack start -d
27+
28+
stop:
29+
@echo
30+
localstack stop
31+
32+
ready:
33+
@echo Waiting on the LocalStack container...
34+
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)
35+
36+
logs:
37+
@localstack logs > logs.txt
38+
39+
test-ci:
40+
make start install ready run; return_code=`echo $$?`;\
41+
make logs; make stop; exit $$return_code;
42+
43+
.PHONY: usage install start run stop ready logs test-ci

lambda-function-urls/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Creating a Lambda function with a function URL
2+
3+
In this example, we will demonstrate how to create a Lambda function with a function URL. With the Function URL property, there is now a new way to call a Lambda Function via HTTP API call.
4+
5+
## Prerequisites
6+
7+
* LocalStack
8+
* Docker
9+
* `awslocal` CLI
10+
* Terraform
11+
12+
## Starting up
13+
14+
Start LocalStack via:
15+
16+
```sh
17+
localstack start -d
18+
```
19+
20+
Push the following command to deploy the Lambda function:
21+
22+
```sh
23+
awslocal lambda create-function \
24+
--function-name localstack-lamba-url-example \
25+
--runtime nodejs14.x \
26+
--zip-file fileb://function.zip \
27+
--handler index.handler \
28+
--role cool-stacklifter
29+
```
30+
31+
## Creating a Lambda function URL
32+
33+
With the Function URL property, there is now a new way to call a Lambda Function via HTTP API call using the `create-function-url-config` command.
34+
35+
```sh
36+
awslocal lambda create-function-url-config \
37+
--function-name localstack-lamba-url-example \
38+
--auth-type NONE
39+
```
40+
41+
You will retrieve a HTTP URL that you can use to invoke the Lambda function, in the form of `http://abcdefgh.lambda-url.us-east-1.localhost.localstack.cloud:4566`.
42+
43+
You can now trigger the Lambda function by sending a HTTP POST request to the URL using `curl`:
44+
45+
```sh
46+
curl -X POST \
47+
'http://abcdefgh.lambda-url.us-east-1.localhost.localstack.cloud:4566/' \
48+
-H 'Content-Type: application/json' \
49+
-d '{"num1": "10", "num2": "10"}'
50+
```
51+
52+
The following output would be retrieved:
53+
54+
```sh
55+
The product of 10 and 10 is 100%
56+
```
57+
58+
## Using Terraform
59+
60+
You can use Terraform to automate the creation of Lambda function and to create a function URL. Run the following commands on your terminal to create the Lambda function and the function URL:
61+
62+
```sh
63+
terraform init
64+
terraform plan
65+
terraform apply --auto-approve
66+
```
67+
68+
Since we are using LocalStack, no actual AWS resources will be created. Instead, LocalStack will create ephemeral development resources, which will automatically be cleaned once you stop LocalStack (using `localstack stop`).

lambda-function-urls/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
exports.handler = async (event) => {
2+
let body = JSON.parse(event.body)
3+
const product = body.num1 * body.num2;
4+
const response = {
5+
statusCode: 200,
6+
body: "The product of " + body.num1 + " and " + body.num2 + " is " + product,
7+
};
8+
return response;
9+
};

lambda-function-urls/main.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
data "aws_region" "current" {}
2+
3+
resource "aws_lambda_function" "example_lambda" {
4+
filename = "function.zip"
5+
function_name = "localstack-lamba-url-example"
6+
role = "cool-stacklifter"
7+
handler = "index.handler"
8+
source_code_hash = filebase64sha256("function.zip")
9+
runtime = "nodejs14.x"
10+
}
11+
12+
resource "aws_lambda_function_url" "lambda_function_url" {
13+
function_name = aws_lambda_function.example_lambda.arn
14+
authorization_type = "NONE"
15+
}
16+
17+
output "function_url" {
18+
description = "Function URL."
19+
value = aws_lambda_function_url.lambda_function_url.function_url
20+
}

lambda-function-urls/provider.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
access_key = "fake"
4+
secret_key = "fake"
5+
skip_credentials_validation = true
6+
skip_metadata_api_check = true
7+
skip_requesting_account_id = true
8+
s3_force_path_style = true
9+
10+
endpoints {
11+
apigateway = "http://localhost:4566"
12+
apigatewayv2 = "http://localhost:4566"
13+
cloudformation = "http://localhost:4566"
14+
cloudwatch = "http://localhost:4566"
15+
cognitoidp = "http://localhost:4566"
16+
cognitosync = "http://localhost:4566"
17+
cognitoidentity = "http://localhost:4566"
18+
dynamodb = "http://localhost:4566"
19+
ec2 = "http://localhost:4566"
20+
es = "http://localhost:4566"
21+
elasticache = "http://localhost:4566"
22+
firehose = "http://localhost:4566"
23+
iam = "http://localhost:4566"
24+
kinesis = "http://localhost:4566"
25+
lambda = "http://localhost:4566"
26+
rds = "http://localhost:4566"
27+
redshift = "http://localhost:4566"
28+
route53 = "http://localhost:4566"
29+
s3 = "http://localhost:4566"
30+
secretsmanager = "http://localhost:4566"
31+
ses = "http://localhost:4566"
32+
sns = "http://localhost:4566"
33+
sqs = "http://localhost:4566"
34+
ssm = "http://localhost:4566"
35+
stepfunctions = "http://localhost:4566"
36+
sts = "http://localhost:4566"
37+
}
38+
39+
default_tags {
40+
tags = {
41+
Environment = "Local"
42+
Service = "LocalStack"
43+
}
44+
}
45+
}
46+
47+
terraform {
48+
# The configuration for this backend will be filled in by Terragrunt or via a backend.hcl file. See
49+
# https://www.terraform.io/docs/backends/config.html#partial-configuration
50+
# backend "s3" {}
51+
52+
# Only allow this Terraform version. Note that if you upgrade to a newer version, Terraform won't allow you to use an
53+
# older version, so when you upgrade, you should upgrade everyone on your team and your CI servers all at once.
54+
required_version = "= 1.2.8"
55+
56+
required_providers {
57+
aws = {
58+
source = "hashicorp/aws"
59+
version = "~> 4.9.0"
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)