Hello everyone! How can I create a few slightly di...
# typescript
p
Hello everyone! How can I create a few slightly different resources re-using an existing code as much as possible? For example, I need to create several Route53 records. However, a few of them are simple CNAMEs but others have failover policy. Is it possible to make the failover block dynamic? Thanks!
w
Sure thing - you can fo something like
let failover = undefined; if (something) { failover = ...  } new Record(... { failover: failover });
g
how about something like this?
Copy code
function Record(type: string, record: string, failoverRoutingPolicies?: RecordFailoverRoutingPolicy[]) { return new aws.route53.Record(record, {
    records: [record],
    ttl: 300,
    type: "A",
    zoneId: aws_route53_zone_primary.zoneId,
});}
This then can be used like this:
Copy code
Record('A', '1.2.3.4');
Record('CNAME', '<http://foo.com|foo.com>');
Record('CNAME', '<http://blabla.com|blabla.com>', {somepolicy});
obviously my example isn't very useful in itself. but you get the idea: create a function which only accepts as args the 'non-common' things and hardcode the common things inside the function body.
if the failoverpolicy is the same across most entries, you can simply create it as const outside the function via
const policy = {mypolicy}
and then pass it to the function
p
many thanks! I'm gonna try it