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'

相关内容

热门资讯

第5分钟实锤!德州ai辅助有用... 第5分钟实锤!德州ai辅助有用(德州之星)其实真的有挂(详细辅助攻略教程);德州ai辅助有用辅助器中...
第九分钟实锤!德扑之星辅助器购... 第九分钟实锤!德扑之星辅助器购买(德扑ai)其实真的有挂(详细辅助解密教程)1、在德扑之星辅助器购买...
9分钟实锤!德州之星有外挂(德... 9分钟实锤!德州之星有外挂(德州)切实是有挂(详细辅助系统教程)1、完成德州之星有外挂的残局,帮助玩...
第八分钟实锤!德州之星有外挂(... 第八分钟实锤!德州之星有外挂(云扑克德州)果然真的有挂(详细辅助攻略教程)一、德州之星有外挂软件透明...
第1分钟实锤!德州ai辅助软件... 第1分钟实锤!德州ai辅助软件(wepower德州)本来真的有挂(详细辅助大神讲解)德州ai辅助软件...
第7分钟实锤!德州微扑克辅助(... 第7分钟实锤!德州微扑克辅助(线上wpk德州)一贯有挂(详细辅助微扑克教程);所有人都在同一条线上,...
第二分钟实锤!德州ai辅助软件... 第二分钟实锤!德州ai辅助软件(德州nzt)好像真的是有挂(详细辅助黑科技教程)1、进入到德州ai辅...
第九分钟实锤!智星德州菠萝开挂... 第九分钟实锤!智星德州菠萝开挂(德州wpk)切实是真的有挂(详细辅助科技教程)1、上手简单,内置详细...
两分钟实锤!智星德州菠萝有挂吗... 两分钟实锤!智星德州菠萝有挂吗(德州扑克)原来真的有挂(详细辅助AI教程)1、两分钟实锤!智星德州菠...
第一分钟实锤!德州ai辅助有用... 第一分钟实锤!德州ai辅助有用(云扑克德州)确实是真的有挂(详细辅助技巧教程)进入游戏-大厅左侧-新...