awsdocs / aws-doc-sdk-examples

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.
Apache License 2.0
9.49k stars 5.61k forks source link

Rust Lambda MVP #4572

Closed DavidSouther closed 1 year ago

beqqrry-aws commented 1 year ago

You should probably also take a look at this page while doing this work. It looks pretty out of date.

DavidSouther commented 1 year ago

Out of date / covered by @brmur's updates to the guide.

DavidSouther commented 1 year ago

Implement the scenario and service action calls to create examples for each SDK.

Service actions

Service actions can either be pulled out as individual functions or can be incorporated into the scenario, but each service action must be included as an excerpt in the SOS output.

Scenario

A scenario runs at a command prompt and prints output to the user on the result of each service action. A scenario can run in one of two ways: straight through, printing out progress as it goes, or as an interactive question/answer script.

Getting started with functions

Use an SDK to manage Lambda functions: create a function, invoke it, update its code, invoke it again, view its output and logs, and delete it.

This scenario uses two lambda handlers: Note: Handlers don't use AWS SDK API calls

The increment handler is very simple:

  1. It accepts a number, increments it, and returns the new value.
  2. It performs simple logging of the result.

The arithmetic handler is more complex:

  1. It accepts a set of actions ['plus', 'minus', 'times', 'divided-by'] and two numbers, and returns the result of the calculation.
  2. It uses an environment variable to control log level (such as DEBUG, INFO, WARNING, ERROR). It logs a few things at different levels, such as:
    • DEBUG: full event data
    • INFO: the result of the calculation
    • WARN~ING~: when a divide by zero error occurs
    • This will be the typical RUST_LOG variable.

The steps of the scenario are:

  1. Create an IAM role that:
    • Has an assume_role policy that grants 'lambda.amazonaws.com' the 'sts:AssumeRole' action.
    • Attaches the 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' managed role.
    • You must wait for ~10 seconds after the role is created before you can use it!
  2. Create a function (CreateFunction) for the increment handler by packaging it as a zip and either:
    • Adding it with CreateFunction Code.ZipFile.
    • --or--
    • Uploading it to S3 and adding it with CreateFunction Code.S3Bucket/S3Key.
    • Note: Zipping the file does not have to be done in code.
    • If you have a waiter, use it to wait until the function is active. Otherwise, call GetFunction until State is Active.
  3. Invoke the function with a number and print the result.
  4. Update the function (UpdateFunctionCode) to the arithmetic handler by packaging it as a zip and either:
    • Adding it with UpdateFunctionCode ZipFile
    • --or--
    • Uploading it to S3 and adding it with UpdateFunctionCode S3Bucket/S3Key.
  5. Call GetFunction until Configuration.LastUpdateStatus is 'Successful' (or 'Failed').
  6. Update the environment variable by calling UpdateFunctionConfiguration and pass it a log level, such as:
    • Environment={'Variables': {'RUST_LOG': 'TRACE'}}
  7. Invoke the function with an action from the list and a couple of values. Include LogType='Tail' to get logs in the result. Print the result of the calculation and the log.
  8. [Optional] Invoke the function to provoke a divide-by-zero error and show the log result.
  9. List all functions for the account, using pagination (ListFunctions).
  10. Delete the function (DeleteFunction).
  11. Delete the role.