<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/taking-advanced-analytics-to-the-cloud-part-i-r-on-aws/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://stanke.co/r-on-aws-cloud/"> R – Luke Stanke</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>

<div>
<p><br>
</p>
<p></p>
<p>Running R on the cloud isn’t very difficult. This demo shows how to get Rstudio running on Amazon Web Services. To run R on the cloud we need to initiate a machine/computer and install R – that’s all very simple. Where you might get caught up is in the settings and permissions.</p>
<div>
<h3>Step 1: Create an AWS Account</h3>
<p>If you don’t have an AWS account you’ll need to sign up.</p>
</div>
<div>
<h3>Step 2: Create an Role</h3>
<p>Once you are signed-up and signed into AWS the first thing you’ll need to do is create a role for your machine. A role provides the necessary permissions for your machine to access various APIs available by AWS.</p>
<p>You can do this by searching IAM on the services tab.</p>
<div>
<img src="https://i2.wp.com/stanke.co/wp-content/uploads/2017/06/aws_iam_search.png?w=456" data-recalc-dims="1">
</div>
<p>Once you are on the page click “Roles” on the left menu, then the “Create New Role” button.</p>
<div>
<img src="https://i2.wp.com/stanke.co/wp-content/uploads/2017/06/aws_iam_roles.png?w=456" data-recalc-dims="1">
</div>
<p>Then select Amazon EC2 from the AWS Service Roll.</p>
<div>
<img src="https://i0.wp.com/stanke.co/wp-content/uploads/2017/06/aws_iam_service_type.png?w=456" data-recalc-dims="1">
</div>
<p>There are a number of services that you could run with your instance that you will set up – and there are varying permissions, too. For now, just search and select <strong>AmazonS3FullAccess</strong>, <strong>AmazonEC2FullAccess</strong>, <strong>RedshiftFullAccess</strong>, and <strong>AthenaFullAccess</strong>. You really won’t need anyt of these right away, but they will be useful when connecting to other services like S3 or another EC2 instance. <em>Note: photo does not include <strong>AthenaFullAccess</strong> but you should include it!</em></p>
<div>
<img src="https://i2.wp.com/stanke.co/wp-content/uploads/2017/06/aws_iam_attach_policy.png?w=456" data-recalc-dims="1">
</div>
<p>From there you’ll be good to go! Depending on your access needs your policies should be selected accordingly. In fact, if you were doing this correctly, you’d want to create policies that are not over-arching like the full access options we’ve selected for this demo.</p>
<div>
<img src="https://i0.wp.com/stanke.co/wp-content/uploads/2017/06/aws_iam_final_policy.png?w=456" data-recalc-dims="1">
</div>
</div>
<div>
<h3>Step 3: Create a Key Pair</h3>
<p>Next, you’ll want to create a key pair. This will allow you to securely log into your instance to update your machine and install Rstudio.</p>
<p>Go to Services and search EC2 in the search bar. Once the EC2 page loads, click “Key Pairs” under the “Network &amp; Security” section on the left menu bar.</p>
<div>
<img src="https://i2.wp.com/stanke.co/wp-content/uploads/2017/06/aws_ec2_create_keypair.png?w=456" data-recalc-dims="1">
</div>
<p>From there click “Create Key Pair”. Give your key a name and hit create. The key pair will download as a .pem file. <strong>Do not lose this key! Also: do not share this key!</strong></p>
</div>
<div>
<h3>Step 4: Create a R/Rstudio/Shiny Security Group</h3>
<p>As you can see by the steps this far its all about security. From the EC2 menu, under the “Network &amp; Security” section on the left menu select “Security Groups”. Click the “Create Security Group Button” and a pop-up will appear. Create a security group name called “Rstudio/Shiny”. Under description write “Allows port access for R and Shiny”. You can leave the VPC drop down alone. On the inbound tab – the default – add three new rules.</p>
<p>Under the first rule select SSH from the dropdown. The source should be auto populated with 0.0.0.0/0. This opens up SSH to anywhere in the world – but you’ll need the .pem key to login.</p>
<p>For the second rule leave it as custom TCP and type 8787 as the port range. This is the port that needs to be opened on the server for you to connect to Rstudio. Under source type 0.0.0.0/0. This means you could log into Rstudio from any IP address. You could also just use your own IP address if you know it, too.</p>
<p>For the third rule leave it as custom TCP also and type 3838 as the port range. This is the port for the Shiny connection. Were not going to use it for the immediate demo but it’ll be very useful in the future. Under source type 0.0.0.0/0 as well.</p>
<div>
<img src="https://i2.wp.com/stanke.co/wp-content/uploads/2017/06/aws_ec2_security_groups.png?w=456" data-recalc-dims="1">
</div>
</div>
<div>
<h3>Step 5: Launch EC2 Instance</h3>
<p>Staying in the EC2 Section click “Instance” under the “Instances” Section on the left menu. Click the “Launch Instance” button. This will take you to “Step 1: Choose an Amazon Machine Image”.</p>
<div>
<img src="https://i1.wp.com/stanke.co/wp-content/uploads/2017/06/aws_ec2_ami.png?w=456" data-recalc-dims="1">
</div>
<p>You’ll have a number of tabs to select AMIs from. Stay on the Quick Start tab and select Amazon Linux AMI – it’s the first option. It has a number of pre-built tools that are useful – but it doesn’t have R installed. You’ll do that in a bit.</p>
<p>For “Step 2: Choose an Instance Type” choosing an instance type can be daunting. Here you are essentially specifying the type of computer you want to run. I typically select from the <strong>General purpose</strong>, <strong>Compute optimized</strong>, or <strong>Memory optimized</strong> options depending on the type of models I’m running and the type of data I am working with. For this example select <strong>t2.micro</strong> because this is just demo the tool. Click “Next: Configure Instance”. <em>Note: you’ll need a more powerful machine to install packages Step 8 and beyond. I’d recommend a c2.large machine just to be safe.</em></p>
<p>For “Step 3: Configure Instance”, under “IAM Role” select the role you created earlier – my role was called EC2- S3-Redshift. <em>Note: Under Advanced Options you could send a bash script to configure your instance, instead we’ll do it with command line tools.</em> Click “Next: Add Storage”.</p>
<div>
<img src="https://i0.wp.com/stanke.co/wp-content/uploads/2017/06/aws_ec2_select_role.png?w=456" data-recalc-dims="1">
</div>
<p>For “Step 4: Add Storage” we can stick with the default settings – for now. Click “Next: Add Tags”</p>
<p>For “Step 5: Add Tags”, click “Add Tag”, enter a key of “Name” and a value of “Rstudio Example” – this will give our machine a name of “Rstudio Example”. Having a name is important when you have more than one machine running. Click “Next: Configure Security Group”</p>
<div>
<img src="https://i2.wp.com/stanke.co/wp-content/uploads/2017/06/aws_ec2_add_tags.png?w=456" data-recalc-dims="1">
</div>
<p>For “Step 6: Configure Security Group”, under “Assign a Security Group” select “Select an existing security group”. Find and select your “Rstudio/Shiny” security group. If this is your first time doing this, it should be fine. If you have multiple security groups like myself you’ll have to search through the lot. Click “Review and Launch”.</p>
<div>
<img src="https://i1.wp.com/stanke.co/wp-content/uploads/2017/06/aws_ec2_select_security_group.png?w=456" data-recalc-dims="1">
</div>
<p>Under the review screen, make sure you’ve selected instance types, security groups, and IAM roles as mentioned above. Click “Launch” – you’ll get a pop-up to select an existing key pair or create a new key pair. Choose an existing key pair and select the key pair you just created. My key is called awesome_key. Awknowledge you’ve selected the key pair and you’ll need it to log on. <strong><em>Note: make sure you have the key pair to log into your instance. This should go without saying, but I’m saying it – you need your key pair to log into your machine and set it up!</em></strong> Launch your instance.</p>
</div>
<div>
<h3>Step 6: Login to your instance</h3>
<p>This is where the Windows OS world diverges from the Apple/Unix/Linux/Ubuntu worlds. Windows doesn’t have a built-in terminal like these other machines so you’ll have to download and setup <a rel="nofollow" href="https://www.chiark.greenend.org.uk/~sgtatham/putty/" target="_blank">PuTTy</a> if you don’t have it already. Next you’ll use your terminal to SSH into your newly created instance and set it up. The instance will likely need about 60 seconds to setup from when you hit launch.</p>
<p>After you launch click on your instance id just to the right of the message saying the instance launches has been initiated.</p>
<div>
<img src="https://i2.wp.com/stanke.co/wp-content/uploads/2017/06/aws_ec2_see_instance.png?w=456" data-recalc-dims="1">
</div>
<p>This will take you to your instance on the EC2 dashboard. The dashboard is full of important information – most notably it’ll tell you if your instance is up and running. From the image below you can see my instance state is green running. This tells me it’s ready to be SSH’ed into.</p>
<div>
<img src="https://i0.wp.com/stanke.co/wp-content/uploads/2017/06/aws_ec2_current_instance.png?w=456" data-recalc-dims="1">
</div>
<p>You can also see the Public DNS on the right and on the description tab. We’ll need that information to SSH into it. *Note: you can also see my IAM Role and key pair name. Click the “Connect” button just to the right of the “Launch Instance Button”. This provides you with addition directons on how to connect to your instance.</p>
<div>
<img src="https://i2.wp.com/stanke.co/wp-content/uploads/2017/06/aws_ec2_steps_to_connect.png?w=456" data-recalc-dims="1">
</div>
<p>First, let’s open our terminal and change directories so that we are in the folder that contains our .pem key. If you haven’t moved it out of your downloads folder, it’s probably there – and you should probably move it.</p>
<div>
<img src="https://i0.wp.com/stanke.co/wp-content/uploads/2017/06/aws_terminal_cd.png?w=456" data-recalc-dims="1">
</div>
<p>Next change the permissions of your key pair to allow you to SSH onto your AWS instance.</p>
<pre></pre></div></div>