小编Mar*_*vić的帖子

如何使用 AWS CDK 通过 ApiGateway 和 S3 设置 CloudFront?

我正在尝试使用 AWS CDK 设置具有 2 个不同来源的 CloudFront 分配:

  • S3
  • 网关

这是堆栈的示意图。

我遇到的问题是我无法将 API 网关的域正确传递到 CloudFront 分配。这是我的尝试:

const api = new ag.RestApi(this, "RestApi", {
    deploy: true
});

api.root
    .addResource("api")
    .addResource("photo")
    .addResource("{id}")
    .addMethod("GET", new ag.LambdaIntegration(lambdaFunction));

const url = URL.parse(api.url);
const domainName = url.hostname as string;

const dist = new cf.CloudFrontWebDistribution(this, "Distribution", {
    originConfigs: [
        {
            s3OriginSource: { s3BucketSource: bucket },
            behaviors: [{ isDefaultBehavior: true }]
        },
        {
            customOriginSource: { domainName },
            behaviors: [
                {
                    pathPattern: "/api/*"
                }
            ]
        }
    ]
});

new …
Run Code Online (Sandbox Code Playgroud)

amazon-cloudfront typescript aws-api-gateway aws-cdk

4
推荐指数
1
解决办法
2235
查看次数