Skip to content

Commit 2d9490e

Browse files
authored
Merge pull request #10 from kyletbase2/master
Added service discovery function
2 parents 1ee1607 + e9f2f08 commit 2d9490e

File tree

4 files changed

+129
-21
lines changed

4 files changed

+129
-21
lines changed

README.md

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,95 @@
1-
![build-status](https://travis-ci.com/theonestack/hl-component-aurora-postgres.svg?branch=master)
1+
# aurora (Postgres) CfHighlander component
2+
## Parameters
23

3-
### Cfhighlander Aurora Postgres component
4+
| Name | Use | Default | Global | Type | Allowed Values |
5+
| ---- | --- | ------- | ------ | ---- | -------------- |
6+
| EnvironmentName | Tagging | dev | true | string
7+
| EnvironmentType | Tagging | development | true | string | ['development','production']
8+
| VPCId | Security Groups | None | false | AWS::EC2::VPC::Id
9+
| DnsDomain | DNS domain to use | None | true | string
10+
| SubnetIds | List of subnets | None | false | CommaDelimitedList
11+
| KmsKeyId | KMS ID | None | false | string (arn)
12+
| NamespaceId | Service discovery namespace ID | None | false | string
13+
| SnapshotId | Snapshot ID to provision from | None | false | string
14+
| WriterInstanceType | Writer instance type *if engine is set to provisioned* | None | false | string
15+
| ReaderInstanceType | Reader instance type *if engine is set to provisioned* | None | false | string
16+
## Outputs/Exports
417

5-
```bash
18+
| Name | Value | Exported |
19+
| ---- | ----- | -------- |
20+
| SecurityGroup | Security Group name | true
21+
| ServiceRegistry | CloudMap service registry ID | true
22+
| DBClusterId | Database Cluster ID | true
23+
24+
## Included Components
625

7-
# install highlander gem
8-
$ gem install cfhighlander
26+
[lib-ec2](https://github.com/theonestack/hl-component-lib-ec2)
927

10-
# build and validate standalone component
11-
$ cfcompile --validate
28+
## Example Configuration
29+
### Highlander
30+
```
31+
Component name:'database', template: 'aurora-postgres' do
32+
parameter name: 'DnsDomain', value: root_domain
33+
parameter name: 'DnsFormat', value: FnSub("${EnvironmentName}.#{root_domain}")
34+
parameter name: 'SubnetIds', value: cfout('vpcv2', 'PersistenceSubnets')
35+
parameter name: 'WriterInstanceType', value: writer_instance
36+
parameter name: 'ReaderInstanceType', value: reader_instance
37+
parameter name: 'EnableReader', value: 'true'
38+
parameter name: 'StackOctet', value: '80'
39+
parameter name: 'NamespaceId', value: cfout('servicediscovery', 'NamespaceId')
40+
end
41+
```
1242

43+
### Aurora Postgres Configuration
1344
```
14-
### Usage
45+
hostname: db
46+
database_name: appdb
47+
dns_format: ${DnsFormat}
48+
49+
storage_encrypted: true
50+
engine: aurora-postgres
51+
engine_version: '13.4'
52+
53+
writer_instance: db.r3.large
54+
reader_instance: db.r3.large
55+
56+
master_login:
57+
username_ssm_param: /${EnvironmentName}/myapp/dbuser
58+
password_ssm_param: /${EnvironmentName}/myapp/dbpass
1559
16-
### Configuration options
60+
security_group:
61+
-
62+
rules:
63+
-
64+
IpProtocol: tcp
65+
FromPort: 5432
66+
ToPort: 5432
67+
ips:
68+
- stack
69+
- company_office
70+
- company_client_vpn
1771
18-
TBD
72+
service_discovery:
73+
name: db
74+
```
1975

20-
### Parameters
76+
## Cfhighlander Setup
2177

22-
TBD
78+
install cfhighlander [gem](https://github.com/theonestack/cfhighlander)
2379

24-
### Configuration options
80+
```bash
81+
gem install cfhighlander
82+
```
2583

26-
TBD
84+
or via docker
2785

28-
### Outputs
86+
```bash
87+
docker pull theonestack/cfhighlander
88+
```
89+
## Testing Components
90+
91+
Running the tests
2992

30-
TBD
93+
```bash
94+
cfhighlander cftest aurora-postgres
95+
```

aurora-postgres.cfhighlander.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
ComponentParam 'VPCId', type: 'AWS::EC2::VPC::Id'
1414
ComponentParam 'SubnetIds', type: 'CommaDelimitedList'
1515
ComponentParam 'KmsKeyId' if (defined? kms) && (kms)
16+
17+
ComponentParam 'NamespaceId' if defined? service_discovery
1618
end
17-
19+
1820
end

aurora-postgres.cfndsl.rb

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
}
2222
if rule['security_group_id']
2323
sg_rule['SourceSecurityGroupId'] = FnSub(rule['security_group_id'])
24-
else
25-
sg_rule['CidrIp'] = FnSub(rule['ip'])
24+
else
25+
sg_rule['CidrIp'] = FnSub(rule['ip'])
2626
end
2727
if rule['desc']
2828
sg_rule['Description'] = FnSub(rule['desc'])
@@ -40,7 +40,7 @@
4040
Description: "outbound all for ports",
4141
IpProtocol: -1,
4242
}
43-
])
43+
])
4444
Tags aurora_tags
4545
end
4646

@@ -110,7 +110,7 @@
110110
DBInstanceClass Ref(:ReaderInstanceType)
111111
Tags aurora_tags
112112
}
113-
113+
114114
Route53_RecordSet(:DBClusterReaderRecord) {
115115
Condition(:CreateReaderRecord)
116116
HostedZoneName FnJoin('', [ Ref('EnvironmentName'), '.', Ref('DnsDomain'), '.'])
@@ -129,6 +129,40 @@
129129
ResourceRecords [ FnGetAtt('DBCluster','Endpoint.Address') ]
130130
}
131131

132+
registry = {}
133+
service_discovery = external_parameters.fetch(:service_discovery, {})
134+
135+
unless service_discovery.empty?
136+
ServiceDiscovery_Service(:ServiceRegistry) {
137+
NamespaceId Ref(:NamespaceId)
138+
Name service_discovery['name'] if service_discovery.has_key? 'name'
139+
DnsConfig({
140+
DnsRecords: [{
141+
TTL: 60,
142+
Type: 'CNAME'
143+
}],
144+
RoutingPolicy: 'WEIGHTED'
145+
})
146+
if service_discovery.has_key? 'healthcheck'
147+
HealthCheckConfig service_discovery['healthcheck']
148+
else
149+
HealthCheckCustomConfig ({ FailureThreshold: (service_discovery['failure_threshold'] || 1) })
150+
end
151+
}
152+
153+
ServiceDiscovery_Instance(:RegisterInstance) {
154+
InstanceAttributes(
155+
AWS_INSTANCE_CNAME: FnGetAtt('DBCluster','Endpoint.Address')
156+
)
157+
ServiceId Ref(:ServiceRegistry)
158+
}
159+
160+
Output(:ServiceRegistry) {
161+
Value(Ref(:ServiceRegistry))
162+
Export FnSub("${EnvironmentName}-#{external_parameters[:component_name]}-CloudMapService")
163+
}
164+
end
165+
132166
Output(:DBClusterId) {
133167
Value(Ref(:DBCluster))
134168
Export FnSub("${EnvironmentName}-#{external_parameters[:component_name]}-dbcluster-id")

tests/service_registry.test.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test_metadata:
2+
type: config
3+
name: enable service registry
4+
description: If you specify service_registry in the config, it should then expect the namespace component paremeter, and add service_discovery['name'] as the record
5+
6+
service_discovery:
7+
name: db

0 commit comments

Comments
 (0)