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.
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.
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]
Download Claudia.js
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.
[default]
aws_secret_access_id = INSERT-YOUR-ACCESS-KEY
aws_access_key_id = INSERT-YOUR-SECRET-KEY
[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.
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:
Close your AWS 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!