Home > Enterprise >  AWS CDK Python: Error when trying to create A Record using zone from lookup
AWS CDK Python: Error when trying to create A Record using zone from lookup

Time:04-04

Using Python3.8, CDK 2.19.0

I want to create an A Record against a hosted zone that's already in my AWS account.

I am doing the following:

hosted_zone = route53.HostedZone.from_hosted_zone_attributes(self, "zone",
    zone_name="my.awesome.zone.",
    hosted_zone_id="ABC12345DEFGHI"
)

route53.ARecord(self, "app_record_set",
    target=self.lb.load_balancer_dns_name, # this is declared above, and works fine.
    zone=hosted_zone,
    record_name="test-cdk.my.awesome.zone"
)

Inside my app.py I have:

env_EU = cdk.Environment(account="12345678901112", region="eu-west-1")

app = cdk.App()
create_a_record = DomianName(app, "DomianName", env=env_EU)

When I run cdk synth I get the following error:

➜ cdk synth
    jsii.errors.JavaScriptError: 
    Error: Expected object reference, got "${Token[TOKEN.303]}"
    File ".../.venv/lib/python3.8/site-packages/jsii/_kernel/providers/process.py", line 326, in send
    ...(full traceback)



Subprocess exited with error 1

I've tried from_lookup (rather than from_hosted_zone_attributes, Python3.9/Node 17/12/16 (just in case) but nothing is helping. I get the same error every time.

If I comment out the A Record creation, then the synth completes as expected.

cdk.context.json also has the correct hosted zone cached BUT only happens if I comment out the A record creation.

CodePudding user response:

The ARecord target expects a type of RecordTarget. You are passing a string (token). Use a LoadBalancerTarget:

import aws_cdk.aws_elasticloadbalancingv2 as elbv2

# zone: route53.HostedZone
# lb: elbv2.ApplicationLoadBalancer

route53.ARecord(self, "AliasRecord",
    zone=zone,
    target=route53.RecordTarget.from_alias(targets.LoadBalancerTarget(lb))
)
  • Related