I am using AWS CDK ( Python ) to create an Alias record targeted to existing cloud front distribution which has been provisioned in different AWS Account and different Route53 Hosted zone.
I am importing existing CF Distribution and public Route53 Zone via below CDK Code.
existinghostezone=route53.HostedZone.from_hosted_zone_attributes(self,"existinghostedzone",
hosted_zone_id="HOSTEDZONE-ID",
zone_name ="ZONENAME")
existingdist=cloudfront.Distribution.from_distribution_attributes(self,"existingdist",
distribution_id="DISTRIBUTION-ID",
domain_name="DISTRIBUTION-DOMAIN-NAME"
)
Below is the code where I am creating a new route53 hosted zone and trying to create route53 A record that points to the imported cloud front distribution above.
#Creating new route53 zone
newZone=route53.HostedZone(self,"newZone",
zone_name="new-zone-name",
comment="Managed by CloudFormation")
#Creating alias entry config
my_alias = route53.IAliasRecordTarget.bind(
self,
record=route53.AliasRecordTargetConfig(
hosted_zone_id= existinghostezone.hosted_zone_id,
dns_name=existingdist.domain_name)
)
#Creating route53 a record
aRecord=route53.ARecord(self,"aRecord",
target=route53.RecordTarget(my_alias),
zone=newZone.hosted_zone_id,
record_name="new-record-name",
comment="Managed by CloudFormation"
)
Now when I try to run cdk synth
on the above code , I am getting following error message.
packages/jsii/_kernel/providers/process.py", line 332, in send
raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Expected object reference, got "${Token[TOKEN.58]}"
As I am new to the AWS CDK ( Python ), I just really don't know what does the above error mean.
I have also tried to create an Alias record with below method as well
#Creating new route53 zone
newZone=route53.HostedZone(self,"newZone",
zone_name="new-zone-name",
comment="Managed by CloudFormation")
#Creating alias entry config
my_alias = route53.IAliasRecordTarget.bind(
self,
record=route53.AliasRecordTargetConfig(
hosted_zone_id= existinghostezone.hosted_zone_id,
dns_name=existingdist.domain_name)
)
#Creating route53 a record
aRecord=route53.ARecord(self,"aRecord",
target=route53.RecordTarget.from_alias(my_alias),
zone=newZone.hosted_zone_id,
record_name="new-record-name",
comment="Managed by CloudFormation"
)
Now when I try to run cdk synth on the above code , I am getting following error message.
raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Got 'undefined' for non-optional instance of {"name":"aliasTarget","type":{"fqn":"@aws-cdk/aws-route53.IAliasRecordTarget"}}
I have tried to find some examples that uses the existing cloud front distribution as an Alias entry however every one has different methods and answers which aren't working for me.
Please advise.
When creating your ARecord, you'll want to provide the zone object (IHostedZone) for the zone
property
Try with:
aRecord = route53.ARecord(self,"aRecord",
target=route53.RecordTarget(my_alias),
zone=newZone, # rather than newZone.hosted_zone_id
record_name="new-record-name",
comment="Managed by CloudFormation"
)