Skip to content

Commit

Permalink
Fix correct error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
EricGhildyal committed Nov 21, 2024
1 parent 07ca37c commit af20baa
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/adapters/ingresses/apig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::utils::load_default_aws_config;

use super::Ingress;
use async_trait::async_trait;
use miette::miette;
use miette::{IntoDiagnostic, Result};
use tokio::{fs::File, io::AsyncReadExt};

Expand Down Expand Up @@ -31,30 +32,39 @@ impl AwsApiGateway {
let config = load_default_aws_config().await;
let apig_client = GatewayClient::new(config);
let lambda_client = LambdaClient::new(config);

Ok(Self {
lambda_artifact: artifact,
apig_client,
lambda_client,
})
}

pub async fn upload_lambda(&self, lambda_name: &str) -> Result<String, aws_sdk_lambda::Error> {
pub async fn upload_lambda(&self, lambda_name: &str) -> Result<String> {
// Parse the bytes into the format AWS wants
let code = Blob::from(self.lambda_artifact.clone());

// Turn it into an uploadable zip file
let function_code = FunctionCode::builder().zip_file(code).build();
let zip_file = function_code
.zip_file()
.ok_or(miette!("Couldn't zip lambda code"))?;

// Upload it to Lambda
let res = self
.lambda_client
.update_function_code()
.function_name(lambda_name)
.zip_file(function_code.zip_file().unwrap().clone())
.zip_file(zip_file.clone())
.send()
.await?;
.await
.into_diagnostic()?;

Ok(res.version().unwrap().to_string())
let version = res
.version()
.ok_or(miette!("Couldn't get version of deployed lambda"))?;

Ok(version.to_string())
}

pub async fn create_apig_deployment(
Expand All @@ -64,7 +74,7 @@ impl AwsApiGateway {
lambda_name: &str,
lambda_version: &str,
traffic_percentage: f64,
) -> Result<(), aws_sdk_apigateway::Error> {
) -> Result<()> {
// Update the APIG with the new lambda version
self.apig_client
.put_integration()
Expand All @@ -74,7 +84,8 @@ impl AwsApiGateway {
lambda_name, lambda_version
))
.send()
.await?;
.await
.into_diagnostic()?;

// Create a deployment with canary settings to deploy our new lambda
self.apig_client
Expand All @@ -88,7 +99,8 @@ impl AwsApiGateway {
.build(),
)
.send()
.await?;
.await
.into_diagnostic()?;

Ok(())
}
Expand All @@ -107,13 +119,12 @@ async fn read_file(artifact_path: PathBuf) -> Result<Vec<u8>> {
impl Ingress for AwsApiGateway {
async fn deploy(&mut self) -> Result<()> {
// First, we need to deploy the new version of the lambda
let lambda_version = self.upload_lambda("releases").await.unwrap();
let lambda_version = self.upload_lambda("releases").await?;

// Next, we need to create a new deployment, pointing at our
// new lambda version with canary settings
self.create_apig_deployment("Releases", "prod", "releases", &lambda_version, 10.0)
.await
.unwrap();
self.create_apig_deployment("Releases", "prod", "releases", &lambda_version, 0.0)
.await?;

Ok(())
}
Expand Down

0 comments on commit af20baa

Please sign in to comment.