Skip to content

Commit eec7f5d

Browse files
committed
First commit
1 parent feab261 commit eec7f5d

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

awslambdahttprequest/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# AWS Lambda Function Http Request Python example
2+
3+
This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services).
4+
5+
It handles an AWS Lambda function that it is invoked by an http request. It shows the parameters of the request and reponds a message including the parameters.
6+
7+
## Requirements
8+
9+
* You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account.
10+
11+
* The code was written for Python 3.
12+
13+
## Using the code
14+
15+
* Access the AWS console.
16+
17+
* Select AWS Lambda in the services menu.
18+
19+
* Create an AWS lambda function.
20+
* Name: `<LAMBDA_NAME>`
21+
* Runtime: `Python 3.8`
22+
* Handler: `lambda_function.lambda_handler`
23+
* Role: `lambda-basic-execution`
24+
* Runtime Settings for the lambda function:
25+
* Memory (MB): `128`
26+
* Timeout: `3 sec`
27+
* The resources that the function's role has access to:
28+
* `Amazon CloudWatch Logs`
29+
30+
* Write the code of the Lambda funtion.
31+
32+
The content of `lambda_function.py` file.
33+
34+
* Deploy the Lambda function.
35+
36+
It deploys the Lambda function to AWS.
37+
38+
* Create an `API Gateway` trigger.
39+
40+
This allows to call the lambda function using an HTTP API.
41+
42+
* Name: `<LAMBDA_NAME>-API`
43+
* API: `Create an API`
44+
* API type: `HTTP API`
45+
* Security: `Open`
46+
47+
You will get an API endpoint, which can be copied and run in your browser's address bar.
48+
49+
It looks like the following URL:
50+
51+
```bash
52+
https://<API_ID>.execute-api.<REGION>.amazonaws.com/<STAGE_NAME>/<LAMBDA_NAME>
53+
```
54+
55+
For example:
56+
57+
```bash
58+
https://abcdefg5jk.execute-api.eu-west-1.amazonaws.com/default/HttpRequestPython`
59+
```
60+
61+
* Run the code.
62+
63+
To run the code, you need to use 2 parameters:
64+
65+
* `first_name`
66+
* `last_name`
67+
68+
You call the API endpoint with this format:
69+
70+
```bash
71+
https://<API_ID>.execute-api.<REGION>.amazonaws.com/<STAGE_NAME>/<LAMBDA_NAME>?first_name=<FIRST_NAME>&last_name=<LAST_NAME>
72+
```
73+
74+
For example:
75+
76+
```bash
77+
https://abcdefg5jk.execute-api.eu-west-1.amazonaws.com/default/HttpRequestPython?first_name=Peter&last_name=Parker
78+
```
79+
80+
* Test the AWS Lambda function.
81+
82+
Go to the URL of API endpoint that you have got: `https://<API_ID>.execute-api.<REGION>.amazonaws.com/<STAGE_NAME>/<LAMBDA_NAME>?first_name=Peter&last_name=Parker` using a browser.
83+
84+
You should see the next response if you have added the right paramenters:
85+
86+
```bash
87+
"Hello Peter Parker!"
88+
```
89+
90+
You should see the next response if you do not have added any paramenter:
91+
92+
```bash
93+
"Who are you?"
94+
```
95+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# lamdda_function.py
2+
# It handles an AWS Lambda function that it is invoked by an http request.
3+
# It shows the parameters of the request and reponds a message including the parameters.
4+
5+
import json
6+
7+
def lambda_handler(event, context):
8+
if 'queryStringParameters' in event: # If parameters
9+
print(event['queryStringParameters']['first_name'])
10+
print(event['queryStringParameters']['last_name'])
11+
body = 'Hello {} {}!'.format(event['queryStringParameters']['first_name'],
12+
event['queryStringParameters']['last_name'])
13+
else: # If no parameters
14+
print('No parameters!')
15+
body = 'Who are you?'
16+
17+
return {
18+
'statusCode': 200,
19+
'body': json.dumps(body)
20+
}
21+
22+
# https://oesnotu7ne.execute-api.eu-west-1.amazonaws.com/default/HttpRequestPython?first_name=Peter&last_name=Parker
23+
24+
25+
{'version': '2.0', 'routeKey': 'GET /HttpRequestPython', 'rawPath': '/default/HttpRequestPython', 'rawQueryString': 'first_name=Peter&last_name=Parker', 'headers': {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3', 'cache-control': 'max-age=0', 'content-length': '0', 'host': 'oesnotu7ne.execute-api.eu-west-1.amazonaws.com', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0', 'x-amzn-trace-id': 'Root=1-60edae76-099272b12272fd32162fc1ee', 'x-forwarded-for': '87.125.176.151', 'x-forwarded-port': '443', 'x-forwarded-proto': 'https'}, 'queryStringParameters': {'first_name': 'Peter', 'last_name': 'Parker'}, 'requestContext': {'accountId': '617184998144', 'apiId': 'oesnotu7ne', 'domainName': 'oesnotu7ne.execute-api.eu-west-1.amazonaws.com', 'domainPrefix': 'oesnotu7ne', 'http': {'method': 'GET', 'path': '/default/HttpRequestPython', 'protocol': 'HTTP/1.1', 'sourceIp': '87.125.176.151', 'userAgent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0'}, 'requestId': 'CagyliOujoEEJmg=', 'routeKey': 'GET /HttpRequestPython', 'stage': 'default', 'time': '13/Jul/2021:15:17:10 +0000', 'timeEpoch': 1626189430751}, 'isBase64Encoded': False}

0 commit comments

Comments
 (0)