Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(AWS-Route53): Check exists of a HostedZone by using fromLookUp #14755

Closed
Fitmavincent opened this issue May 19, 2021 · 2 comments
Closed

(AWS-Route53): Check exists of a HostedZone by using fromLookUp #14755

Fitmavincent opened this issue May 19, 2021 · 2 comments
Assignees
Labels
effort/small Small work item – less than a day of effort guidance Question that needs advice or information. p2

Comments

@Fitmavincent
Copy link

❓ General Issue

The Question

How do I utilise a error handling or try catch block to do a exist check of a resource.

let zone;
try {
  zone = HostedZone.fromLookup(scope, `${domainName}LookUpHostedZone`, {
    domainName: domainName
  });      
} catch (error) {
  zone = new PublicHostedZone(scope, `${domainName}HostedZone`, {
    zoneName: domainName
  });
}

the fromLookup methods fail immediately and what's the recommendation of performing a try catch block for operation like this?

Environment

  • CDK CLI Version: 1.102.0
  • Module Version: aws-route53
  • Node.js Version: 16.0.0
  • OS: all
  • Language (Version): Typescript

Other information

Similar problems for other AWS resources:
How to check if a custom VPC already exist using AWS CDK?

@Fitmavincent Fitmavincent added guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels May 19, 2021
@ryparker ryparker self-assigned this Jun 16, 2021
@ryparker ryparker added effort/small Small work item – less than a day of effort p2 and removed needs-triage This issue or PR still needs to be triaged. labels Jun 16, 2021
@NGL321 NGL321 assigned NGL321 and unassigned ryparker Jun 16, 2021
@NGL321
Copy link
Contributor

NGL321 commented Jun 16, 2021

Hey @Fitmavincent,

The best way to do this is to use the SDK to try to get the HostedZone. This way the existence of the resource is determined not during deploy time but during synth. Additionally, this means that no extraneous Cloudformation actions are taken.

This is our standard method (something I have used a few times). It should look something like the below:

var AWS = require('aws-sdk');
AWS.config.update({region: 'REGION'});
var route53 = new AWS.Route53({apiVersion: '2016-11-15'});

let zone;
let params = {
	Id: 'XXXX'
};

route53.getHostedZone(params, function(err, data) {
  if (err) {
	zone = new PublicHostedZone(scope, `${domainName}HostedZone`, {
    	zoneName: domainName
	});
  } else {
    console.log("Zone Exists");
  }
});

I hope that this helps! If this is not helpful guidance, please feel free to reopen and bug us about it again.

😸 😷

@NGL321 NGL321 closed this as completed Jun 16, 2021
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort/small Small work item – less than a day of effort guidance Question that needs advice or information. p2
Projects
None yet
Development

No branches or pull requests

3 participants