Locked lesson.
About this lesson
Setting up AWS Secret Keys and configuration.
Exercise files
Download this lesson’s related exercise files.
Amazon AWS Part 2.docx59 KB Amazon AWS Part 2 - Solution.docx
60.2 KB
Quick reference
Amazon AWS Part 2
In this video we'll continue installing AWS-SDK and connecting our app to AWS with Security Keys.
When to use
To connect our Heroku app to AWS, we need to configure our production.rb file and add our secret keys to Heroku via the command line terminal.
Instructions
First: Create an "Individual IAM User" at Amazon AWS in the IAM section of the Services menu. Save the Secret Access Key and Key ID in a safe place. We won't use those keys in our app, but you may need them in the future for other things.
Next: In the top right hand corner of AWS, click on your name and choose "My Security Credentials"
Next: Click Access Keys (Access Key ID and Secret Access Key)
Next: Click Create New Access Key, then "Show Access Key"
Next: IMPORTANT, copy the Access Key ID and Secret Access Key to a notepad file
Next: Head over to the Heroku Paperclip AWS help file at https://devcenter.heroku.com/articles/paperclip-s3
Next: Copy this code to your config/environments/production.rb file:
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'
Be sure to change the URL at the end of that code to reflect your own Amazon AWS region (mine is us-west-2 yours may be different).
Next: enter the following 4 commands into the terminal to configure your environmental variables:
heroku config:set S3_BUCKET_NAME=your_bucket_name
heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key
heroku config:set AWS_REGION=your_aws_region
Be sure to change each command to add your specific information after the = sign.
Finally: Save your code to git, push it to github, push it to Heroku and then Heroku run rake db:migrate for good measure.
Hints & tips
- To connect your app to AWS, first create an Individual IAM user
- Next create access keys
- Next add configuration code to your production.rb file
- Next Configure your environmental variables by entering the commands in the terminal
- 00:05 For this video, we need to connect our development environment to AWS.
- 00:08 And we do that sorta like we connected to Git and GitHub with secret keys, SSH keys.
- 00:13 In this case, Amazon has their own little system for that.
- 00:16 So if we head over here, click on Services, and
- 00:19 then scroll down to IAM, this is the security sorta center.
- 00:24 And yours might look a little bit different, I've already set this up.
- 00:26 You wanna create an individual IAM user.
- 00:28 I've already done this, but you need to go ahead and do it.
- 00:30 And you can see I named it John Elder, name it whatever you want.
- 00:33 And when you set this up, they're gonna give you an access key and a secret key.
- 00:37 Go ahead and copy those and paste them somewhere, and
- 00:39 save that file 'cause they'll only give you that information once.
- 00:42 Now, we actually won't be using that, but you might need it in the future, so
- 00:45 you wanna hold onto that.
- 00:46 So once you've done that, you can come back here and click your name, and
- 00:51 then My Security Credentials.
- 00:53 And what you want here is the access keys.
- 00:55 And we wanna create a new access key, so click on that, and
- 00:58 then click the Show Access Key, copy these down.
- 01:01 This is very important.
- 01:03 So I'm gonna copy this, open up my Notepad,
- 01:08 paste that in, copy, copy.
- 01:17 And be sure and highlight every single letter there.
- 01:21 I'm just Ctrl+C and Ctrl+V pasting.
- 01:24 Okay, so that's done.
- 01:26 So now we need to head back to that heroku help file and copy all this.
- 01:33 And you could see, it goes in the config/environments/production.rb file.
- 01:36 And we actually played around with that file earlier.
- 01:38 Head back to our config/environments/production, so
- 01:41 that's config > environments > production.
- 01:44 And you remember,
- 01:45 this is where we put our Heroku URL way back at the beginning of the course.
- 01:49 So if we paste this in here, we need to do one more thing,
- 01:54 head back here, and scroll down, and grab this line of code too.
- 01:59 Copy this, head back, and just tab down a little bit and paste that in there.
- 02:03 And now you'll need to make a change.
- 02:04 Right here, this is us-west-2, which is my region,
- 02:07 you need to paste in whatever your region is, just right there.
- 02:11 And go ahead and save that.
- 02:12 Now, let's take a look at this code right here.
- 02:14 This is public, right?
- 02:16 We're gonna push this code to Github and anyone will be able to see it.
- 02:18 So we don't wanna paste in our secret keys right there because anyone could see.
- 02:23 And if somebody gets a hold of those things,
- 02:24 they can do anything they want with your account.
- 02:26 So we gotta keep this secret.
- 02:27 So Rails gets around this by creating these environmental variables,
- 02:31 that allows us to swap in, but keep it hidden.
- 02:35 So to do that, go ahead and save this file, and come back here and scroll up.
- 02:39 And we need to enter each of these commands into the terminal.
- 02:42 And I'm just gonna paste this in here because we need to make some changes.
- 02:46 So here it's asking for our bucket name, that's this right here,
- 02:50 so it just works as equal your bucket name, just paste that in there.
- 02:54 Now it wants our Access Key ID, that's this guy, so paste that in there.
- 03:00 Next, it wants our secret access key, so paste that in there.
- 03:04 And finally, it's asking for our region.
- 03:06 Mine is us-west-2.
- 03:11 So moment of truth, copy each of these, head back to our terminal,
- 03:15 paste them in, hit Enter.
- 03:20 Grab the next one, don't copy the dollar sign.
- 03:25 Paste the next one, Paste the next one.
- 03:41 Copy and paste the last one.
- 03:42 So let's see if this works.
- 03:45 Let's save our code, git add, period,
- 03:50 git commit -am "added aws',
- 03:54 git push, git push heroku master.
- 03:58 And for good measure, let's run our heroku run rake db:migrate,
- 04:03 since we're dealing with database stuff, it's always a good idea.
- 04:10 So head back over to your heroku site, make sure it's your heroku site,
- 04:13 not your development site.
- 04:15 And let's type in Grumpy, choose our image, click Submit,
- 04:20 and hopefully, if everything went well, fingers crossed.
- 04:26 Yes, so if we right-click here and click Open image in new tab,
- 04:31 we can see that yes, the URL is us-west-2 on Amazon, and everything looks good.
- 04:37 So if we go back, we have the thumbnail, we have the full size one, it's on Amazon.
- 04:43 And if you head back to Amazon, click on this and click on S3, and
- 04:48 then click on your actual bucket, you can see there's stuff in here now.
- 04:53 It's automatically adding these directories.
- 04:56 And if you keep clicking through, here's medium, original, and thumb.
- 05:01 And here's the actual image, if we click on it, you could see here's the URL.
- 05:05 So every time somebody uploads an image,
- 05:06 you'll be able to click through here and actually look at it on AWS if you want.
- 05:10 So I think we're done here, that's all for this video.
Lesson notes are only available for subscribers.