Search code examples
amazon-cloudfrontaws-appsyncaws-cdk

Using AppSync via CloudFront with the CDK


I want to deploy a AppSync API behind a CloudFront distribution.

The CloudFront distribution needs an HTTP origin, how do I get this from my API object inside one CDK stack?


Solution

  • I found a solution. Had to use an intrinsic function to get the right value out of the distribution construct.

    const api = new appsync.GraphqlApi(...);
    
    const origin = new origins.HttpOrigin(
      cdk.Fn.select(2, cdk.Fn.split("/", api.graphqlUrl))
    );
    
    const distribution = new cloudfront.Distribution(this, "DemoDistribution", {
      defaultBehavior: { origin },
    });