Skip to content

Commit 4703248

Browse files
committed
feat(domains): migrate ip to load balancer
1 parent bd79f49 commit 4703248

File tree

3 files changed

+55
-38
lines changed

3 files changed

+55
-38
lines changed

packages/accounts-api/env.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ declare namespace NodeJS {
55
LOG_LEVEL?: string
66
GRAPHQL_HTTP_URL: string
77
JWT_SECRET: string
8+
JWT_TOKEN?: string
89
HASURA_SECRET?: string
10+
AWS_ACCESS_KEY?: string
11+
AWS_SECRET_KEY?: string
12+
AWS_ROUTE53_REGION?: string
13+
AWS_LOAD_BALANCER_DNS?: string
14+
AWS_LOAD_BALANCER_HOSTED_ZONE_ID?: string
915
}
10-
}
16+
}

packages/domains-api/src/config/config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ type Config = {
1313
awsAccessKey?: string
1414
awsSecretKey?: string
1515
awsRoute53Region: string
16-
awsRouteIp: string
16+
awsLoadBalancerDns: string
17+
awsLoadBalancerHostedZoneId: string
1718
}
1819

1920
const config: Config = {
@@ -27,7 +28,8 @@ const config: Config = {
2728
awsAccessKey: process.env.AWS_ACCESS_KEY,
2829
awsSecretKey: process.env.AWS_SECRET_KEY,
2930
awsRoute53Region: process.env.AWS_ROUTE53_REGION || 'sa-east-1',
30-
awsRouteIp: process.env.AWS_ROUTE_IP || 'localhost'
31+
awsLoadBalancerDns: process.env.AWS_LOAD_BALANCER_DNS || 'a2f212cad2d294747a445a1c3912809d-d2f26ccca140a83f.elb.us-east-1.amazonaws.com.',
32+
awsLoadBalancerHostedZoneId: process.env.AWS_LOAD_BALANCER_HOSTED_ZONE_ID || 'Z35SXDOTRQ7X7K'
3133
};
3234

3335
export default config;
Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,85 @@
11
import config from '../config/config';
2-
import logger from '../config/logger'
2+
import logger from '../config/logger';
3+
4+
type AliasTarget = {
5+
DNSName: string;
6+
EvaluateTargetHealth: boolean;
7+
HostedZoneId: string;
8+
};
39

410
type ResourceRecord = {
5-
Value: string
6-
}
11+
Value: string;
12+
};
713

814
type ResourceRecordSet = {
9-
Name: string
10-
ResourceRecords: ResourceRecord[]
11-
TTL: number
12-
Type: string
13-
}
15+
Name: string;
16+
ResourceRecords?: ResourceRecord[];
17+
AliasTarget?: AliasTarget;
18+
TTL?: number;
19+
Type: string;
20+
};
1421

1522
type Change = {
16-
Action: string
17-
ResourceRecordSet: ResourceRecordSet
18-
}
23+
Action: string;
24+
ResourceRecordSet: ResourceRecordSet;
25+
};
1926

2027
type ChangeBatch = {
21-
Changes: Change[]
22-
Comment: string
23-
}
28+
Changes: Change[];
29+
Comment: string;
30+
};
2431

2532
type RecordParams = {
26-
ChangeBatch: ChangeBatch
27-
HostedZoneId: string
28-
}
33+
ChangeBatch: ChangeBatch;
34+
HostedZoneId: string;
35+
};
2936

3037
type Args = {
31-
domain: string
32-
hostedZoneId: string
33-
}
38+
domain: string;
39+
hostedZoneId: string;
40+
};
3441

3542
export default (route53: any) => async ({ domain, hostedZoneId }: Args) => {
36-
// Create defaults Record on AWS
3743
const records: RecordParams = {
3844
ChangeBatch: {
3945
Changes: [
4046
{
4147
Action: 'CREATE',
4248
ResourceRecordSet: {
4349
Name: domain,
44-
ResourceRecords: [
45-
{ Value: config.awsRouteIp }
46-
],
47-
TTL: 300,
48-
Type: 'A'
50+
Type: 'A',
51+
AliasTarget: {
52+
DNSName: config.awsLoadBalancerDns,
53+
EvaluateTargetHealth: true,
54+
HostedZoneId: config.awsLoadBalancerHostedZoneId
55+
}
4956
}
5057
},
5158
{
5259
Action: 'CREATE',
5360
ResourceRecordSet: {
54-
Name: `*.${domain}`,
61+
Name: `*.${domain}`,
5562
ResourceRecords: [
56-
{ Value: config.awsRouteIp }
63+
{ Value: config.awsLoadBalancerDns }
5764
],
5865
TTL: 300,
59-
Type: 'A'
66+
Type: 'CNAME'
6067
}
6168
}
6269
],
6370
Comment: 'autocreated'
6471
},
6572
HostedZoneId: hostedZoneId
66-
}
67-
logger.child({ records }).info('changeResourceRecordSets');
73+
};
6874

75+
logger.child({ records }).info('changeResourceRecordSets');
76+
6977
try {
7078
const result = await route53.changeResourceRecordSets(records).promise();
71-
logger.child({ result }).info('changeResourceRecordSets');
79+
logger.child({ result }).info('changeResourceRecordSets - success');
80+
return result;
7281
} catch (err) {
73-
logger.child({ err }).info('error');
74-
return [];
82+
logger.child({ err }).error('changeResourceRecordSets - error');
83+
throw err;
7584
}
76-
}
85+
};

0 commit comments

Comments
 (0)