Skip to content

Commit 6578b4c

Browse files
committed
Remove ssh keypair, update readme
1 parent bf04abc commit 6578b4c

File tree

3 files changed

+12
-43
lines changed

3 files changed

+12
-43
lines changed

README.md

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,12 @@ The [Database](#database) component deploys a database instance inside a private
376376
and it's not publicly accessible from outside of VPC.
377377
<br>
378378
In order to connect to the database we need to deploy the ec2 instance which will be used
379-
to open an SSH tunnel to the database instance.
379+
to forward traffic to the database instance.
380380
<br>
381-
Because of security reasons, ec2 instance is also deployed inside private subnet
381+
Because of security reasons, the ec2 instance is also deployed inside a private subnet
382382
which means we can't directly connect to it. For that purpose, we use AWS System Manager
383-
which enables us to connect to the ec2 instance even though it's inside private subnet.
383+
which enables us to connect to the ec2 instance even though it's inside a private subnet.
384+
The benefit of using AWS SSM is that we don't need a ssh key pair.
384385

385386
![AWS RDS connection schema](/assets/images/ssm-rds.png)
386387

@@ -392,18 +393,6 @@ which enables us to connect to the ec2 instance even though it's inside private
392393
$ brew install --cask session-manager-plugin
393394
```
394395

395-
2. Generate a new ssh key pair or use the existing one.
396-
397-
```bash
398-
$ ssh-keygen -f my_rsa
399-
```
400-
401-
3. Set stack config property by running:
402-
403-
```bash
404-
$ pulumi config set ssh:publicKey "ssh-rsa Z...9= [email protected]"
405-
```
406-
407396
SSM Connect can be enabled by setting `enableSSMConnect` property to `true`.
408397

409398
```ts
@@ -418,30 +407,23 @@ export const ec2InstanceId = project.ec2SSMConnect?.ec2.id;
418407
Open up your terminal and run the following command:
419408

420409
```bash
421-
$ aws ssm start-session --target EC2_INSTANCE_ID --document-name AWS-StartPortForwardingSession --parameters '{"portNumber":["22"], "localPortNumber":["9999"]}'
422-
```
423-
424-
Where `EC2_INSTANCE_ID` is an ID of the EC2 instance that is created for you. ID can be
425-
obtained by exporting it from the stack.
426-
427-
Next, open another terminal window and run the following command:
428-
429-
```bash
430-
$ ssh ec2-user@localhost -p 9999 -N -L 5555:DATABASE_ADDRESS:DATABASE_PORT -i SSH_PRIVATE_KEY
410+
$ aws ssm start-session --target EC2_INSTANCE_ID --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters '{"host": ["DATABASE_ADDRESS"], "portNumber":["DATABASE_PORT"], "localPortNumber":["5555"]}'
431411
```
432412

433-
Where `DATABASE_ADDRESS` and `DATABASE_PORT` are the address and port of the database instance,
434-
and `SSH_PRIVATE_KEY` is the path to the SSH private key.
413+
Where `EC2_INSTANCE_ID` is an ID of the EC2 instance that is created for you
414+
(ID can be obtained by exporting it from the stack), and
415+
`DATABASE_ADDRESS` and `DATABASE_PORT` are the address and port of the
416+
database instance.
435417

436418
And that is it! 🥳
437419
Now you can use your favorite database client to connect to the database.
438420

439421
![RDS connection](/assets/images/rds-connection.png)
440422

441423
It is important that for the host you set `localhost` and for the port you set `5555`
442-
because we have an SSH tunnel open that forwards traffic from localhost:5555 to the
443-
DATABASE_ADDRESS:DATABASE_PORT. For the user, password, and database field, set values
444-
which are set in the `Project`.
424+
because we are port forwarding traffic from
425+
localhost:5555 to DATABASE_ADDRESS:DATABASE_PORT.
426+
For the user, password, and database field, set values which are set in the `Project`.
445427

446428
```ts
447429
const project = new studion.Project('demo-project', {

src/components/ec2-ssm-connect.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const awsRegion = config.require('region');
77

88
export type Ec2SSMConnectArgs = {
99
vpc: awsx.ec2.Vpc;
10-
sshPublicKey: pulumi.Input<string>;
1110
tags?: pulumi.Input<{
1211
[key: string]: pulumi.Input<string>;
1312
}>;
@@ -19,7 +18,6 @@ export class Ec2SSMConnect extends pulumi.ComponentResource {
1918
ec2MessagesVpcEndpoint: aws.ec2.VpcEndpoint;
2019
ssmMessagesVpcEndpoint: aws.ec2.VpcEndpoint;
2120
ec2: aws.ec2.Instance;
22-
sshKeyPair: aws.ec2.KeyPair;
2321

2422
constructor(
2523
name: string,
@@ -97,21 +95,12 @@ export class Ec2SSMConnect extends pulumi.ComponentResource {
9795
{ parent: this, dependsOn: [ssmPolicyAttachment] },
9896
);
9997

100-
this.sshKeyPair = new aws.ec2.KeyPair(
101-
`${name}-ec2-keypair`,
102-
{
103-
publicKey: args.sshPublicKey,
104-
},
105-
{ parent: this },
106-
);
107-
10898
this.ec2 = new aws.ec2.Instance(
10999
`${name}-ec2`,
110100
{
111101
ami: 'ami-067d1e60475437da2',
112102
associatePublicIpAddress: false,
113103
instanceType: 't2.micro',
114-
keyName: this.sshKeyPair.keyName,
115104
iamInstanceProfile: ssmProfile.name,
116105
subnetId,
117106
vpcSecurityGroupIds: [this.ec2SecurityGroup.id],

src/components/project.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ export class Project extends pulumi.ComponentResource {
7979
this.createServices(services);
8080

8181
if (args.enableSSMConnect) {
82-
const sshConfig = new pulumi.Config('ssh');
8382
this.ec2SSMConnect = new Ec2SSMConnect(`${name}-ssm-connect`, {
8483
vpc: this.vpc,
85-
sshPublicKey: sshConfig.require('publicKey'),
8684
});
8785
}
8886

0 commit comments

Comments
 (0)