Cloud Platforms – AWS
MSc in Business Analytics Assignment 2
In this assignment, you will create lambda functions, associate these functions with API Gateways, and interact with s3 buckets. The learning objectives of this assignment are:
– Practice writing and launching lambda functions
– Practice how to create an API endpoint that calls a lambda function using API
– Practice how to trigger a lambda function from a S3 event.
– Practice how to write to buckets with AWS Python’s SDK: boto3.
– Optional: discover how to pass parameters to lambda functions
Deliverables:
– Submit a document with the requested information and screenshots
– Submit the requested python files
Note: The screenshots inside the AWS Console must display your username in the top right corner.
Assignment Instructions
Pre-requisites
• Recap the example shown in class about writing a simple lambda function — summarized in the PDF given available in Moodle after Lecture 4.
• Recap the example shown in class where we created an API with API Gateway – available in the video provided in Moodle after Lecture 4.
• Recap the example shown in class where we interact with S3 buckets from a Lambda function – material available in Moodle after Lecture 5.
• Make sure you understand the problem of recursive invocation of Lambda functions to avoid unnecessary AWS charges.
Code Help, Add WeChat: cstutorcs
Part 1: Create a Lambda function that writes to S3 (3 points)
1. Create a new S3 bucket and name it “MIBA-AWS-2023-Bucket-{YOUR-NAME}-input”
2. Create a new S3 bucket and name it “MIBA-AWS-2023-Bucket-{YOUR-NAME}-
3. Create a lambda function that puts a new file in the input bucket. The name of the
file should contain the current time and have extension “.txt” (e.g., 201735.txt if the time is 20:17 and 35’). The contents of this file should be some text of your choice. Do not associate any trigger to this lambda function.
4. Run the Lambda function a couple of times (by using the “Test” button) to generate a few files in your bucket.
Submissions for this question:
– The python code
– Add a screenshot to the report that shows the policies attached to the role.
Part 2: Create an API with API Gateway (3 points)
1. Use API Gateway to create an HTTP API that has an endpoint called “write” that is integrated with this lambda function.
2. Verify that you can call this function using the API and that the file in the S3 bucket is updated.
Submissions for this question:
– The python code
– Include the domain name of the API gateway created by AWS in the report.
– Include a screenshot of the API call in the report, showing the HTTP address and
Part 3: Create a Lambda function that is triggered by S3 (4 points)
1. Create a lambda function that is triggered each time that a new “txt” file is put in the input bucket (for security, add “txt” to the suffix when defining the S3 trigger).
2. That lambda function should write a file to the output bucket, whose name is the current time, and its extension is “.md”. The content of the file should be a list of all the files available in the input bucket.
Hint 1: check this example to learn how to list files using boto3
Hint 2: Consider that you will need some additional permissions to list objects in a bucket.
浙大学霸代写 加微信 cstutorcs
IT IS VERY IMPORTANT that the trigger is associated to the input bucket, and that the lambda function code writes to the output bucket, to avoid recursive calls and unexpected charges. Work with the indicated file extensions as an additional prevention measure.
Submissions for this question:
– The python code
– Add a screenshot to the report showing the permissions of the lambda function.
Voluntary part (not necessary to get the full mark, but gives up to 3 bonus points)
Theory for this part: capturing parameters from an API call in a Lambda function
In class we learned that HTTP APIs can be called using the format http://(domain)/(endpoint)?param=value1
We used API Gateway to do simple calls to lambda functions, without parameters.
In API Gateway, when an endpoint is called, if parameters are present, these are sent to the lambda function. The question is how these values are accessible from inside the lambda_handler function.
If you noticed, the lambda_handler function takes two arguments, event and context. The first argument is a python dictionary that contains information relative to the event that triggered the Lambda function. In the case of an API Gateway call, the key
event[‘queryStringParameters’]
points to another dictionary with the parameters of the API call. Specifically, the value of a parameter called e.g. “number” would be recovered as
event[‘queryStringParameters’][‘number’]
Create a simple lambda function and link it to API Gateway
1. Use the AWS Management Console to create a new AWS Lambda function called “testLambdaFunction” using the “Python 3.9” runtime.
1 Note AWS API Gateway creates APIs that work over HTTPS (a secure version of HTTP), but it is called in the same way (just replace http by https).
2. In the function code editor, write a Python function that returns the square of a parameter called “number” (as discussed above).
3. Remember the first time you click on “Test” to run the lambda function it asks to define a “Test event” with some parameters. In class we did not need that step. However, now you can enter the following json, which will simulate that the lambda function received a value of 5:
“queryStringParameters”: {
“number”: 5 }
This exercise uses elements that are covered by the AWS Free Tier. After the exercise, it is recommended to release all resources (delete all the files that have been generated, the functions and delete the API).
4. Use API Gateway to create an HTTP API that has an endpoint called “square” that is integrated with this lambda function. When done, verify that you can call the API (using the right endpoint and parameters) and get the expected result.
程序代写 CS代考 加微信: cstutorcs