Amazon S3 is AWS’s storage service. In an infrastructure-as-code workflow, it appears as a bucket resource: describe the bucket in Rust input, then let generated code create it through AWS rather than configuring it manually. section 9.1 section 9.5
A practical implementation separates parsing from cloud operations. The entry point parses the infrastructure input, checks whether any resources exist, starts asynchronous work only when needed, and coordinates bucket creation alongside optional Lambda creation. section 9.5
A bucket-creation path has these responsibilities:
- Keep S3-specific creation code in an s3 module, separate from input parsing and Lambda handling. section 9.5
- Initialize an AWS S3 SDK client asynchronously before issuing bucket operations. section 9.5
- Create the declared bucket and, when configured, continue by linking its event to a Lambda. section 9.5
- Translate AWS SDK failures into useful compile-time errors while leaving parsing errors as syn errors. section 9.5
Coordinate a bucket with other resources
An async create_infra routine constructs S3 and Lambda clients, creates a Lambda when present, then creates the bucket. If the bucket declares an event, the resulting Lambda output is used to connect the two resources. This ordering makes the dependency explicit while keeping each service behind its own client. 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 listing shows the orchestration point where an S3 client creates a bucket after optional Lambda creation, with later event linkage guarded by bucket configuration. section 9.5
