# Exercise 5-4: Importing SQL File from Amazon S3 Bucket into TiDB Cloud

## Overview
+ In this exercise, you import SQL files from Amazon S3 bucket into TiDB Cloud cluster.

## Notes:
+ Before this task, make sure you can use AWS S3 and have example SQL files staged in the bucket.

## Duration
+ This exercise should take you approximately 15 minutes to complete.

## Tasks
1. Get the `TiDB Cloud Account ID` and `TiDB Cloud External ID`.
2. Create access policy for Amazon S3 bucket.
3. Create access role for TiDB Cloud.
4. Import SQL files to TiDB Cloud cluster.

## Solution Steps
1. On the `Clusters` page, click the name of the cluster to enter the `Overview` page of the cluster.
<img src="./diagram/GoDedicatedTierOverview_aa_001_20230111.png" width="50%" align="top"/>

2. Click the `Import` in the left navigation pane.

3. On the `Import` page. If your cluster is `Dedicated Tier`, click `Import Data`; If your cluster is `Serverless Tier`, click `Import Data`.

4. Choose the `AWS Role ARN`, and click `Guide for getting the required Role-ARN` to get the `TiDB Cloud account ID` and `TiDB Cloud External ID` of your cluster. 
<img src="./diagram/ch09-2-02.png" width="50%" align="top"/>

5. Open the [Amazon S3 console](https://console.aws.amazon.com/s3/) and then click `Buckets`.
<img src="./diagram/ch09-2-03.png" width="50%" align="top"/>
  
6. Click `Copy ARN` to get your S3 bucket ARN.
<img src="./diagram/ch09-2-04.png" width="50%" align="top"/>

7. Open the [IAM console](https://console.aws.amazon.com/iam/) and then click `Policies`.
<img src="./diagram/ch09-2-05.png" width="50%" align="top"/>

8. Click `Create Policy`.
<img src="./diagram/ch09-2-06.png" width="50%" align="top"/>

9. On the `Create policy` page, you can choose to click the `Visual editor` tab or click the `JSON` tab.
<img src="./diagram/ch09-2-07.png" width="50%" align="top"/>

If you choose the `JSON` tab to create an access policy, you can refer to the following template.
<img src="./diagram/ch09-2-08.png" width="50%" align="top"/>

```
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:GetObjectVersion"
            ],
            "Resource": "<Your S3 bucket ARN>/universe-dumpling/*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket",
                "s3:GetBucketLocation"
            ],
            "Resource": "<Your S3 bucket ARN>"
        }
    ]
}
```
  
10. Click `Next:Tags`, add a tag, and then click `Next:Review`.
<img src="./diagram/ch09-2-09.png" width="50%" align="top"/>
    
11. On the next page set the policy `Name` and `description`, and then click `Create policy`.
<img src="./diagram/ch09-2-10.png" width="50%" align="top"/>   
 
12. Open the [IAM console](https://console.aws.amazon.com/iam/), and then click `Roles`.
<img src="./diagram/ch09-2-11.png" width="50%" align="top"/> 

13. Click `Create role`.
<img src="./diagram/ch09-2-12.png" width="50%" align="top"/> 
  
14. In the `Trusted entity type` section, choose `AWS account`. Next, in the `An AWS account` section, choose `Another AWS account`, then input the `TiDB Cloud account ID` in `Account ID`. In the `Options` section, click `Require external ID (Best practice when a third party will assume this role)` and then input the `TiDB Cloud External ID` in `External ID`. Click `Next`.
<img src="./diagram/ch09-2-13.png" width="50%" align="top"/> 

15. Choose the policy for the role, and then click `Next`.
<img src="./diagram/ch09-2-14.png" width="50%" align="top"/>

16. In the `Role details` section, set the `Role name` and `Description`, and then click `Create role`.
<img src="./diagram/ch09-2-15.png" width="50%" align="top"/>

17. On the `Roles` page, click the name of the role that you just created.
<img src="./diagram/ch09-2-16.png" width="50%" align="top"/>

18. Note the value for `ARN`. Write this down, you will need it later. 
<img src="./diagram/ch09-2-17.png" width="50%" align="top"/>


19. Go back to the [TiDB Cloud Console](https://tidbcloud.com/console/clusters), click the name of the cluster to enter the `Overview` page of the cluster.
<img src="./diagram/GoDedicatedTierOverview_aa_001_20230111.png" width="50%" align="top"/>

20. Click the `Import` in the left navigation pane.

21. On the `Import` page. If your cluster is `Dedicated Tier`, click `Import Data`; If your cluster is `Serverless Tier`, click `Import Data`.

22. In the `Source` session, select `SQL File` in the `Data format` section, and then fill in the `Bucket URI` of your example SQL file data. In the `Bucket Access` session, choose `AWS Role ARN`, then enter the `Role ARN` you just got. Click `Next`.
<img src="./diagram/ImportSQLData_aa_002_20230202.png" width="50%" align="top"/>

22.  Wait for your data import to finish. After the import task is completed, you can click `Explore your data by Chat2Query` to query your imported data.
<img src="./diagram/ImportSQLFileChat2Query_aa_001_20230223.png" width="50%" align="top"/>

