AWS CDK,为Route53中的现有S3存储桶创建别名记录

时间:2019-08-29 00:27:02

标签: typescript amazon-s3 amazon-route53 aws-cdk

我想知道如何使用AWS CDK获取现有的S3存储桶并为其创建别名记录点吗?

到目前为止很好:

const myExistingBucket = Bucket.fromBucketName(this, 'myExistingBucket', 'myExistingBucketName')

new route53.ARecord(this, 'myAliasRecord', {
  zone: myHostedZone,
  target: route53.AddressRecordTarget.fromAlias(new route53_targets.BucketWebsiteTarget(myExistingBucket))
});

我得到了:不能将类型为'IBucket'的参数分配给类型为'Bucket'的参数

fromBucketArn()fromBucketAttributes()fromBucketName()函数都返回IBucket类型,但是BucketWebsiteTarget()函数需要Bucket类型。

那么,我应该如何使用AWS CDK从现有类型中获取Bucket类型?

1 个答案:

答案 0 :(得分:1)

最后,我最终使用CloudFront基于AWS CDK Static Site Example在S3 Bucket上提供HTTPS(这是一种更好的做法)。 s3BucketSource直接接受IBucket类型。

const myExistingBucket = Bucket.fromBucketName(this, 'myExistingBucket', 'myExistingBucketName')

// CloudFront distribution that provides HTTPS
const devDistribution = new cloudfront.CloudFrontWebDistribution(this, 'mySiteDistribution', {
  aliasConfiguration: {
    acmCertRef: certArn, // Must be in us-east-1 region.
    names: ['app.example.com'],
    sslMethod: cloudfront.SSLMethod.SNI,
    securityPolicy: cloudfront.SecurityPolicyProtocol.TLS_V1_1_2016,
  },
  originConfigs: [
    {
      s3OriginSource: {
        s3BucketSource: myExistingBucket
      },
      behaviors: [{ isDefaultBehavior: true }],
    }
  ]
});

new route53.ARecord(this, 'myAliasRecord', {
  zone: zone,
  target: route53.AddressRecordTarget.fromAlias(new route53_targets.CloudFrontTarget(devDistribution)),
});