AWS CDK: how to target API Gateway API from Route53

aws-cdk route53 example
cdk api gateway authorizer
aws-cdk api gateway
aws cdk-openapi
aws-cdk api gateway custom domain
route53 arecord cdk
aws-route53-targets
aws cdk api gateway resource

I have an existing domain name registered with AWS Route53 and I have set up a custom domain name in API Gateway. In the console i can configure things such that xxxxxx.zenxxxxxxfoundry.com from the outside, actually reaches the API Gateway API and then on through to my Lambda functions.

Now I want to achieve this with AWS CDK.

I have tried the following:

    const zone = route53.HostedZone.fromHostedZoneId(this, 'ZenithWebFoundryZone', 'ZXXXXXX04V8134');
    new route53.AliasRecord(this, 'BlogAPIRecord', {
      zone: zone,
      recordName: 'xxxxxx.zenxxxxxxfoundry.com',
      target: {
        bind: (): route53.AliasRecordTargetProps => ({
          dnsName: 'd-xxxxxxy00g.execute-api.ap-southeast-2.amazonaws.com',
          hostedZoneId: 'ZXXXXXX04V8134'
        })
      }
    });

which builds ok npm run build but when I run cdk synth I get the rather obtuse error:

$ cdk synth
HostedZone.fromHostedZoneId doesn't support "zoneName"
Subprocess exited with error 1

Switching on --trace didn't really help much: the additional info:

Error: Subprocess exited with error 1
    at ChildProcess.proc.on.code (/Users/mikecoxon/.npm-packages/lib/node_modules/aws-cdk/lib/api/cxapp/exec.ts:108:23)
    at ChildProcess.emit (events.js:189:13)
    at ChildProcess.EventEmitter.emit (domain.js:441:20)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)

I have looked through the whole stack script and there is no reference to zoneName anywhere. Does anyone know where this error comes from?

With aws-cdk v0.34.0 should be able to do the following:

const zone = route53.HostedZone.fromHostedZoneAttributes(this, 'ZenithWebFoundryZone', {
  hostedZoneId: 'ZXXXXXX04V8134',
  zoneName: 'zenxxxxxxfoundry.com' // your zone name here
});
new route53.ARecord(this, 'BlogAPIRecord', {
  zone,
  recordName: 'xxxxxx.zenxxxxxxfoundry.com',
  target: route53.AddressRecordTarget.fromAlias({
    bind: (): route53.AliasRecordTargetConfig => ({
      dnsName: 'd-xxxxxxy00g.execute-api.ap-southeast-2.amazonaws.com', // Specify the applicable domain name for your API.
      hostedZoneId: 'XXXX', // Specify the hosted zone ID for your API.
    }),
  }),
});

If your API is in the same stack/code base you can get the dnsName and hostedZoneId from it (it's a CF attribute).

Otherwise refer to DNSName and HostedZoneId in the AWS::Route53::RecordSet AliasTarget documentation.

Note: the hostedZoneId for your alias record is not the same as the hosted zone id of your own zone.

class ApiGateway � AWS CDK, ApiGateway .NET, Amazon.CDK.AWS.Route53.Targets.ApiGateway. TypeScript ( source) Defines an API Gateway REST API as the alias target. Requires that� An API Gateway API that has a custom domain name, such as api.example.com, that matches the name of the Route 53 record that you want to create. A registered domain name. You can use Amazon Route 53 as your domain registrar, or you can use a different registrar.

In AWS CDK 0.36.1 you can use the @aws-cdk/aws-route53-targets package to create aliases.

import { HostedZone, RecordSet, RecordType, RecordTarget } from '@aws-cdk/aws-route53'
import { ApiGatewayDomain } from '@aws-cdk/aws-route53-targets'
import { Certificate } from '@aws-cdk/aws-certificatemanager'

// ...

  const customDomain = new apigateway.DomainName(this, 'CustomDomain', {
    domainName: props.apiDomain,
    certificate: Certificate.fromCertificateArn(this, 'Certificate', props.certificateArn),
    endpointType: apigateway.EndpointType.EDGE,
  })

  const hostedZone = HostedZone.fromHostedZoneAttributes(this, 'HostedZone', {
    hostedZoneId: props.hostedZoneId,
    zoneName: props.hostedZoneName,
  })

  new RecordSet(this, 'ApiRecordSetA', {
    zone: hostedZone,
    recordType: RecordType.A,
    recordName: 'api',
    target: RecordTarget.fromAlias(new ApiGatewayDomain(customDomain))
  })

  new RecordSet(this, 'ApiRecordSetAAAA', {
    zone: hostedZone,
    recordType: RecordType.AAAA,
    recordName: 'api',
    target: RecordTarget.fromAlias(new ApiGatewayDomain(customDomain))
  })

aws-apigateway module � AWS CDK, With aws-cdk v0.34.0 should be able to do the following: const zone = route53. HostedZone.fromHostedZoneAttributes(this� Amazon API Gateway is a fully managed service that makes it easy for developers to publish, maintain, monitor, and secure APIs at any scale. Create an API to access data, business logic, or functionality from your back-end services, such as applications running on Amazon Elastic Compute Cloud (Amazon EC2), code running on AWS Lambda, or any web application.

I know this is an old question, but I was facing the same issue using cdk 1.36.1 and managed to solve it using a new Custom Domain Name in ApiGateway, together withBasePath Mapping and then adding a CName record in an existing hosted zone on route53 pointing to the new custom domain:

import {BasePathMapping, DomainName, EndpointType, LambdaRestApi} from '@aws-cdk/aws-apigateway';
import {Certificate} from '@aws-cdk/aws-certificatemanager';
import {HostedZone, CnameRecord} from '@aws-cdk/aws-route53'

// First create a custom domain:
const customDomain = new DomainName(this, 'customDomain', {
      domainName: 'api.xxxxxxx.com',
      certificate: Certificate.fromCertificateArn(this, 'ACM_Certificate', ACM_CERTIFICATE_ARN),
      endpointType: EndpointType.EDGE
});

// create a new ApiGateway instance and associate a lambda function with it:
const api = new LambdaRestApi(this, 'MainGatewayEndpoint', {
      handler: toyFunction,
});

// Associate the Custom domain that we created with new APIGateway using BasePathMapping:
new BasePathMapping(this, 'CustomBasePathMapping', {
      domainName: custom,
      restApi: api
});

// Get a reference to AN EXISTING hosted zone using the HOSTED_ZONE_ID. You can get this from route53
const hostedZone = HostedZone.fromHostedZoneAttributes(this, 'HostedZone', {
      hostedZoneId: PROD_HOSTED_ZONE_ID,
      zoneName: 'xxxxxxx.com'
});

// Finally, add a CName record in the hosted zone with a value of the new custom domain that was created above:
new CnameRecord(this, 'ApiGatewayRecordSet', {
      zone: hostedZone,
      recordName: 'api',
      domainName: customDomain.domainNameAliasDomainName
});


AWS CDK: how to target API Gateway API from Route53, Hi there,. I am trying to add target of AliasRecord pointing to Apigateway. eladb added the @aws-cdk/aws-apigateway label on Jul 31, 2019. CDK Constructs for AWS Route53 Alias Targets This library contains Route53 Alias Record targets for: API Gateway custom domains (self, "AliasRecord", zone

I got the same error with the following:

const zone = route53.HostedZone.fromHostedZoneId(this, 'MyZone', 'ZXXXXXXXXXXXXX');

I have a zone in Route53 similar to below:

Domain Name:    example.com.
Type:           Public Hosted Zone
Hosted Zone ID: ZXXXXXXXXXXXXX

I changed to the following and it worked (CDK 0.34.0):

const zone = new route53.HostedZoneProvider(this,  {
  domainName: 'example.com',
}).findAndImport(this, 'MyZone');

How to bind apigateway as AliasRecordTarget to Route53 � Issue , To route domain traffic to an API Gateway API, use Amazon Route 53 to create an packages/@aws-cdk/aws-route53-targets/lib/api-gateway-domain-name.ts� 4. Attach the NameCheap domain with the AWS API Gateway. Back to the interesting part. Go to “Custom Domain Names” section of AWS API Gateway and click “Create Custom Domain Name”.

I managed to get this working but was caught out with the Hosted Zone name requiring the '.' at the end of it.

This is for EXISTING hosted zones that have been created manually and need to be looked up to add alias records through CDK.

const hostedZone = route53.HostedZone.fromHostedZoneAttributes(this, 'HostedZone', {
  hostedZoneId: config.apiHostedZoneId,
  name: `${config.apiDomainName}.`,
});

new route53.RecordSet(this, 'ApiRecordSetA', {
  zone: hostedZone,
  recordType: route53.RecordType.A,
  recordName: config.apiDomainName,
  target: route53.RecordTarget.fromAlias(new route53Targets.ApiGatewayDomain(apiDomain))
});

new route53.RecordSet(this, 'ApiRecordSetAAAA', {
  zone: hostedZone,
  recordType: route53.RecordType.AAAA,
  recordName: config.apiDomainName,
  target: route53.RecordTarget.fromAlias(new route53Targets.ApiGatewayDomain(apiDomain))
});

feat(apigateway): support custom domain names � Issue #3135 � aws , This library contains Route53 Alias Record targets for: API Gateway custom domains # Example automatically generated without compilation. Working with the latest version of AWS Cloud Development Kit (CDK) — 1.42.0 at the time of writing — let’s run through a short demo of HTTP API. We’ll explore how to set up the API

aws-cdk.aws-route53-targets � PyPI, aws-cdk.aws-apigateway 1.10.0 AwsIntegration - can be used to invoke arbitrary AWS service APIs. zone: hostedZoneForExampleCom, target: route53 . AWS Management Console: For information about how to get the value by using the console, see Using Custom Domains with AWS Elastic Beanstalk in the AWS Elastic Beanstalk Developer Guide. Elastic Beanstalk API : Use the DescribeEnvironments action to get the value of the CNAME attribute.

Amazon API Gateway Construct Library, A very common practice is to use Amazon API Gateway with AWS Lambda as the backend integration. zone: hostedZoneForExampleCom, target: route53. Amazon API Gateway is a fully managed service that makes it easy for developers to publish, maintain, monitor, and secure APIs at any scale. Create an API to access data, business logic, or functionality from your back-end services, such as applications running on Amazon Elastic Compute Cloud (Amazon EC2), code running on AWS Lambda, or any web application.

@aws-cdk/aws-apigateway, no worries, This looks like it's for an API Gateway custom domain name? _ Stage DomainAliasRecordSet: Type: AWS::Route53::RecordSet Properties:� where api-id is generated by API Gateway, region (AWS Region) is specified by you when creating the API, and stage is specified by you when deploying the API. The hostname portion of the URL (that is, api-id .execute-api. region .amazonaws.com ) refers to an API endpoint.

Comments
  • ZenithWebFoundryZone is a zone name you are passing in fromHostedZoneId method? The documentation says that the 2nd parameter to fromHostedZoneId is id but doesn't explain id of what.
  • No it's actually the third parameter. "id" seems always to be the second parameter for these factory methods, and most of the examples seem to present that as a kind of handle for the construct being made. However, most of the examples don't actually use the handle, so I assume it has some application in the resulting Cloud Formation Template.
  • I referred to this documentation docs.aws.amazon.com/cdk/api/latest/docs/… and it is from AWS.... as per that the third parameter is hostedZoneId.... but there is no explanation about id parameter.
  • I'm on the verge of confirming your suggestion, however, I seem to be beset with this issue now, and can't perform cdk synth for the moment.
  • confirmed this works - thank you so much. I had a lot of issues cleanly updating to 0.34.0 through npm.
  • I was thinking about this, I was just afraid to try it, because I thought it might clobber my current hosted zone, which is set up and working. Maybe I should shut my eyes and try it?