liveBook · Manning

AWS Lambda

As explained in Write Powerful Rust Macros

AWS Lambda is a serverless computing platform that runs code in response to events without requiring users to manage servers.

AWS Lambda is used here as a compute resource in an infrastructure-as-code workflow. A Lambda function runs supplied code without the caller managing its infrastructure; configuration includes memory and a maximum execution time, and billing is tied to the time the code requires. section 9.1

A Rust infrastructure DSL parses requested resources, then uses the AWS SDK for Rust to create Lambda functions alongside Amazon S3 buckets. Resource creation is asynchronous: clients are initialized, the requested Lambda is created, and an optional bucket event can link the bucket to that Lambda. section 9.5

A practical Lambda provisioning flow separates responsibilities:

  • Parse the DSL input into a resource request. section 9.5
  • Keep Lambda creation in a dedicated Lambda module and translate AWS SDK failures into useful compiler errors. section 9.5
  • Create the Lambda before connecting an event-producing S3 bucket when the bucket declares an event. section 9.5
  • Plan for retries and state or existence checks in production, because recently created resources can appear unavailable and repeat creation fails when names already exist. section 9.5
Listing 9.17 async create_infra function coordinating the infrastructure setup
async fn create_infra(iac_input: IacInput) -> Result<(), IacError> {
    let s3_client = S3Client::new() #1
        .await;                  
    let lambda_client = LambdaClient::new()
        .await;                  
    let mut output = None;

    if let Some(lambda) = &iac_input.lambda {
        eprintln!("creating lambda...");
        output = Some(lambda_client.create_lambda(lambda).await?); #2
    }                            

    if let Some(bucket) = &iac_input.bucket {
        eprintln!("creating bucket...");
        s3_client.create_bucket(bucket).await?;      #3

        if bucket.has_event {
            eprintln!("linking bucket and lambda by an event...");
            let lambda_arn_output = output
                .expect("when we have an event, we should have a lambda");
            let lambda = iac_input.lambda
                .expect("when we have an event, we should have a lambda");

            let lambda_arn = lambda_arn_output.function_arn()
                .expect("creating a lambda should return its ARN");
            lambda_client.add_bucket_permission(
                &lambda, &bucket.name
            ).await?;                       #4
            s3_client.link_bucket_with_lambda(
                bucket, lambda_arn
            ).await?;                      
        }
    }
    Ok(())
}

The coordinator illustrates the async sequence: construct S3 and Lambda clients, create a requested Lambda, create a requested bucket, then use the Lambda result when wiring a bucket event. section 9.5

Avoiding accidental provisioning during IDE macro expansion

A procedural macro can have side effects: IDE macro expansion may invoke infrastructure creation and claim a resource name. Guard creation by detecting IDE completion, so parsing and expansion can proceed without creating Lambda or S3 resources. section 9.5

How Write Powerful Rust Macros covers it

How deep it goes
Works through asynchronous SDK-based creation and S3 event linkage, while deferring production retries and persistent state.
Examples
Demonstrates an async create_infra coordinator and an IDE-completion guard that prevents accidental resource creation.
What you can do after
Build it from scratch by provisioning Lambda and S3 resources through an asynchronous Rust procedural macro.
How it connects
Connects Lambda with infrastructure as code, AWS SDK for Rust, Amazon S3, procedural macros, Tokio, and compiler errors.

Also explained in

Compare how 2 books explain AWS Lambda →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.