首页 > 其他分享 >[975] Creating a POSTMAN Api | AWS API Gateway Passing Data to AWS Lambda

[975] Creating a POSTMAN Api | AWS API Gateway Passing Data to AWS Lambda

时间:2024-02-20 10:46:14浏览次数:36  
标签:Body 975 Creating Country AWS Param China event Name

ref: AWS Lambda Function URLs

ref: Guide to AWS Lambda Function URLs


1. Create a Lambda Function

aws -> Services -> Lambda

2. Create a Function URL

Configuration -> Function URL

Then, the Function URL is generated and can be seen from the upper right corner. Copy it!

3. Create a test POST in Postman

Paste the Function URL in the POST URL area and then click the Savebutton.

Click the Send button, then the test results will be shown below.

4. Add information in Params, Headers, and Body

5. Modify the lambda_function and show details of "event"

Test the lambda_function from the test case of Postman. The return information is show as below.

The details of output are as follows.

{
    "event": {
        "headers": {
            "content-length": "59",
            "x-amzn-tls-version": "TLSv1.2",
            "x-forwarded-proto": "https",
            "postman-token": "0f337b21-f35a-4b63-9904-423aea79304a",
            "x-forwarded-port": "443",
            "x-forwarded-for": "54.86.50.139",
            "header_name": "Alex",
            "accept": "*/*",
            "header_country": "China",
            "x-amzn-tls-cipher-suite": "ECDHE-RSA-AES128-GCM-SHA256",
            "x-amzn-trace-id": "Root=1-65d3f694-5a37ccac4de9141818821224",
            "host": "as2yuw3zjpqcankr7k4eteumme0bmnbz.lambda-url.ap-southeast-2.on.aws",
            "content-type": "application/json",
            "cache-control": "no-cache",
            "accept-encoding": "gzip, deflate, br",
            "user-agent": "PostmanRuntime/7.36.3"
        },
        "isBase64Encoded": false,
        "rawPath": "/",
        "routeKey": "$default",
        "requestContext": {
            "accountId": "anonymous",
            "timeEpoch": 1708390036365,
            "routeKey": "$default",
            "stage": "$default",
            "domainPrefix": "as2yuw3zjpqcankr7k4eteumme0bmnbz",
            "requestId": "62ac7803-5d6b-4208-a8e9-c74647caa7a6",
            "domainName": "as2yuw3zjpqcankr7k4eteumme0bmnbz.lambda-url.ap-southeast-2.on.aws",
            "http": {
                "path": "/",
                "protocol": "HTTP/1.1",
                "method": "POST",
                "sourceIp": "54.86.50.139",
                "userAgent": "PostmanRuntime/7.36.3"
            },
            "time": "20/Feb/2024:00:47:16 +0000",
            "apiId": "as2yuw3zjpqcankr7k4eteumme0bmnbz"
        },
        "queryStringParameters": {
            "Param_Country": "China",
            "Param_Name": "Alex"
        },
        "body": "{\r\n    \"Body_Name\": \"Alex\",\r\n    \"Body_Country\": \"China\"\r\n}",
        "version": "2.0",
        "rawQueryString": "Param_Name=Alex&Param_Country=China"
    }
}

From the lambda_function, we can extract detail values. Specifically, for the key of body, the value of it is the type of string, we should convert it to the JSON format. Using json.loads() function can extract the JSON file from the string.

6. Extract specific values from "event"

 Get the values from the above JSON file in the lambda_function.

import json

def lambda_handler(event, context):
    return {
        # Headers
        "Header_Name": event["headers"]["header_name"],
        "Header_Country": event["headers"]["header_country"],
        
        # Params
        "Param_Name": event["queryStringParameters"]["Param_Name"],
        "Param_Country": event["queryStringParameters"]["Param_Country"],
        
        # Body
        "Body_Name": json.loads(event["body"])["Body_Name"],
        "Body_Country": json.loads(event["body"])["Body_Country"],
        
        # "POST" or "GET"
        "Type": event["requestContext"]["http"]["method"]
    }

It will shows like this.

{
    "Header_Name": "Alex",
    "Type": "POST",
    "Param_Country": "China",
    "Body_Country": "China",
    "Body_Name": "Alex",
    "Header_Country": "China",
    "Param_Name": "Alex"
}

 

标签:Body,975,Creating,Country,AWS,Param,China,event,Name
From: https://www.cnblogs.com/alex-bn-lee/p/18022569

相关文章

  • AWS Migration产品(MGN)
    AWS把迁移服务分为“评估”、“动员和准备”、“迁移”、“应用现代化”四个模块,每个模块下都对应着一个或者多个的AWS产品。评估Well-ArchitectedFrameworkMigrationAssessmentTools(CART,MRA)AWSMigrationEvaluatorMigrationportfolioassessment动员和准备......
  • [刷题笔记] P9751 [CSP-J 2023] 旅游巴士
    Problem_LinkDescription给定一个\(n\)个点,\(m\)条边的有向图。起点为\(1\),终点为\(n\)。起始时间和终止时间必须是\(k\)的倍数。通过每条边的时间为\(1\)。每条边有限制\(a_i\)即若通过当前边必须满足当前时间\(t\geqa_i\)。求满足上述限制的前提下,到达终点的最小......
  • AWS Security Group Rule的限制问题
    需要维护一批IP白名单,一个个添加到securitygroup费时,而且以后还有其它机器和服务也需要用到,因此创建了一个Prefixlist(VPC-->Managedprefixlists)里面包含77个ip完成后尝试添加到EC2的securitygroup,却提示说:Themaximumnumberofrulespersecuritygrouphasbeenrea......
  • aws ebs 报无权限错误
    awseks 创建ebs的pvc时报以下错误:WarningProvisioningFailed9m28sebs.csi.aws.com_ebs-csi-controller-79d7954f96-thd4s_9a0470ef-ef71-4c55-8a8c-2be08fdc17d9failedtoprovisionvolumewithStorageClass"gp2":rpcerror:code=Internaldesc=Couldnotc......
  • 将EC2转移到另一个AWS帐号
    1.-创建EC2AMI2.-创建好后选择EditAMIPermission添加需要共享的AWS帐号3.-登入对应帐号AWSconsole界面进入AMI,选择Privateimages即可看到......
  • 强大的AWS lambda
    AWS强大的lambda自从几年前换工作后,我所参与的项目一直都是基于AWS云服务的架构,我慢慢对serverless的相关基础建设有了一定了解和实践经验。其中lambda是我心中最强大的serveless服务,是很好的高并发业务解决方案。什么是lambdalambda是AWS提供的一种无服务器的计算服务,根据请......
  • AWS ECS + CloudMap + Lambda workshop (一)
    第一篇:创建一个由ECS托管的nodejs服务一)创建拥有AdministratorAccess的IAMUser,在权限设置时,直接给予AdministratorAccess二)ECR服务注册代码仓库输入名称,完成Copy镜像库的URIXXXXXXXX.dkr.ecr.cn-northwest-1.amazonaws.com.cn/nodejs-test-app使用docker命令上传代......
  • AWS-SAA C03 题库 —— PART04 131-200
    131.Acompanyisdevelopingafile-sharingapplicationthatwilluseanAmazonS3bucketforstorage.ThecompanywantstoserveallthefilesthroughanAmazonCloudFrontdistribution.Thecompanydoesnotwantthefilestobeaccessiblethroughdirect......
  • day39基于阿里云的全面云上业务 - 基于AWS的全面云上业务(12.1-12.2)
    12.1、基于阿里云的全面云上业务(两节)容器服务ACK控制台:https://cs.console.aliyun.com/?spm=5176.12818093_47.categories-n-products.dcsk.60eb16d0N97QLB#/k8s/cluster/list容器服务Kubernetes版产品文档:https://help.aliyun.com/zh/ack/ack-managed-and-ack-dedicated/user-......
  • 洛谷 P9751 [CSP-J 2023] 旅游巴士 题解
    Solution能在起点等\(k\)的非负整数倍相当于能在任意点等\(k\)的非负整数倍。由于离开的时间要是\(k\)的负整数倍,将每个点拆成\(k\)个点,\(dis_{i,j}\)表示到了第\(i\)个点长度\(\bmod\text{}k\equivj\)的最短路径。转移时若时间未到,直接在原地等\(k\)的负整......