we create a GET method on a resource specified by a sequence of path parameters to call the backend Lambda function. The path parameter values specify the input data to the Lambda function
[root@ip-172-31-82-149 ~]# ls amazing-api anaconda-ks.cfg original-ks.cfg [root@ip-172-31-82-149 ~]# cd amazing-api/ [root@ip-172-31-82-149 amazing-api]# ls handler.py serverless.yml [root@ip-172-31-82-149 amazing-api]# export AWS_ACCESS_KEY_ID=AKIAWSYHFCM3Y6ZLJWN6 [root@ip-172-31-82-149 amazing-api]# export AWS_SECRET_ACCESS_KEY=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 [root@ip-172-31-82-149 amazing-api]# serverless deploy Serverless: Packaging service... Serverless: Excluding development dependencies... Serverless: Creating Stack... Serverless: Checking Stack create progress... ..... Serverless: Stack create finished... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading artifacts... Serverless: Uploading service amazing-api.zip file to S3 (314 B)... Serverless: Validating template... Serverless: Updating Stack... Serverless: Checking Stack update progress... ............................... Serverless: Stack update finished... Service Information service: amazing-api stage: dev region: us-east-1 stack: amazing-api-dev resources: 10 api keys: None endpoints: GET - https://da3w2i5gj0.execute-api.us-east-1.amazonaws.com/dev/results functions: hello: amazing-api-dev-hello layers: None Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing. [root@ip-172-31-82-149 amazing-api]#
[root@ip-172-31-82-149 ~]# ls amazing-api anaconda-ks.cfg original-ks.cfg [root@ip-172-31-82-149 ~]# cd amazing-api/ [root@ip-172-31-82-149 amazing-api]# ls handler.py serverless.yml [root@ip-172-31-82-149 amazing-api]# export AWS_ACCESS_KEY_ID=AKIAWSYHFCM3Y6ZLJWN6 [root@ip-172-31-82-149 amazing-api]# export AWS_SECRET_ACCESS_KEY=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 [root@ip-172-31-82-149 amazing-api]# serverless deploy Serverless: Packaging service... Serverless: Excluding development dependencies... Serverless: Creating Stack... Serverless: Checking Stack create progress... ..... Serverless: Stack create finished... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading artifacts... Serverless: Uploading service amazing-api.zip file to S3 (314 B)... Serverless: Validating template... Serverless: Updating Stack... Serverless: Checking Stack update progress... ............................... Serverless: Stack update finished... Service Information service: amazing-api stage: dev region: us-east-1 stack: amazing-api-dev resources: 10 api keys: None endpoints: GET - https://da3w2i5gj0.execute-api.us-east-1.amazonaws.com/dev/results functions: hello: amazing-api-dev-hello layers: None Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing. [root@ip-172-31-82-149 amazing-api]#
[root@ip-172-31-82-149 amazing-api]# vi handler.py
[root@ip-172-31-82-149 amazing-api]# cat handler.py
import json
def hello(event, context):
if event['httpMethod']=='GET' and event['queryStringParameters']['query']:
body = {
"message": 'Your Query is' +event['queryStringParameters']['query']
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
return response
[root@ip-172-31-82-149 amazing-api]#serverless deploy
https://da3w2i5gj0.execute-api.us-east-1.amazonaws.com/dev/results?query=india
Enter this in URL query=india
Now to use post method do the below changes
[root@ip-172-31-82-149 amazing-api]# vi serverless.yml
functions:
hello:
handler: handler.hello
events:
- http:
path: results
method: get
- http:
path: query
method: post
[root@ip-172-31-82-149 amazing-api]# vi handler.py
[root@ip-172-31-82-149 amazing-api]# cat handler.py
import json
def hello(event, context):
if event['httpMethod']=='GET' and event['queryStringParameters']['query']:
body = {
"message": 'Your Query is' +event['queryStringParameters']['query']
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
if event['httpMethod']=='POST' and event['body']:
body = {
"message": 'received your body text', 'msgBody':event['body']
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
return response
[root@ip-172-31-82-149 amazing-api]#serverless deploy
curl -d '{"Country":"India"} -H "Content-Type: application/json" -x POST https://da3w2i5gj0.execute-api.us-east-1.amazonaws.com/dev/query
No comments:
Post a Comment