I am trying to create a REST API to return data to my front-end using a Lambda function all done in CDK.
Basically my api-gateway would route traffic from /uploads to my Lambda function. However, I'm having a bit of difficulty incorporating this.
const s3UploaderUrlLambda = new lambda.Function(
//defined my Lambda function
);
const api = new apigateway.LambdaRestApi(this, 's3uploader', {
handler: s3UploaderUrlLambda, //I believe this handler means that it will target this
//Lambda for every single route but I only want it for /uploads
proxy: false
});
const uploads = api.root.addResource('uploads');
uploads.addMethod('GET')
Can anyone help?
CodePudding user response:
Here is an example of how you could set up your CDK stack to route traffic from the /uploads path to your s3UploaderUrlLambda Lambda function:
const s3UploaderUrlLambda = new lambda.Function(
//defined my Lambda function
);
const api = new apigateway.RestApi(this, 's3uploader');
const uploads = api.root.addResource('uploads');
const uploadsIntegration = new apigateway.LambdaIntegration(s3UploaderUrlLambda);
uploads.addMethod('GET', uploadsIntegration);
This creates a new REST API using the apigateway.RestApi construct, and adds a new resource called uploads to the root of the API. Then it creates a new LambdaIntegration for the s3UploaderUrlLambda function, and adds a GET method to the uploads resource using this integration. This will route any GET requests to the /uploads path to the s3UploaderUrlLambda Lambda function.
You can also route different methods like post and put if you want to.
uploads.addMethod('POST', uploadsIntegration);
uploads.addMethod('PUT', uploadsIntegration);