avatargravity well (Rob Tomlin)

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

6502

Abstract

varchar</span>](<span class="hljs-number">25</span>) <span class="hljs-keyword">NULL</span>, [EmployeeTitle] [<span class="hljs-type">varchar</span>](<span class="hljs-number">50</span>) <span class="hljs-keyword">NULL</span>, <span class="hljs-keyword">CONSTRAINT</span> [PK_Employees] <span class="hljs-keyword">PRIMARY KEY</span> CLUSTERED ( [EmployeeId] <span class="hljs-keyword">ASC</span> ) <span class="hljs-keyword">WITH</span> (PAD_INDEX = <span class="hljs-keyword">OFF</span>, STATISTICS_NORECOMPUTE = <span class="hljs-keyword">OFF</span>, IGNORE_DUP_KEY = <span class="hljs-keyword">OFF</span>, ALLOW_ROW_LOCKS = <span class="hljs-keyword">ON</span>, ALLOW_PAGE_LOCKS = <span class="hljs-keyword">ON</span>) <span class="hljs-keyword">ON</span> [<span class="hljs-keyword">PRIMARY</span>] ) <span class="hljs-keyword">ON</span> [<span class="hljs-keyword">PRIMARY</span>]</pre></div><h1 id="00c7">Enabling RDS SQL Server Integration with S3</h1><p id="c9ea">This is the most important series of steps. Without creating an IAM Policy and Role, we cannot access S3 from RDS. This is a detailed walk-through.</p><h2 id="2870">Create an IAM Policy for access to S3</h2><p id="db27">We need to first build a Policy that will be used to access the S3 bucket and object (employees.csv.)</p><ol><li>In the AWS Console, access IAM.</li></ol><figure id="ab2c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*UY0SYhpPj71i5lnkBp1jEg.jpeg"><figcaption></figcaption></figure><p id="0d4c">2. Click <b>Policies (</b>under Access management<b>) </b>on the left side Dashboard and click<b> Create Policy.</b></p><figure id="a620"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*bKwZ_fcXS_MdJcf6jldpDA.jpeg"><figcaption>Create IAM Policy</figcaption></figure><p id="2cba">3. For <b>Service</b>, click the blue “choose a service” to search. Type S3 in the search box and click on the blue S3 that shows up below the search.</p><figure id="9ec4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jm0mnSR5LLnwAj7TfcNJ9A.jpeg"><figcaption>Search for the S3 Service and Click on it</figcaption></figure><p id="2c7e">4. For <b>Actions</b>, <b>Access Level</b>, choose the following to grant the access that your DB instance requires (from the <b>List </b>and <b>Read </b>sections):</p><p id="7c97"><code>ListAllMyBuckets</code> – required</p><p id="61e4"><code>ListBucket</code> – required</p><p id="7d9c"><code>GetBucketACL</code> – required</p><p id="8961"><code>GetBucketLocation</code> – required</p><p id="e6e0"><code>GetObject</code> – required for downloading files from S3 to <code>D:\S3</code></p><p id="9fdf"><i>Note: There is also a PutObject if you need to export to S3 from SQL Server.</i></p><figure id="dc5b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*UF7l2xOjBZzJZUKKpjt1hg.jpeg"><figcaption>Required Actions to access a bucket and object</figcaption></figure><p id="0c57">5. For <b>Resources</b>, you should see bucket and object options. We will click each and enter the bucket name and object name.</p><figure id="b361"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*UQv3IaL3ZX2Y1F4jWRKuRA.jpeg"><figcaption></figcaption></figure><p id="326b">5. Click the blue <b>Add ARN</b> for bucket and enter our S3 bucket name, sql-server-s3-test. (See image below.)</p><p id="3fd6">This creates the ARN, <b>arn:aws:s3:::sql-server-s3-test.</b></p><p id="fea5">Click Add when done.</p><figure id="c832"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rZ0BF4gkK2PGmw70DVenqA.jpeg"><figcaption>Bucket ARN</figcaption></figure><p id="c78f">6. Click the blue <b>Add ARN</b> and enter our S3 bucket and object (file) name, sql-server-s3-test and employees.csv. (See image below.)</p><p id="12f5">This creates the ARN, arn:aws:s3:::sql-server-s3-test/employees.csv<b>.</b></p><p id="a8c9">Click Add when done.</p><figure id="6eb4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jy29jmsY4B0HTFFIKD5bgw.jpeg"><figcaption>Bucket/Object ARN</figcaption></figure><figure id="542d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*pZZSBBt2LIH7z9DtmmZX9Q.jpeg"><figcaption>Resource Section of our Policy</figcaption></figure><p id="ef54">7. Click <b>Review Policy</b> and give it the name, <b>sql-server-s3-test</b>. Click <b>Create Policy.</b></p><figure id="86fe"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*j5LlhBE7G3LrMlauotOZDA.jpeg"><figcaption>Policy Name</figcaption></figure><figure id="1dd6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*MR7rEscqReFMFzjLHKLQcg.jpeg"><figcaption>Policy Creation Success!</figcaption></figure><h2 id="fca8">Creating an IAM Role that uses the IAM Policy</h2><p id="9ed1">Next we need to create an IAM Role to use the Policy we just created.</p><ol><li>Open your AWS Console and go to IAM again. Choose <b>Roles </b>in the left side Dashboard.</li><li>Click <b>Create Role.</b></li><li>Choose the following as they appear,</li></ol><ul><li><b>AWS service </b>(at the top.)</li><li><b>RDS </b>(in the middle list, “select a service to view use cases”.)</li><li><b>RDS — Add Role to Database </b>(towards the bottom in the “Select your use case” section.)</li></ul><p id="2051">4. Click the <b>Next: Permissions </b>button.</p><p id="34f2">5. For <b>Attach permissions policies</b>, enter the name of the IAM policy , sql-server-s3-test, in the search box.</p><figure id="6e79"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nv1qemVSfdh2x5mxxSxr_Q.jpeg"><figcaption>Policy we created</figcaption></figure><p id="782e">6. Click the <b>box </b>to the left of sql-server-s3-test. <b>Do not click the name itself.</b></p><p id="4c0d">7. Click the Next button twice until you get to step 4. Enter a name for the Role. Use the same name as the policy, sql-server-s3-test, (the name does not have to be the same.)</p><figure id="2907"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rTU8vkrYIuqdTNBPad0F0g.jpeg"><figcaption></figcaption></figure><p id="5e7f">8. Click the <b>Create role</b> button.</p><h2 id="3478">Associating your IAM role with your DB instance</h2><ol><li>Open the AWS Console and go to RDS.</li></ol><p id="24a9">2. Choose the RDS SQL Server DB instance name to display its details (click the name of the RDS.)</p><p id="f97c">3. On the <b>Connectivity & security</b> tab, in the <b>Manage IAM roles</b> section (scroll down to

Options

find this), choose the IAM role to add for <b>Add IAM roles to this instance</b>.</p><p id="94b5">4. To the right, for <b>Feature</b>, choose <b>S3_INTEGRATION</b>.</p><figure id="abfc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*pOiYiyVKY8ZqkRL7p7Cm0Q.jpeg"><figcaption>Final Step for RDS to S3 Integration</figcaption></figure><p id="b2cb">5. Click the <b>Add role</b> button.</p><h2 id="f4bc">Recap</h2><p id="1819">The RDS Integration to S3 required an IAM Policy, Role that uses that Policy and finally a connection of the Role to the RDS instance.</p><h1 id="ead8">Loading the File From S3 to SQL Server</h1><ol><li>Jump back in to SQL Server Management Studio and open a new query window.</li><li>In the query window paste the following. <b>Do not run it yet.</b></li></ol><div id="c456"><pre>exec msdb.dbo.rds_download_from_s3 @<span class="hljs-attribute">s3_arn_of_file</span>=<span class="hljs-string">'arn:aws:s3:::sql-server-s3-test/employees.csv'</span>, @<span class="hljs-attribute">rds_file_path</span>=<span class="hljs-string">'D:\S3\seed_data\data.csv'</span>, @<span class="hljs-attribute">overwrite_file</span>=1;</pre></div><p id="ec99">Notice the ARN to the employees.csv file in our S3 bucket.</p><p id="4766">When executed, this will start a <b>task </b>that will start a download from S3 to D:\S3\seed_data\data.csv on the RDS. (I’d recommend you leave the names as they are for this tutorial.)</p><p id="aecc">Make note of the <b>task_id</b> it creates. You can run the following to check the status of your task. (Note: it can take up to 30 seconds to complete.)</p><div id="7dec"><pre><span class="hljs-keyword">SELECT</span> * <span class="hljs-keyword">FROM</span> msdb.dbo.rds_fn_task_status(<span class="hljs-literal">NULL</span>,task_id);</pre></div><p id="acdc">3. Run the code from step 2 (download from S3) and then run the code from step 3 periodically to monitor progress.</p><figure id="a0ce"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*oZR_AGKYaPnGPKRMiPZ8Tg.jpeg"><figcaption>Task Status</figcaption></figure><h2 id="faa5">Loading The Contents Of The File In To A Table</h2><p id="3f51">After the status is SUCCESS, we can load the data in to the Employees table. Use the script below,</p><div id="02c8"><pre>BULK <span class="hljs-keyword">INSERT</span> Employees <span class="hljs-keyword">FROM</span> <span class="hljs-string">'D:\S3\seed_data\data.csv'</span> <span class="hljs-keyword">WITH</span> ( FIRSTROW = <span class="hljs-number">2</span>, FIELDTERMINATOR = <span class="hljs-string">','</span>, ROWTERMINATOR = <span class="hljs-string">'\n'</span>

 )</pre></div><p id="9d3f"><b>Note</b>: If you have trouble, try changing the ROWTERMINATOR to ‘0x0A’</p><p id="a4eb">Looking at the Employees table we see,</p><figure id="b19d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*M8fP1hi7vrIdYd_tQ2tGig.jpeg"><figcaption>employees.csv to S3 to RDS to Employees table</figcaption></figure><h2 id="31f2">Success!</h2><p id="a8a0">Lots of moving parts so if you run in to any problems, please leave a comment.</p><h1 id="72a3">Conclusion</h1><p id="0258">There were a lot of steps and moving parts from Policy and Role creation to Associating the Role with the RDS. And finally running the rds_download_from_s3 code.</p><p id="170f">This is a very powerful and potentially useful process. Keep in mind that adding PutOption in our Policy, we can also export to S3.</p><p id="142d"><b>Thank you for reading and, again, if you run in to any problems, please leave a comment.</b></p><p id="38fc"><i>Read all you want and help me continue writing by joining Medium.</i></p><div id="ea46" class="link-block">
      <a href="https://bobtomlin-70659.medium.com/membership">
        <div>
          <div>
            <h2>Join Medium with my referral link - gravity well (Rob Tomlin)</h2>
            <div><h3>As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…</h3></div>
            <div><p>bobtomlin-70659.medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*Dke9PllqVNzDuxbx)"></div>
          </div>
        </div>
      </a>
    </div><p id="d115"><a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/User.SQLServer.Options.S3-integration.html">Amazon Documentation</a></p><p id="c227">You may also enjoy:</p><div id="3396" class="link-block">
      <a href="https://javascript.plainenglish.io/how-to-read-a-csv-file-in-node-js-305b94e75024">
        <div>
          <div>
            <h2>How to Read a CSV File in Node.js</h2>
            <div><h3>And Use Classes with Object.assign() to Store The Data</h3></div>
            <div><p>javascript.plainenglish.io</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*1WeuR9OoKyD5EvtT9KjXOA.jpeg)"></div>
          </div>
        </div>
      </a>
    </div><div id="d292" class="link-block">
      <a href="https://javascript.plainenglish.io/how-to-read-an-excel-file-in-node-js-6e669e9a3ce1">
        <div>
          <div>
            <h2>How To Read An Excel File In Node.js</h2>
            <div><h3>It Couldn’t Be Easier</h3></div>
            <div><p>javascript.plainenglish.io</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*sToEh-u8xAhmVc1B9X2NQw.jpeg)"></div>
          </div>
        </div>
      </a>
    </div><div id="cf55" class="link-block">
      <a href="https://aws.plainenglish.io/using-node-js-to-display-images-in-a-private-aws-s3-bucket-4c043ed5c5d0">
        <div>
          <div>
            <h2>Using Node.js to Display Images in a Private AWS S3 Bucket</h2>
            <div><h3>Bypassing the File System and Using Streams</h3></div>
            <div><p>aws.plainenglis</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*LdS66i2n5Jekh-4rnbO9kw.jpeg)"></div>
          </div>
        </div>
      </a>
    </div></article></body>

Cloud-Computing Tips

How to Integrate S3 and a SQL Server RDS Instance

And bulk load the contents in to a table

Image Created by Author

In this tutorial we will walk through in detail how to give your SQL Server RDS Instance the ability to access file(s) (.csv in our example) in an S3 bucket and store it on the RDS Instance. We will then use SQL Server’s BULK INSERT to load the file’s contents in to a table.

You can transfer files between a DB instance running Amazon RDS for SQL Server and an Amazon S3 bucket. By doing this, you can use Amazon S3 with SQL Server features such as BULK INSERT. For example, you can download .csv, .xml, .txt, and other files from Amazon S3 to the DB instance host and import the data from D:\S3\ into the database. All files are stored in D:\S3\ on the DB instance.

Some Limitations

  • The DB instance and the S3 bucket must be in the same AWS Region.
  • Only the following file extensions are supported for download: .bcp, .csv, .dat, .fmt, .info, .lst, .tbl, .txt, and .xml.
  • The S3 bucket can’t be open to the public.

Assumptions For This Tutorial

I will assume you are familiar with Amazon Web Services cloud-based solution for object storage, S3, and have access to S3.

I will also assume you have access to a SQL Server RDS Instance. However, this can be done with other RDS types as well.

What We Will Do

  1. Create a non-public S3 bucket and save a csv file in it.
  2. Create a table in SQL Server into which we will load the csv file.
  3. Enabling RDS SQL Server Integration with S3 (which includes IAM Policy and Role creation.)
  4. Run a Script in SQL Server to download the S3 file to the RDS.
  5. Run a Script in SQL Server to BULK INSERT the file’s contents in to a table.

Let’s Get Started!

Creating the S3 bucket and csv File

  1. Open your Amazon Console and go to S3
AWS Console and Search for S3

2. Click Create Bucket and create an S3 bucket named sql-server-s3-test and choose your region. Remember, the bucket and RDS must be in the same region.

Create bucket
Bucket Name and Region

3. Click Create. Locate the bucket and verify it is not public.

Non-public bucket

4. Create a file named employees.csv with the contents below.

EmployeeId,EmployeeFirst,EmployeeLast,EmployeeTitle
1,Bob,Jones,Software Developer
2,Carol,Stevens,DevOps
3,Ted,Billingsley,Project Manager
4,Alice,Wrest,Database Admin

5. In the S3 console, click the bucket name, sql-server-s3-test, to open the bucket. Click Upload and follow the instructions to upload employees.csv.

Before upload
After upload we see employees.csv

Creating The Employee Table In SQL Server

  1. Open SQL Server Management Studio and un the Script below or create a similar table.
CREATE TABLE [dbo].[Employees](
 [EmployeeId] [int] IDENTITY(1,1) NOT NULL,
 [EmployeeFirst] [varchar](25) NULL,
 [EmployeeLast] [varchar](25) NULL,
 [EmployeeTitle] [varchar](50) NULL,
 CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED 
(
 [EmployeeId] ASC
)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Enabling RDS SQL Server Integration with S3

This is the most important series of steps. Without creating an IAM Policy and Role, we cannot access S3 from RDS. This is a detailed walk-through.

Create an IAM Policy for access to S3

We need to first build a Policy that will be used to access the S3 bucket and object (employees.csv.)

  1. In the AWS Console, access IAM.

2. Click Policies (under Access management) on the left side Dashboard and click Create Policy.

Create IAM Policy

3. For Service, click the blue “choose a service” to search. Type S3 in the search box and click on the blue S3 that shows up below the search.

Search for the S3 Service and Click on it

4. For Actions, Access Level, choose the following to grant the access that your DB instance requires (from the List and Read sections):

ListAllMyBuckets – required

ListBucket – required

GetBucketACL – required

GetBucketLocation – required

GetObject – required for downloading files from S3 to D:\S3\

Note: There is also a PutObject if you need to export to S3 from SQL Server.

Required Actions to access a bucket and object

5. For Resources, you should see bucket and object options. We will click each and enter the bucket name and object name.

5. Click the blue Add ARN for bucket and enter our S3 bucket name, sql-server-s3-test. (See image below.)

This creates the ARN, arn:aws:s3:::sql-server-s3-test.

Click Add when done.

Bucket ARN

6. Click the blue Add ARN and enter our S3 bucket and object (file) name, sql-server-s3-test and employees.csv. (See image below.)

This creates the ARN, arn:aws:s3:::sql-server-s3-test/employees.csv.

Click Add when done.

Bucket/Object ARN
Resource Section of our Policy

7. Click Review Policy and give it the name, sql-server-s3-test. Click Create Policy.

Policy Name
Policy Creation Success!

Creating an IAM Role that uses the IAM Policy

Next we need to create an IAM Role to use the Policy we just created.

  1. Open your AWS Console and go to IAM again. Choose Roles in the left side Dashboard.
  2. Click Create Role.
  3. Choose the following as they appear,
  • AWS service (at the top.)
  • RDS (in the middle list, “select a service to view use cases”.)
  • RDS — Add Role to Database (towards the bottom in the “Select your use case” section.)

4. Click the Next: Permissions button.

5. For Attach permissions policies, enter the name of the IAM policy , sql-server-s3-test, in the search box.

Policy we created

6. Click the box to the left of sql-server-s3-test. Do not click the name itself.

7. Click the Next button twice until you get to step 4. Enter a name for the Role. Use the same name as the policy, sql-server-s3-test, (the name does not have to be the same.)

8. Click the Create role button.

Associating your IAM role with your DB instance

  1. Open the AWS Console and go to RDS.

2. Choose the RDS SQL Server DB instance name to display its details (click the name of the RDS.)

3. On the Connectivity & security tab, in the Manage IAM roles section (scroll down to find this), choose the IAM role to add for Add IAM roles to this instance.

4. To the right, for Feature, choose S3_INTEGRATION.

Final Step for RDS to S3 Integration

5. Click the Add role button.

Recap

The RDS Integration to S3 required an IAM Policy, Role that uses that Policy and finally a connection of the Role to the RDS instance.

Loading the File From S3 to SQL Server

  1. Jump back in to SQL Server Management Studio and open a new query window.
  2. In the query window paste the following. Do not run it yet.
exec msdb.dbo.rds_download_from_s3
@s3_arn_of_file='arn:aws:s3:::sql-server-s3-test/employees.csv',
@rds_file_path='D:\S3\seed_data\data.csv',
@overwrite_file=1;

Notice the ARN to the employees.csv file in our S3 bucket.

When executed, this will start a task that will start a download from S3 to D:\S3\seed_data\data.csv on the RDS. (I’d recommend you leave the names as they are for this tutorial.)

Make note of the task_id it creates. You can run the following to check the status of your task. (Note: it can take up to 30 seconds to complete.)

SELECT * FROM msdb.dbo.rds_fn_task_status(NULL,task_id);

3. Run the code from step 2 (download from S3) and then run the code from step 3 periodically to monitor progress.

Task Status

Loading The Contents Of The File In To A Table

After the status is SUCCESS, we can load the data in to the Employees table. Use the script below,

BULK INSERT Employees
     FROM 'D:\S3\seed_data\data.csv'
     WITH
     (
         FIRSTROW = 2,
         FIELDTERMINATOR = ',',
         ROWTERMINATOR = '\n'
      
     )

Note: If you have trouble, try changing the ROWTERMINATOR to ‘0x0A’

Looking at the Employees table we see,

employees.csv to S3 to RDS to Employees table

Success!

Lots of moving parts so if you run in to any problems, please leave a comment.

Conclusion

There were a lot of steps and moving parts from Policy and Role creation to Associating the Role with the RDS. And finally running the rds_download_from_s3 code.

This is a very powerful and potentially useful process. Keep in mind that adding PutOption in our Policy, we can also export to S3.

Thank you for reading and, again, if you run in to any problems, please leave a comment.

Read all you want and help me continue writing by joining Medium.

Amazon Documentation

You may also enjoy:

S3
Sql Server
Cloud Computing
AWS
Programming
Recommended from ReadMedium