te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to use AWS CDK to look up existing ApiGateway - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use AWS CDK to look up existing ApiGateway - Stack Overflow

programmeradmin2浏览0评论

I am using AWS CDK to build my lambda, and I would like to register endpoints from the lambda's CDK stack.

I found I can get an existing ApiGateway construct using fromRestApiId(scope, id, restApiId) (documentation here)

So currently this works well:

    //TODO how to look up by ARN instead of restApiId and rootResourceId??
    const lambdaApi = apiGateway.LambdaRestApi
                                .fromRestApiAttributes(this, generateConstructName("api-gateway"), {
                                    restApiId: <API_GATEWAY_ID>,
                                    rootResourceId: <API_GATEWAY_ROOT_RESOURCE_ID>,
                                });

    const lambdaApiIntegration = new apiGateway.LambdaIntegration(lambdaFunction,{
        proxy: true,
        allowTestInvoke: true,
    })

    const root = lambdaApi.root;

    root.resourceForPath("/v1/meeting/health")
        .addMethod("GET", lambdaApiIntegration);

But I would like to deploy to many AWS accounts, and many regions. I don't want to have to hardcode the API_GATEWAY_ID or API_GATEWAY_ROOT_RESOURCE_ID for each account-region pair.

Is there a more generic way to get the existing ApiGateway construct, (e.g. by name or ARN)?

Thank you in advance.

I am using AWS CDK to build my lambda, and I would like to register endpoints from the lambda's CDK stack.

I found I can get an existing ApiGateway construct using fromRestApiId(scope, id, restApiId) (documentation here)

So currently this works well:

    //TODO how to look up by ARN instead of restApiId and rootResourceId??
    const lambdaApi = apiGateway.LambdaRestApi
                                .fromRestApiAttributes(this, generateConstructName("api-gateway"), {
                                    restApiId: <API_GATEWAY_ID>,
                                    rootResourceId: <API_GATEWAY_ROOT_RESOURCE_ID>,
                                });

    const lambdaApiIntegration = new apiGateway.LambdaIntegration(lambdaFunction,{
        proxy: true,
        allowTestInvoke: true,
    })

    const root = lambdaApi.root;

    root.resourceForPath("/v1/meeting/health")
        .addMethod("GET", lambdaApiIntegration);

But I would like to deploy to many AWS accounts, and many regions. I don't want to have to hardcode the API_GATEWAY_ID or API_GATEWAY_ROOT_RESOURCE_ID for each account-region pair.

Is there a more generic way to get the existing ApiGateway construct, (e.g. by name or ARN)?

Thank you in advance.

Share Improve this question asked Jan 13, 2021 at 17:25 baumannalexjbaumannalexj 8752 gold badges11 silver badges21 bronze badges 4
  • what is ARN for Api Gateway, I don't see one. Are you referring to arn:aws:execute-api:region:accountId:apiId/* that we refer in IAM policies? it still has api-id part of it. so, whatever we choose to parameterize the CDK code with stack imports or environment variables or stack inputs, we have to choose to include api-id anyway. – Balu Vyamajala Commented Jan 13, 2021 at 17:44
  • Great point :} I found this docs.aws.amazon./apigateway/latest/developerguide/…, but maybe since there is no direct ARN, that is why we cannot look up the existing API construct by ARN. – baumannalexj Commented Jan 13, 2021 at 17:50
  • Mostly places, I have exported api id from CloudFormation/CDK and imported into other stacks/cdk where it is used. let me know if you want me to write up an answer with some details. – Balu Vyamajala Commented Jan 13, 2021 at 17:59
  • That would be amazing if you could write something on exports! We need to do this in other CDK stacks where we want to reference an export of a group of resources (export a group of security groups, and the CDK can reference the export by name, instead of the unique security groups by account/region -unique names). – baumannalexj Commented Jan 13, 2021 at 18:03
Add a ment  | 

1 Answer 1

Reset to default 13

Lets take a simple Api with one resource

const restApi = new apigw.RestApi(this, "my-api", {
  restApiName: `my-api`,
});
const mockIntegration = new apigw.MockIntegration();
const someResource = new apigw.Resource(this, "new-resource", {
  parent: restApi.root,
  pathPart: "somePath",
  defaultIntegration: mockIntegration,
});
someResource.addMethod("GET", mockIntegration);

Lets assume we want use this api and resource in another stack, we first need to export

new cdk.CfnOutput(this, `my-api-export`, {
  exportName: `my-api-id`,
  value: restApi.restApiId,
});

new cdk.CfnOutput(this, `my-api-somepath-export`, {
  exportName: `my-api-somepath-resource-id`,
  value: someResource.resourceId,
});

Now we need to import in new stack

const restApi = apigw.RestApi.fromRestApiAttributes(this, "my-api", {
  restApiId: cdk.Fn.importValue(`my-api-id`),
  rootResourceId: cdk.Fn.importValue(`my-api-somepath-resource-id`),
});

and simply add additional resources and methods.

const mockIntegration = new apigw.MockIntegration();
new apigw.Resource(this, "new-resource", {
  parent: restApi.root,
  pathPart: "new",
  defaultIntegration: mockIntegration,
});
发布评论

评论列表(0)

  1. 暂无评论