Wingjam
Wingjam

Reputation: 802

AWS CDK, creating an alias record for an existing S3 bucket in Route53

I wonder how I could get an existing S3 bucket and create an alias record point to it using AWS CDK?

So far so good :

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

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

And I got : Argument of type 'IBucket' is not assignable to parameter of type 'Bucket'.

fromBucketArn(), fromBucketAttributes() and fromBucketName() functions all return IBucket type, but the BucketWebsiteTarget() function need a Bucket type.

So, how am I suppose to get a Bucket type from an existing one using AWS CDK?

Upvotes: 5

Views: 3989

Answers (1)

Wingjam
Wingjam

Reputation: 802

Finally, I ended up using CloudFront to provide HTTPS over S3 Bucket (which is a better practice) based on AWS CDK Static Site Example. s3BucketSource accept directly a IBucket type.

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)),
});

Upvotes: 2

Related Questions