gabrie-lhilarion / workshop-ws-5-8-aws-ijemmaonwuzulike

workshop-ws-5-8-aws created by GitHub Classroom

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CS52 Workshops: AWS (Amazon Web Services) Workshop

Authors: Katherine Bernardez, Robert Crawford, Alexander Danilowicz, Lindsey Hodel, Thomas Kim, Stephen Liao

Overview

We will be making a Slack app that sends the top five stories from Hacker News to your Slack channel. Here is the flow:

This image is from this Medium article, which we based our workshop off of because it is so awesome!

What you will learn

  • Create an AWS Account
  • Set up Claudia
  • Deploy a Lambda function to the cloud
  • How to integrate APIs (in this case Hacker News) with AWS
  • How to configure awscli
  • How to be an AWS god

Setup

As usual, fork this repo!

Change the name of the directory to workshop-ws-5-8-aws-YOUR-NAME.

Also change the name in package-lock.json and package.json to match your project directory name.

πŸ’­ You need to do this because when Claudia uploads a Lambda function to AWS, it uses the directory name as the function name and if everyone has the same directory name then there will be conflicting function names and no one will be able to deploy.

Sign up for a free AWS account

Click here and then on Create A Free Account. Enter your information. We chose Personal Account, but it should not matter. You will have to enter your credit card information because after the first 12 months AWS starts to bill you (do not worry we will cancel our account at the end of class).

Step by Step

Download AWS-CLI

pip install awscli

Configure AWS-CLI with your proper credentials

On the AWS website in the top right click on your account name and then on My Security Credentials.

πŸ’­ Normally you would want to get started with IAM users for security reasons, but since this is a quick workshop and we will be canceling our AWS account just click on Continue to Security Credentials.

Expand the Access keys tab and click on Create New Access Key. Hold on to the access key and secret key.

Run aws configure.

On the command line, enter your access key and secret key as they come up.

AWS Access Key Id = COPY-PASTE-YOUR-ACCESS-KEY
AWS Secret Key = COPY-PASTE-YOUR-SECRET-KEY
Default Region name = us-east-2
Default Output format = [do not worry about this, hit enter]

❗ Note that we are working in us-east-2 as our region. (Fun fact: this is the AWS center located in Ohio, which is closest to Hanover!)

Download Claudia.js

πŸ’­ Claudia.js is a nice package that allows us to treat infrastructure as code. It lets us take any function we write and upload it as a Lambda function. We could do this manually on AWS, by clicking around through a bunch of menus, but that wouldn't be very software engineer of us.

npm install claudia -g

Set up Claudia.js

Open up your ~/.aws/credentials file by running atom ~/.aws/credentials in Terminal and make it look like this.

❗ Your current file should look like this:

[default]
aws_secret_access_id = INSERT-YOUR-ACCESS-KEY
aws_access_key_id = INSERT-YOUR-SECRET-KEY

πŸ’― You should change it so it looks like this! In other words, replace [default] with [claudia].

[claudia]
aws_secret_access_id = INSERT-YOUR-ACCESS-KEY
aws_access_key_id = INSERT-YOUR-SECRET-KEY

And then, set AWS_PROFILE environment variable to Claudia with the following command in your top-level project directory: export AWS_PROFILE=claudia.

To make sure that we did that correctly, run echo $AWS_PROFILE and it should return the value claudia.

Configure Slack WebHooks

We have already provided you with the code, so no need to copy and paste πŸ˜„

Now, to set up our Slack personal channel! Click here, go to Add Configuration and in the drop down menu select Privately to yourself.

Click on Add Incoming WebHooks Integration, copy the Webhook URL and replace the current URL in env.json.

Deploy to AWS with Claudia

claudia create --region us-east-2 --handler index.handler --timeout 10 --set-env-from-json env.json

You should now see claudia.json in your directory. It should look like this:

The "role" and "name" shouldn't match the below but they should be there.

{
  "lambda": {
    "role": "workshop-ws-5-8-aws-YOUR-NAME-executor",
    "name": "workshop-ws-5-8-aws-YOUR-NAME",
    "region": "us-east-2"
  }
}

Run:

aws events put-rule --name hackerNewsDigest --schedule-expression 'cron(0/59 * * * ? *)'

This sets the Slack message as an event to trigger every hour (potential extra credit could be for someone to make this trigger once a day rather than every hour).

Running the above command will output the arn of the task. The output will look something like:

{
    "RuleArn": "arn:aws:events:us-east-2:860157212032:rule/hackerNewsDigest"
}

Now, run this function with the RuleArn string in place of INSERT-YOUR-ARN and your project directory name in place of workshop-ws-5-8-aws-YOUR-NAME.

❗ You run this function in your terminal, but we recommend editing this command in Atom or your favorite text editor, so you do not mess up the syntax!

aws lambda add-permission \
  --statement-id 'hackernews-scheduled-messages' \
  --action 'lambda:InvokeFunction' \
  --principal 'events.amazonaws.com' \
  --source-arn INSERT-YOUR-ARN \
  --function-name workshop-ws-5-8-aws-YOUR-NAME \
  --region us-east-2

ruleArn is the output from the aws events... command. function-name can also be found by going to the AWS Console -> Services -> Lambda -> Functions.

Now, let's test it! Run claudia test-lambda. You should see a Slack message in your personal channel.

It should look like this:

.

πŸŽ‰ You are all done! πŸŽ‰

Close your AWS Account

❗ There is no reason you should get billed, but better safe than sorry. Since we do not want you to get billed, close your account! Go to My Account and at the bottom of the page click Close Account. πŸ’°

Summary / What you Learned

  • Create an AWS Account
  • Set up Claudia
  • Deploy a Lambda function to the cloud
  • How to integrate APIs (in this case Hacker News) with AWS
  • How to configure awscli
  • How to be an AWS god

Extra Credit

The world is your oyster!

  • Write a custom Cron expression to change the frequency of the Lambda call. Documentation here. It is a super long read, but useful explanations of Serverless, Claudia and Lambda.
  • Use a different news API. This would involve editing the Javascript functions we wrote, take a look!

Resources

About

workshop-ws-5-8-aws created by GitHub Classroom


Languages

Language:JavaScript 100.0%