AWSCDK-无法通过Cloudfront域名访问ALB
创始人
2024-09-24 01:30:40
0

该问题通常是由于未正确配置 Cloudfront 缓存策略而引起的。下面是一份示例代码,展示如何使用 AWS CDK 正确配置 Cloudfront 缓存策略,并向 ALB 进行转发。

import * as cloudfront from '@aws-cdk/aws-cloudfront';
import * as s3 from '@aws-cdk/aws-s3';
import * as targets from '@aws-cdk/aws-elasticloadbalancingv2';
import * as cdk from '@aws-cdk/core';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const albArn = 'arn:aws:elasticloadbalancing:us-east-1:111111111111:loadbalancer/app/my-load-balancer/2222222222222222';
    const albDnsName = 'my-load-balancer-1234567890.us-east-1.elb.amazonaws.com';

    const alb = targets.ApplicationLoadBalancer.fromBalancerAttributes(this, 'ALB', {
      loadBalancerArn: albArn,
      securityGroups: [],
      vpc: ec2.Vpc.fromLookup(this, 'VPC', {
        isDefault: true
      }),
    });

    const cloudfrontOai = new cloudfront.OriginAccessIdentity(this, 'OriginAccessIdentity');
    const s3Bucket = new s3.Bucket(this, 'Bucket', {
      websiteIndexDocument: 'index.html',
      websiteErrorDocument: '404.html'
    });

    const distribution = new cloudfront.CloudFrontWebDistribution(this, 'Distribution', {
      originConfigs: [{
        s3OriginSource: {
          s3BucketSource: s3Bucket
        },
        behaviors: [{
          isDefaultBehavior: true,
          forwardedValues: {
            queryString: true,
            cookies: {
              forward: 'all'
            },
          },
          allowedMethods: cloudfront.CloudFrontAllowedMethods.ALL,
          defaultTtl: cdk.Duration.seconds(0),
          maxTtl: cdk.Duration.seconds(0),
          minTtl: cdk.Duration.seconds(0),
        }],
      }, {
        customOriginSource: {
          domainName: albDnsName,
          originProtocolPolicy: cloudfront.OriginProtocolPolicy.HTTPS_ONLY,
          originHeaders: {
            'host': alb.loadBalancerDnsName,
          },
        },
        behaviors: [{
          pathPattern: '/api/*',
          allowedMethods: cloudfront.CloudFrontAllowedMethods.ALL,
          forwardedValues: {
            queryString: true,
            cookies: {
              forward: 'all'

相关内容

热门资讯

推荐一款!微乐微信自建房辅助工... 推荐一款!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)插件1、任何微乐微信自建房辅...
实测揭晓!微乐自建房辅助工具插... 实测揭晓!微乐自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)攻略;所有人都在同一条线...
盘点一款!微乐小程序麻将自建房... 盘点一款!微乐小程序麻将自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)教程1、微乐小...
推荐十款!微乐自建房辅助工具脚... 推荐十款!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)教程1、微乐自建房辅助工具脚本辅...
盘点几款!微乐自建房辅助工具免... 盘点几款!微乐自建房辅助工具免费app,微乐自建房脚本免费入口(开挂自建房)app1.微乐自建房辅助...
最新技巧!微信卡五星辅助器,微... 最新技巧!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)方法微信卡五星辅助器透视方法中分为三...
实操分享!微乐自建房辅助工具免... 实操分享!微乐自建房辅助工具免费app,微乐小程序免费黑科技(开挂自建房)软件1、进入游戏-大厅左侧...
技术分享!微信卡五星辅助器,微... 技术分享!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)工具1)微信卡五星辅助器有没有挂:进...
盘点十款!微乐自建房辅助工具,... 盘点十款!微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)软件1、任何微乐家乡麻将自建...
实测分享!微信雀神麻将挂件开挂... 实测分享!微信雀神麻将挂件开挂辅助,微乐小程序免费黑科技(开挂自建房)攻略1、微信雀神麻将挂件开挂辅...