Skip to content

Commit f051aba

Browse files
committed
Update readme with ssm connect option
1 parent feb7ffc commit f051aba

File tree

4 files changed

+92
-9
lines changed

4 files changed

+92
-9
lines changed

README.md

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,16 @@ type ProjectArgs = {
7878
)[];
7979
environment: Environment;
8080
hostedZoneId?: pulumi.Input<string>;
81+
enableSSMConnect?: pulumi.Input<boolean>;
8182
};
8283
```
8384

84-
| Argument | Description |
85-
| :------------- | :---------------------------------------------------------------------: |
86-
| services \* | Service list. |
87-
| environment \* | Environment name. |
88-
| hostedZoneId | Route53 hosted zone ID responsible for managing records for the domain. |
85+
| Argument | Description |
86+
| :--------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------: |
87+
| services \* | Service list. |
88+
| environment \* | Environment name. |
89+
| hostedZoneId | Route53 hosted zone ID responsible for managing records for the domain. |
90+
| enableSSMConnect | Setup ec2 instance and SSM in order to connect to the database in the private subnet. Please refer to the [SSM Connect](#ssm-connect) section for more info. |
8991

9092
```ts
9193
type DatabaseService = {
@@ -282,9 +284,90 @@ export type WebServerArgs = {
282284
};
283285
```
284286

287+
## SSM Connect
288+
289+
The [Database](#database) component deploys a database instance inside a private subnet,
290+
and it's not publicly accessible from outside of VPC.
291+
<br>
292+
In order to connect to the database we need to deploy the ec2 instance which will be used
293+
to open an SSH tunnel to the database instance.
294+
<br>
295+
Because of security reasons, ec2 instance is also deployed inside private subnet
296+
which means we can't directly connect to it. For that purpose, we use AWS System Manager
297+
which enables us to connect to the ec2 instance even though it's inside private subnet.
298+
299+
![AWS RDS connection schema](/assets/images/ssm-rds.png)
300+
301+
**Prerequisites**
302+
303+
1. Install the [Session Manager plugin](https://docs.aws.amazon.com/systems-manager/latest/userguide/install-plugin-macos-overview.html#install-plugin-macos)
304+
2. Generate a new ssh key pair or use the existing one.
305+
306+
```bash
307+
$ ssh-keygen -f my_rsa
308+
```
309+
310+
3. Set stack config property by running:
311+
312+
```bash
313+
$ pulumi config set ssh:publicKey "ssh-rsa Z...9= [email protected]"
314+
```
315+
316+
SSM Connect can be enabled by setting `enableSSMConnect` property to `true`.
317+
318+
```ts
319+
const project = new studion.Project('demo-project', {
320+
enableSSMConnect: true,
321+
...
322+
});
323+
324+
export const ec2InstanceId = project.ec2SSMConnect?.ec2.id;
325+
```
326+
327+
Open up your terminal and run the following command:
328+
329+
```bash
330+
$ aws ssm start-session --target EC2_INSTANCE_ID --document-name AWS-StartPortForwardingSession --parameters '{"portNumber":["22"], "localPortNumber":["9999"]}'
331+
```
332+
333+
Where `EC2_INSTANCE_ID` is an ID of the EC2 instance that is created for you. ID can be
334+
obtained by exporting it from the stack.
335+
336+
Next, open another terminal window and run the following command:
337+
338+
```bash
339+
$ ssh ec2-user@localhost -p 9999 -N -L 5555:DATABASE_ADDRESS:DATABASE_PORT -i SSH_PRIVATE_KEY
340+
```
341+
342+
Where `DATABASE_ADDRESS` and `DATABASE_PORT` are the address and port of the database instance,
343+
and `SSH_PRIVATE_KEY` is the path to the SSH private key.
344+
345+
And that is it! 🥳
346+
Now you can use your favorite database client to connect to the database.
347+
348+
![RDS connection](/assets/images/rds-connection.png)
349+
350+
It is important that for the host you set `localhost` and for the port you set `5555`
351+
because we have an SSH tunnel open that forwards traffic from localhost:5555 to the
352+
DATABASE_ADDRESS:DATABASE_PORT. For the user, password, and database field, set values
353+
which are set in the `Project`.
354+
355+
```ts
356+
const project = new studion.Project('demo-project', {
357+
enableSSMConnect: true,
358+
services: [
359+
{
360+
type: 'DATABASE',
361+
dbName: 'database_name',
362+
username: 'username',
363+
password: 'password',
364+
...
365+
}
366+
]
367+
});
368+
```
369+
285370
## 🚧 TODO
286371

287-
- [x] Allow connection with RDS via ec2 instance
288-
- [x] Execute commands from ecs service
289372
- [ ] Add worker service for executing tasks
290373
- [ ] Update docs, describe each service, describe required stack configs...

assets/images/rds-connection.png

44.4 KB
Loading

assets/images/ssm-rds.png

86.3 KB
Loading

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
"dist"
2525
],
2626
"scripts": {
27-
"build": "npm run clean && tsc",
2827
"clean": "rm -rf dist",
28+
"build": "npm run clean && tsc",
2929
"format": "prettier -w .",
30-
"release": "release-it",
30+
"release": "npm run build && release-it",
3131
"test": ""
3232
},
3333
"prettier": "@studion/prettier-config",

0 commit comments

Comments
 (0)