'Adding a HTTP_PROXY integration to an API GW route using CDK cfn constructs
I am attempting to add a new route to a HTTP API Gateway that points to a cloudfront CDN. Because my route requires a request parameter mapping, I cannot use the level 2 @aws-cdk/aws-apigatewayv2-integrations. The CDK must use the cfn construct apiGatewayV2.CfnIntegration. The code builds the API gateway and the integration and the route, but I cannot find a way to attach the target to the route. Below is an example of the code. How can I extract the integrationID (or physicalID) from the CfnIntegration const?
The alternative is to use the .addRoutes() method available on apiGatewayV2.HttpApi. But this requires the bind method on the integration. Since the apiGatewayV2.CfnRoute does not have a bind method I do not think this is a viable option.
const WebAPI = new apiGatewayV2.HttpApi(this, 'WebAPI', {
apiName: "MyAPI",
createDefaultStage: false,
});
const ProxyIntegration = new apiGatewayV2.CfnIntegration(this, 'ProxyIntegration', {
integrationType: "HTTP_PROXY",
integrationUri: `https://${MyCloudFront.distributionDomainName}`,
integrationMethod: apiGateway.HttpMethod.GET,
apiId: MyAPI.apiId,
payloadFormatVersion:"1.0",
requestParameters:{"overwrite:path":"$request.path"},
})
const StaticFileRoute= new apiGatewayV2.CfnRoute(this, 'StaticFileRoute', {
apiId: WebAPI.apiId,
//target: ProxyIntegration.ref, <--- this results in an error
routeKey: "GET /{proxy+}",
})
Any ideas on how to attach the integration to the route? I have this same issue when attempting to attach a apiGatewayV2.CfnAuthorizer to a route.
Solution 1:[1]
Do you use HttpApi so it's possible use the addRoutes method, but HttpApi class is in "experimental mode" so you should use CfnApi and if you use CfnApi you can use the form that you are explained... Creating a route and creating the integration.
Your problem is in the "format" of target value of your route, the format should be integrations/${myintegration.ref}.
Example...
const httpApi = new apigatewayv2.CfnApi(this, "Api", {
name: "sample-api",
protocolType: "HTTP"
});
const httpIntegration = new apigatewayv2.CfnIntegration(
this,
"HttpIntegration",
{
apiId: httpApi.ref,
integrationType: "HTTP_PROXY",
integrationMethod: "ANY",
integrationUri: `https://another-sample-api.com/{proxy}`,
payloadFormatVersion: "1.0",
}
);
const proxyRoute = new apigatewayv2.CfnRoute(this, "ProxyRoute", {
apiId: httpApi.ref,
routeKey: "ANY /{proxy+}",
target: `integrations/${httpIntegration.ref}`,
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Duván Silva |
