A prerequisite for

  1. For details about creating AWS S3 buckets, see docs.aws.amazon.com/zh_cn/Amazo…
  2. Create Kinesis Firehose flow reference: docs.aws.amazon.com/zh_cn/fireh…

Realize the function

1. Use AWS API Gataway + AWS Lambda to get the visitor’s Public IP, insert it into the request content, and pass it into AWS S3 through AWS Kinesis Firehose

2. Access the deployed API with the key


Create Lambda functions using the console

  1. Open the AWS Lambda console.



    * After entering the console, please pay attention to the region submitted again. Please select the region corresponding to your business

  2. Select Create a Function to go to the function creation page



  3. In the function name, type my-function.

  4. Select a running language. Please refer to the official documentation for language support. This example uses noda.js as the running environment
  5. Select Create Function. The function was successfully created and entered the lambda online compilation environment



  6. Replace the code in index.js with the following code (in this example node.js is used so that those who have not experienced Node.js can understand it, important statements are commented)

    / * *

    * Import aws development modules

    * /
    const
    AWS = require(
    'aws-sdk'
    );
    / * *

    * Configure authentication information for aws modules

    * /
    AWS.config.update({

    accessKeyId:
    'AKI***********PY'
    .

    secretAccessKey:
    'xle****************cIoq'
    .

    region:
    'ap-southeast-1'
    });
    / * *

    * The lambda core module, which handles information about API requests, such as getting the request body, is in this module

    * Request information is transmitted through events. If you want to get additional information about requests such as public IP, you need to map it to the AWS API Gateway

    * /
    exports.handler = async (event) => {

    let firehose =
    new
    AWS.Firehose();

    let ip = event.context.sourceip;
    // Public IP is mapped to context.sourceip in API Gateway

    if
    (event.bodyjson){

    event.bodyjson.publicip = ip;
    // The request body is mapped to json named bodyJSON in the API Gateway

    }

    / * *

    * Set parameters for firehose

    * /

    let params = {

    DeliveryStreamName:
    'delay-collection-a'
    .

    Record: {
    /* required */

    Data: JSON.stringify(event.bodyjson)

    }

    };

    / * *

    * Send data to the Firehose

    * /

    firehose.putRecord(params, function(err, data) {

    if
    (err) console.log(err, err.stack);
    // an error occurred

    else
    console.log(data);
    // successful response

    });

    / * *

    * This is to test that the data eventually passed to the Firehose can be returned to the requester

    * /

    return
    event.bodyjson;
    };

  7. Click save

Create the AWS API Gateway using the console

  1. Open theAWS API Gateway console.



  2. chooseBegin to useThe API creation page is displayed



  3. In select protocol, select REST
  4. In New API, select New API
  5. Enter the API name my-API in the Settings
  6. chooseCreate API, the API is created successfully, and the API management page is displayed



Edit my-API using console

  1. Select the My-API that has been created on the AWS Gateway API management page
  2. To create a resource for my-API, selectoperationIn theCreate a resourceThe page for creating resources is displayed



  3. inResource nameEnter my-resource and selectCreate a resourceThe resource is created successfully



  4. inoperationIs the newly created resource my-resourceCreate method



  5. Select the POST method in the methods drop-down box



  6. The selectedPOSTAfter method, check next toCheck theButton, enterMy-resource-post-settingpage



  7. Select lambda functions in the integration environment
  8. In the lambda region, select the region selected in the first step of creating the lambda
  9. Type any letter into a lambda function, and the function you created, my-funciton, will appear in the drop-down box. Select it! Choose it! Choose it!
  10. Click on thesaveButton, pop-up permission confirmation box, click OK, wait a moment to enter the method execution interface



  11. On the method execution screen, selectIntegration request



  12. Select on the integration request pageMapping template (where he gets information about the user’s request body, such as request headers and publicIP), click on theAdd a mapping template,And enter it in the input boxapplication/json



  13. Click the check button to save and the change delivery behavior confirmation box will pop up. Click Yes to lock the integration
  14. Click to generate template input box and the template input box is displayed



  15. Enter the following template and click the Save button

    ## See http:
    //docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
    ## This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload
    #set($allParams = $input.params())
    {
    "bodyjson"
    : $input.json(
    '$'
    ),
    "params"
    : {
    #foreach($type in $allParams.keySet())

    #set($params = $allParams.get($type))
    "$type"
    : {

    #foreach($paramName in $params.keySet())

    "$paramName"
    :
    "$util.escapeJavaScript($params.get($paramName))"

    #
    if
    ($foreach.hasNext),#end

    #end
    }

    #
    if
    ($foreach.hasNext),#end
    #end
    },
    "stage-variables"
    : {
    #foreach($key in $stageVariables.keySet())
    "$key"
    :
    "$util.escapeJavaScript($stageVariables.get($key))"

    #
    if
    ($foreach.hasNext),#end
    #end
    },
    "context"
    : {

    "account-id"
    :
    "$context.identity.accountId"
    .

    "api-id"
    :
    "$context.apiId"
    .

    "api-key"
    :
    "$context.identity.apiKey"
    .

    "authorizer-principal-id"
    :
    "$context.authorizer.principalId"
    .

    "caller"
    :
    "$context.identity.caller"
    .

    "cognito-authentication-provider"
    :
    "$context.identity.cognitoAuthenticationProvider"
    .

    "cognito-authentication-type"
    :
    "$context.identity.cognitoAuthenticationType"
    .

    "cognito-identity-id"
    :
    "$context.identity.cognitoIdentityId"
    .

    "cognito-identity-pool-id"
    :
    "$context.identity.cognitoIdentityPoolId"
    .

    "http-method"
    :
    "$context.httpMethod"
    .

    "stage"
    :
    "$context.stage"
    .

    "sourceip"
    :
    "$context.identity.sourceIp"
    .

    "user"
    :
    "$context.identity.user"
    .

    "user-agent"
    :
    "$context.identity.userAgent"
    .

    "user-arn"
    :
    "$context.identity.userArn"
    .

    "request-id"
    :
    "$context.requestId"
    .

    "resource-id"
    :
    "$context.resourceId"
    .

    "resource-path"
    :
    "$context.resourcePath"

    }
    }

    “Bodyjson” : $input.json(‘$’)

    * get public IP statement is: “sourceip” : “$context. Identity. Sourceip”,


Use the console to test the API

  1. To return to the method execution screen, clicktestButton to test the page



  2. Enter the following JSON content in the request body and click Test

    {

    "test1"
    :
    "abcdef"
    .

    "test2"
    :
    "abcdef"
    }

  3. After the request is successful, 200 will be returned, and there is more publicIP in the response parameter (because aws console is used, the exact publicIP cannot be obtained, so local call is required to obtain it), which proves that the API is written successfully



Deploy the API using the console

  1. In the API Gateway console, select resource/my-Resource in API My-API and click Post to enter the method execution interface



  2. Click on the method execution screenMethod request“, then request the setup screen and select itNeed the API key, is set tois



  3. Click on theoperationButton, selected from the drop-down listThe deployment of the API



  4. In the deployment API dialog box that is displayed, select the Deployment phase drop-down list and selectA new phaseIn theThe name of the stageEnter the test



  5. Click the save button, jump to the stage interface, deployment is successful,Where the URL is called:pzoszxq868.execute-api.us-east-1.amazonaws.com/testThat’s the API that we publish, /my-resource, that’s the method that we call



  6. Because we set the access required key for the API, next, configure the key for the API

Configure the key for the API using the console

  1. In the AWS API Gateway console, selectThe API key, the selectedoperation, select a value from the drop-down list boxCreate API key



  2. inThe nameEnter: Taoli,API keysSelect automatic generation,d click the save button, and the API key is generated successfully



  3. You can view the generated key on the generated API key management page. Select the API key and click Display to display the key



  4. The selectedUse the planAnd click thecreateButton to create a usage plan. Click the button to create the page using the plan



  5. inThe name of theEnter my-test as requiredlimitandThe quotaThe number of clicksThe next step



  6. The selectedAdding API StorageIn theAPISelect it from the drop-down list boxMy-APIIn thephaseSelect it from the drop-down list boxTest, after the button is checked,Click on theThe next step



  7. The selectedSubscribe to an API key from the usage plan,thenThe name of theEnter the t.The created API key is displayedTaoli,Select it, check the button, and click Finish



  8. Now that all the API development work is done, we will use Postman to test the API we have released

Use Postman to test the deployed API

  1. Use the POST method to request the URL pzoszxq868.execute-api.us-east-1.amazonaws.com/test/my-resource
  2. Before the request, the request headers to add the following access keys, while adding a transport Type x – API – key: d2M16C6EXG6t8cR9buOuPBqHesLQwER7nhelyZk4 content-type: application/json
  3. Click Send, the request succeeds, and the result is returned



  4. You can query S3 data and enter S3 data normally

Viii. Reference materials

  1. AWS Lambda

  2. Amazon API Gateway