मैं अपना लैम्ब्डा बनाने के लिए एडब्ल्यूएस सीडीके का उपयोग कर रहा हूं, और मैं लैम्ब्डा के सीडीके स्टैक से एंडपॉइंट्स पंजीकृत करना चाहता हूं।
मैंने पाया कि मैं fromRestApiId(scope, id,restApiId) का उपयोग करके मौजूदा ApiGateway निर्माण प्राप्त कर सकता हूं। (दस्तावेज़ीकरण यहां)
तो वर्तमान में यह अच्छी तरह से काम करता है:
//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);
लेकिन मैं कई एडब्ल्यूएस खातों और कई क्षेत्रों में तैनात करना चाहता हूं। मैं नहीं चाहता कि प्रत्येक खाता-क्षेत्र युग्म के लिए API_GATEWAY_ID या API_GATEWAY_ROOT_RESOURCE_ID को हार्डकोड करना पड़े।
क्या मौजूदा एपीगेटवे निर्माण (जैसे नाम या एआरएन) प्राप्त करने का एक और सामान्य तरीका है?
पहले ही, आपका बहुत धन्यवाद।
1 उत्तर
आइए एक संसाधन के साथ एक साधारण एपीआई लें
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);
आइए मान लें कि हम इस एपीआई और संसाधन को किसी अन्य स्टैक में उपयोग करना चाहते हैं, हमें पहले निर्यात करने की आवश्यकता है
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,
});
अब हमें नए स्टैक में आयात करने की आवश्यकता है
const restApi = apigw.RestApi.fromRestApiAttributes(this, "my-api", {
restApiId: cdk.Fn.importValue(`my-api-id`),
rootResourceId: cdk.Fn.importValue(`my-api-somepath-resource-id`),
});
और बस अतिरिक्त संसाधन और विधियाँ जोड़ें।
const mockIntegration = new apigw.MockIntegration();
new apigw.Resource(this, "new-resource", {
parent: restApi.root,
pathPart: "new",
defaultIntegration: mockIntegration,
});
arn:aws:execute-api:region:accountId:apiId/*
का जिक्र कर रहे हैं जिसका जिक्र हम IAM नीतियों में करते हैं? इसमें अभी भी इसका एपीआई-आईडी हिस्सा है। इसलिए, हम जो भी सीडीके कोड को स्टैक आयात या पर्यावरण चर या स्टैक इनपुट के साथ पैरामीटर करने के लिए चुनते हैं, हमें वैसे भी एपीआई-आईडी को शामिल करना चुनना होगा।