# PCTA Workshop (Japanese) Jumphost setup scripts

This project use AWS CDK to deploy resources.
The `cdk.json` file tells the CDK Toolkit how to execute your app.

## Prerequite

* Need to install aws sdk, cdk
* Create EC2 Keypair and put the name into `cdk.json`
* Modify num of students in `cdk.json`

## AWS login (If you use API-Key, skip this section)

Change aws credentials with workshop account.
Below is example commands if you register a profile as "workshop". 

```bash
aws sso login --profile workshop
export AWS_DEFAULT_PROFILE=workshop
```

## Create Workshop Environment

1. `cdk deploy AwsSetupStack`    Create S3 Bucket for import hands-on for TiDB Cloud
  You don't need this step next time.

2. `ec2_stack.sh` Do the following command
  * `cdk deploy --outputs-file cdk-output.json Ec2SetupStack`     Create Ec2 Instances (jump hosts) for TiDB Cloud hands-on

3. `./get_ip_from_cdkout.sh`     Create IP lists for Students jump hosts

4. `ec2_post_setup.sh [pem file]`  Copy sql and scripts into the user directory on each created host.
  ```bash
  jq --raw-output .Ec2SetupStack.Instructoripaddress cdk-output.json | while read h
  do
    scp -o "StrictHostKeyChecking no" -i ~/.ssh/pcta-workshop-20231019.pem -r ../../tidb-pcta-course-ja-lab ubuntu@$h:/home/ubuntu/
  done
  ```

Now you can get IP list as csv form using jq. 

```bash
cat IP.json | jq -r '[.key,.value] | join(",")' 
```

## generate ppk from pem

on the instructor server

```bash
sudo apt update
sudo apt-get install putty-tools
puttygen pcta-workshop-20231019.pem -o pcta-workshop-20231019.ppk -O private
```

## generate signed URL

copy pem/ppk to S3

:::note
You need to use IAM Access KEY to generate a long-term presign. If you use SSO user, the presigned URL will expire as the SSO user credential expires.
:::


then:
```
aws --profile workshop-sign s3 presign s3://tidb-workshop-pcta-2023-ec2/pcta-workshop-20231019.pem --expires-in 600000 --region ap-northeast-1 --endpoint-url https://s3.ap-northeast-1.amazonaws.com
aws --profile workshop-sign s3 presign s3://tidb-workshop-pcta-2023-ec2/pcta-workshop-20231019.ppk --expires-in 600000 --region ap-northeast-1 --endpoint-url https://s3.ap-northeast-1.amazonaws.com
```

You can share this signed URL to students.

## destroy environment

* `cleanup.sh`  Destroy AWS Resources (EC2 instances) and erase output files.
  * `aws cdk destroy Ec2SetupStack`
