我们在通过 CDK 部署的基础设施中使用经典负载均衡器。为了部署负载均衡器,我们使用 2 级构造。代码是这样的:
const lb = new elb.LoadBalancer(this, 'LB', {
vpc: vpcRef,
internetFacing: true,
healthCheck: {
port: 80
},
});
lb.addListener({
externalPort: 80,
});
}
我们无法找到任何可以启用访问日志记录的属性。有人建议我使用 AccessLoggingPolicyProperty。我检查了一下,发现该属性只能与 1 级构造一起使用。请指导我如何使用 2 级结构在经典负载均衡器上通过 CDK 启用访问日志。
最佳答案
根据 the documentation您需要配置正确权限的 S3 存储桶。这样您就可以关注aws-cdk documentation on how to get access to L1构造。
它看起来大致像下面的代码
const lbLogs = new Bucket(this, 'LB Logs');
const elbAccountId = 'TODO: find right account for you region in docs';
lbLogs.grantPut(new AccountPrincipal(elbAccountId));
lbLogs.grantPut(
new ServicePrincipal('delivery.logs.amazonaws.com', {
conditions: {
StringEquals: {
's3:x-amz-acl': 'bucket-owner-full-control',
},
},
})
);
lbLogs.grantRead(new ServicePrincipal('delivery.logs.amazonaws.com'));
const cfnLoadBalancer = lb.node.defaultChild as CfnLoadBalancer;
cfnLoadBalancer.accessLoggingPolicy = {
enabled: true,
s3BucketName: lbLogs.bucketName,
};
关于amazon-web-services - AWS CDK : enabling access logging for classical load balancer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67821495/