简体   繁体   English

我可以使用 CDK 或 CLI 在 AWS API Gatway 中创建资源和方法吗?

[英]Can I create resources and methods in AWS API Gatway using CDK or CLI?

Currently, I'm learning AWS CDK and I want to create an Api gateway with multiple resources and each with its own methods.目前,我正在学习 AWS CDK,我想创建一个具有多种资源的 Api 网关,每个资源都有自己的方法。

Can I do so using CDK or CLI or do I have to do so from the AWS web console?我可以使用 CDK 或 CLI 执行此操作,还是必须从 AWS Web 控制台执行此操作?

Yes its possible from both, here is an example Lambda function with Rest API in CDK:是的,两者都有可能,这里是 CDK 中带有 Rest API 的 Lambda 函数示例:

    const transactionFunction = new lambda.Function(this, 'TransactionFunction', {
      runtime: lambda.Runtime.NODEJS_14_X,
      memorySize: 1024,
      timeout: Duration.seconds(10),
      handler: 'index.handler',
      code: lambda.Code.fromAsset(path.join(__dirname, '../src/lambda/transaction/')),
      environment: {
        REGION: Stack.of(this).region
      }
    });

    const api = new RestApi(this, 'TransactionAPI', {
      description: 'Transactions API',
      deployOptions: {
        stageName: 'dev'
      },
      defaultCorsPreflightOptions: {
        allowHeaders: [
          'Content-Type',
          'X-Amz-Date',
          'Authorization',
          'X-Api-Key',
        ],
        allowMethods: ['OPTIONS', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
        allowCredentials: true,
        allowOrigins: Cors.ALL_ORIGINS
      }
    })

    const transactions = api.root.addResource('transactions');

    transactions.addMethod('POST', new LambdaIntegration(transactionFunction));

    new CfnOutput(this, 'apiUrl', { value: api.url });

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway-readme.html#defining-apis https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway-readme.html#defining-apis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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