In this challenge we're going to learn how to trigger an AWS Lambda function when a file is uploaded to Amazon S3. That AWS Lambda function will take the uploaded file and tweet the image to a Twitter account.
- AWS Tools
- Sign-up for an AWS account
- Install AWS CLI
- .NET Core
- Install .NET Core 1.0 (NOTE: You MUST install 1.0. Do NOT install 1.1 or later!)
- Install Visual Studio Code
- Install C# Extension for VS Code
- AWS C# Lambda Tools
Level 1: Trigger an AWS Lambda function from Amazon S3
For level 1, you will need to create an Amazon S3 bucket, create an AWS IAM role and policy, deploy an AWS Lambda function and use Amazon CloudWatch to debug and monitor. Use us-west-2
aka US West Oregon
region. Below are some hints to help you get started.
AWS IAM role:
-
Create an AWS IAM policy for the AWS Lambda function AWS IAM role.
- Allow it to create and put objects to Amazon CloudWatch.
- If using the console to create the IAM role, choose
CloudWatchLogsFullAccess
. - See policy: aws/policies/lambda_role_policy_level_1.json
-
Create an AWS IAM role for the AWS Lambda function.
- Make sure it has a trust relationship with
lambda.amazonaws.com
.- If using the console to create the IAM role, choose
AWS Lambda
onStep 2 : Select Role Type
. - If you're curious as to the policy it created for your see: aws/policies/lambda_role_trust_relationship.json
- If using the console to create the IAM role, choose
- Attach the policy created above.
- Make sure it has a trust relationship with
Deploy the AWS Lambda starter function:
-
Run the following in terminal as you need, from the
ImageTweeter
directory.- To refresh dependencies:
dotnet restore
- To build the lambda function:
dotnet build
- To publish the lambda function:
dotnet publish
- To deploy the lambda function:
dotnet lambda deploy-function -fn lamda-sharp-image-tweeter
- To refresh dependencies:
Note: On first deployment it will ask you for a role. Choose the role you created earlier. If you do not see it in the list, check your Trust Relationship for the role that it allows lambda.amazonaws.com.
Amazon S3 bucket:
- The bucket name must be unique.
- Add an event on the bucket that triggers the AWS Lambda function.
- For this project you can use images from aws/images or use your own.
ACCEPTANCE TEST: The lambda function is triggered by uploading an image to Amazon S3. Look in Amazon CloudWatch for log message Hello from The AutoMaTweeter!
.
Level 2: Log the bucket and key from the Amazon S3 event
For level 2, modify the LambdaHandler.cs
file in src/ImageTweeter
to read the Amazon S3 event notification. Get the bucket
and key
name from the event and output to Amazon CloudWatch.
You can use the imports from the LambdaHandler.cs
file and the AWS SDK for .NET documentation as a reference.
ACCEPTANCE TEST: The Amazon S3 bucket and key name appear in the Amazon CloudWatch logs.
Level 3: Post the image uploaded to Twitter
For level 3, modify the code to post the uploaded image to Twitter with a message. TweetInvi is the library I used and included it as a depedency in this project. You'll also need to modify the Amazon S3 bucket and AWS IAM policy.
Amazon S3 bucket:
- The bucket needs a policy to allow AWS resources (Principal) to get objects from that bucket.
- See starter policy, you will need to replace some values: aws/policies/lambda_role_policy_level_2.json
AWS IAM role:
- Update the policy for the lambda function role to allow it to get objects from Amazon S3
- See starter policy, you will need to replace some values: aws/policies/lambda_role_policy_level_2.json
Twitter API Keys:
- You will also need 4 Twitter API keys: consumer key, consumer secret, access token, and access token secret, which I will provide for tonight's project.
- You can view the tweets at: https://twitter.com/the_automatweet
- If you'd like keys for your account, you can get them on the Twitter App site.
Note: I had some conflicting namespace and functions at times. If you have this issue, my workaround was to create my own namespace using AmazonRekognitionModel = Amazon.Rekognition.Model;
ACCEPTANCE TEST: After the image is uploaded to Amazon S3 it will post to Twitter.
Boss Level: Analyze the image with Amazon Rekognition
For the boss level, use Amazon Rekognition to tweet keyword descriptions of the image uploaded to Amazon S3.
ACCEPTANCE TEST: The tweet includes an the image uploaded and a list of words that describe the image.
Have fun!! 😄