简体   繁体   中英

How to create an API Gateway trigger on an existing lambda function with aws-sdk?

How can I create a trigger on an existing lambda function with the aws-sdk?

I want an API Gateway endpoint with a proxy integration type as the trigger.

I'm using nodejs and aws-sdk 2.39.0.

Not elegant, but it works.

I'm more than happy to receive revisions / better solutions. I am very new to the AWS SDK.

const createApi = () => {

    sts.getCallerIdentity(function(err, data) {
        if (err) {
            console.log(err);
        } else {

            const accountId = data.Account;
            const functionName = 'resize-images-on-' + settings.aws.bucket;

            const apiName = 'resize-images-on-' + settings.aws.bucket + '-api';
            const uri = 'arn:aws:apigateway:' + settings.aws.region + ':lambda:path/2015-03-31/functions/arn:aws:lambda:' + settings.aws.region + ':' + accountId + ':function:' + functionName + '/invocations';

            var apiSwagger = {
              "swagger": "2.0",
              "info": {
                "version": "2017-04-10T12:43:16Z",
                "title": apiName
              },
              "basePath": "/prod",
              "schemes": [
                "https"
              ],
              "paths": {
                "/resize": {
                  "x-amazon-apigateway-any-method": {
                    "responses": {
                      "200": {
                        "description": "200 response"
                      }
                    },
                    "x-amazon-apigateway-integration": {
                      "responses": {
                        ".*": {
                          "statusCode": "200"
                        }
                      },
                      "uri": uri,
                      "passthroughBehavior": "when_no_match",
                      "httpMethod": "POST",
                      "type": "aws_proxy"
                    }
                  }
                }
              }
            }

            apiSwagger = JSON.stringify(apiSwagger);

            const apiParams = {
                body: apiSwagger,
            };

            apigateway.importRestApi(apiParams, function(err, data) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('API created');
                    configureLambda(data.id, accountId);
                }
            });
        }
    });
}

const configureLambda = (apiId, accountId) => {

    console.log('Configuring lambda ...');

    const functionName = 'resize-images-on-' + settings.aws.bucket;

    const lambdaPolicies = [
        {
            Action: "lambda:InvokeFunction", 
            FunctionName: functionName, 
            Principal: "apigateway.amazonaws.com", 
            StatementId: uuid(),
            SourceArn: 'arn:aws:execute-api:' + settings.aws.region + ':' + accountId + ':' + apiId + '/prod/ANY/resize'
        },
        {
            Action: "lambda:InvokeFunction", 
            FunctionName: functionName, 
            Principal: "apigateway.amazonaws.com", 
            StatementId: uuid(),
            SourceArn: 'arn:aws:execute-api:' + settings.aws.region + ':' + accountId + ':' + apiId + '/*/*/resize'
        }
    ];


    lambda.addPermission(lambdaPolicies[0], function(err, data) {
        if (err) {
            console.log(err);
        } else {
            lambda.addPermission(lambdaPolicies[1], function(err, data) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('Lambda configured');
                }
            });

        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM