AWS-CDK Api-gateway
In this article we are going to cover a complete example of creating an API Gateway with Lambda integration.
There are different ways of creating api gateway, but in this example we will be covering how to create Rest Api.
If you are new to CDK do check out blog.
Create a new file under lib folder.
import * as apiGateway from 'aws-cdk-lib/aws-apigateway';
import * as cdk from 'aws-cdk-lib';
export class ApiGatewayStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const apiGatewayResource = new apigateway.RestApi(this, 'rest-api', {
description: 'Api gateway',
deployOptions: {
stageName: 'dev',
},
// 👇 enable CORS
defaultCorsPreflightOptions: {
allowMethods: ['OPTIONS', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
allowCredentials: true,
allowOrigins: ['*'],
},
});
new cdk.CfnOutput(this, 'apiUrl', {value: api.url});
}
}
No comments:
Post a Comment
If you have any doubts, Please let me know