David Eyers
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
## Lab 10—AWS Lambda (Serverless computing) [Lab 8]: /a8wHsmkVTh6-ud_vNAe2Mw In this lab we will explore AWS Lambda, which is Amazon's Function as a Service (FaaS) platform. ## Navigate to AWS Lambda - In the usual (unusual) AWS Academy Learner Lab way, reach your AWS Management Console. ![image](https://hackmd.io/_uploads/ryKC2bg0A.png) - Using "Find Services" or otherwise, click through to AWS Lambda. ![image](https://hackmd.io/_uploads/ryjX6beAA.png) - Click the "Create function" button that should be near the top right of the web interface you see. You should reach a form from which you can select "Author from scratch" ... ![image](https://hackmd.io/_uploads/HyNSaWe0A.png) - ... and scrolling further down that page, other options that you should set up are shown. In particular - You need to name your function - In the walk-through here Python 3.11 is used—although of course in your own experimentation feel free to use whatever language you want to. - Note that the "Execution role" must be set to the existing role: the AWS Academy accounts do not have sufficient IAM privileges to be able to create roles. ![image](https://hackmd.io/_uploads/BkdnT-x0C.png) - Click "Create function" and you should be taken to a page dedicated to your function. ![image](https://hackmd.io/_uploads/Sk-JRZxCR.png) - The Function overview shows your AWS Lambda function, and the resources it could interact with. The test function currently does not interact with other AWS services—we will later interact with AWS S3. - Scroll down and you will find the "Code source" pane (under the "Code" tab). - In this case the "hello world" type of message will be sufficient for testing. ![image](https://hackmd.io/_uploads/rkvZC-gC0.png) - From the button bar at the top of the page, click the "Test" button, and you will be taken to a page on which you should configure a test event. ![image](https://hackmd.io/_uploads/SJON0ZlA0.png) - In this case there is no need to fine-tune the test event that you are defining. - Lambda maintains the set of test events that you define for each function, so that you can reuse them. - Complete the definition of your test event (likely this just involves giving it a name), save it, and you will be returned to the function designer page. ![image](https://hackmd.io/_uploads/SJTwAWl0A.png) - With the event that you defined selected in the pull-down menu to the right of the "Test" button, click the "Test" button. - You should see the following "Execution result" pane appear. ![image](https://hackmd.io/_uploads/HyViR-xC0.png) - The above execution success includes the data returned from the function, using the HTTP status code 200, to indicate everything is OK, and the body of the request containing the text that was returned by the test function's code. - Now, using the "breadcrumbs" in the top-left of the button bar or otherwise, return to the Lambda page and define a new function. - This time select "Use a blueprint". ![image](https://hackmd.io/_uploads/HJJu1zeA0.png) - Search the "Blueprints" for "s3-get-object-python" and select that blueprint. ![image](https://hackmd.io/_uploads/Sygk31Ge0R.png) - In the "Basic information" pane can can name your function. - Ensure that you select the AWS Academy role, or you will likely encounter missing permission errors. ![image](https://hackmd.io/_uploads/HJVieGl0A.png) - Note that the above blueprint includes an S3 trigger. Scroll down to see the configuration that I used, as shown below. - You will need to specify the S3 bucket to use, and you will not be able to access `dme26-test`. You can use the instructions in [Lab 8] to create an S3 bucket again, noting that it does not need to contain any files. ![image](https://hackmd.io/_uploads/HkCNgMe0C.png) - Scroll up to see the function's code. - Note that the Python function is using the boto3 library, just as we did, in [Lab 8]. ![image](https://hackmd.io/_uploads/S1AIefxAR.png) - When you have read through the Python code, click "Create function" button at the end of the web page. - You will reach the configuration page of your new test function, just as you did for the hello world example, shown previously. - Note that in this case, the S3 trigger is shown. ![image](https://hackmd.io/_uploads/ryDJbMgAC.png) - First, let's try a test event that will fail. - Click "Test" to configure a new test event. - Create your event from the "Amazon S3 Put" template, and give your event a name. ![image](https://hackmd.io/_uploads/HJ5jXze0A.png) (The screenshot below is scrolled down from the screenshot shown above.) - Click the "Save" button to define your test event. ![image](https://hackmd.io/_uploads/BJ767Gg0C.png) - Now, back at the function configuration page, select your test event and click the "Test" button. ![image](https://hackmd.io/_uploads/BkwgEGgRR.png) - You should see an "Execution Result" sub-panel appear. - Note that the stack trace indicates where the problem occurred in the code. - The cause is that an invalid S3 bucket was specified in the test event's template, and we did not change it. ![image](https://hackmd.io/_uploads/HydGEfgC0.png) - The test selection pull-down menu allows you to "Configure test events" to fix the bucket being referred to. ![image](https://hackmd.io/_uploads/H1x4EzxA0.png) - In a separate web browser tab or window, navigate to the S3 bucket that you intend to use. - Selecting the bucket from the S3 console. - Click the "Copy ARN" button, to place the specific Amazon Resource Name into the clipboard. ![image](https://hackmd.io/_uploads/rkndEzlAA.png) - In your other browser tab, where you are editing your test event, paste in your ARN as the value of the "arn" key in the test event's JSON. - Also change the "name" of your "bucket" to the value that you are using. - Note that in the "object" section, the "key" of the object that will be retrieved defaults to `test/key` (URI encoded as `test%2fkey`). - Recall that S3 keys are the names of S3 objects. - You need to change the value `test/key` to an S3 key of an object that is actually in your bucket. - (Also, if you have no objects in your S3 bucket, you will first need to upload one.) ![image](https://hackmd.io/_uploads/B112rGxAC.png) - Save your updated test event. :::info :bulb: Note that we are using fields for the S3 "put" template, despite the function's code reading from an S3 bucket rather than writing to it. So much of the JSON structure is the same, though, that this approach is still faster than setting up the test event from scratch. Irrelevant fields will be ignored. ::: - Now re-run your test event, and if your S3 bucket is set up to host a website (and thus serves an HTML page when you visit the bucket address in a web browser), then hopefully you will see an execution success. - The execution response should indicate the type of the content that the function retrieved from your S3 bucket. :::info :bulb: Recall that [Lab 8] steps through activating website hosting features such as specifying an index file (you will need to first upload such an index file to your S3 bucket). ::: ![image](https://hackmd.io/_uploads/HJt0BfeCR.png) - For more information, you can click on the "Monitor" tab that's just above the "Code source" panel. - This will display CloudWatch metrics related to the operation of your function. (Note that there is sometimes a delay between a function executing and the CloudWatch metrics updating.) ![image](https://hackmd.io/_uploads/Hy9lLzeCR.png) - Scroll down to reach the CloudWatch Logs section. You can see your "Recent invocations" list, and information about the cost of the function. (Depending on what you (or I) have clicked, you may see a different number of invocations) ![image](https://hackmd.io/_uploads/B1yNIflCR.png) - So far we have been manually testing the Lambda function. - An S3 "trigger" is the mechanism that reacts to S3 events. - To view your S3 trigger, select the "Configuration" tab of your function (to the right of the "Monitor" tab you were just using), and click on the "Triggers" left menu item. ![image](https://hackmd.io/_uploads/rkejIfgAC.png) - In a browser window focused on your S3 console, click the "Upload" button. ![image](https://hackmd.io/_uploads/r1d1wfgAA.png) - You should see a panel into which you can drag-and-drop (or otherwise) upload a file. - Select a non-sensitive file with a known file-type and drag-and-drop this into the upload panel. ![image](https://hackmd.io/_uploads/SkrWvzeRR.png) - You can then click the "Upload" button in the bottom-right. ![image](https://hackmd.io/_uploads/Sy-d_MeAA.png) - You should be returned to the S3 overview page, and see your new object (`mascot-cosc349.jp2` in this screen-shot). ![image](https://hackmd.io/_uploads/BJxjdMx0A.png) - Back in your browser window examining your function's logs, you should now see another invocation on your "Recent invocations". (Note again that CloudWatch may not update the Recent invocations immediately.) ![image](https://hackmd.io/_uploads/Bk16uGx0C.png) - Just underneath the "Monitor" tab, there is a button labeled "View CloudWatch Logs". Click this button. ![image](https://hackmd.io/_uploads/SytQFfl0R.png) - You will be taken to the CloudWatch service web pages. - You should see some Log Streams. ![image](https://hackmd.io/_uploads/BkAEFfg0A.png) - Click on the Log Stream related to the event around the time you modified your S3 bucket. - Within that Log Stream, you should see displayed the console output of your Lambda function. ![image](https://hackmd.io/_uploads/rJhUFGlAA.png) - In the case shown above, the content type of the uploaded file is shown as an `binary/octet-stream` is was what I had uploaded. :::info :confused: **Irrelevant aside**: the `jp2` extension is normally used by JPEG-2000 images. Uploading in the past has identified as `image/jp2`, but something changed in 2023 (I am using a different web browser for the operation, for starters). As the name suggests JPEG-2000 is a newer file format inspired by JPEG. In general JPEG-2000 files do a much better job than JPEG in creating small file sizes that retain high quality... however the format has not gained widespread adoption... although it can be used internally within PDF documents as one of the available raster image encoding options. So there you go. ::: - When you reach this point in the lab exercise, you should have convinced yourself that you can attach code that gets run in response to changes to a particular S3 bucket. :::success :pencil: Exercises: - Develop an AWS Lambda function of your own design that notices when objects with names that have a particular prefix (e.g., `images/`) have been uploaded to an S3 bucket and updates content within that bucket. - A first step might be to use S3 put to deposit an object with a related name that contains information about objects you uploaded, e.g., uploading `images/pic1.jpg` might trigger your function, which uploads an information file `info/pic1.jpg.txt`. - Going further, you could develop code that on seeing upload of `images/pic1.jpg` creates a thumbnail, which is stored into `thumb/pic1.jpg`. - There is an ImageMagick example in the function templates, but note that it uses Node.js rather than Python, so may not be intuitive to work with, if you are not familiar with JavaScript. - It is expected that you will need to read documentation of the API functions used to achieve this task. ::: ## Cleaning up You should ensure that you leave your Lambda functions in a safe state. For example, if a Google search crawler, or a potentially malicious script can trigger your function, you may end up causing costs for yourself. For this lab's example, though, generally you would be the only user with write access to your S3 bucket, and thus your Lambda function should probably be safe to leave active. (If concerned, you can delete your Lambda function.)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully