Skip to content
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.

Commit

Permalink
Rewrite heartbeat Lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
mlafeldt committed Aug 9, 2021
1 parent 7681a51 commit 7d0aba9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ edition = "2018"
name = "echo"
path = "echo/lambda.rs"

[[bin]]
name = "heartbeat"
path = "heartbeat/lambda.rs"

[dependencies]
hyper = "0.14"
lambda_runtime = "0.4"
log = "0.4"
serde_json = "1.0"
Expand Down
30 changes: 30 additions & 0 deletions heartbeat/lambda.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use hyper::{Body, Client, Request, Uri};
use lambda_runtime::{handler_fn, Context, Error};
use serde_json::Value;
use std::env;

#[tokio::main]
async fn main() -> Result<(), Error> {
simple_logger::init_with_level(log::Level::Trace)?;
lambda_runtime::run(handler_fn(handler)).await?;
Ok(())
}

async fn handler(_: Value, _: Context) -> Result<(), Error> {
let endpoint: Uri = env::var("HEARTBEAT_ENDPOINT")
.expect("heartbeat endpoint must be set")
.parse()
.unwrap();

let req = Request::get(endpoint)
.header("User-Agent", "dilbert-feed-rust")
.body(Body::empty())
.unwrap();

let resp = Client::new().request(req).await?;
if !resp.status().is_success() {
return Err(format!("HTTP error: {}", resp.status()).into());
};

Ok(())
}
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export class DilbertFeedStack extends cdk.Stack {
const heartbeatEndpoint = ssm.StringParameter.valueForStringParameter(this, `/${id}/heartbeat-endpoint`)
const heartbeat = new lambda.Function(this, 'HeartbeatFunc', {
functionName: `${id}-heartbeat`,
code: lambda.Code.fromAsset('heartbeat'),
handler: 'lambda.handler',
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromAsset('bin/heartbeat'),
handler: 'bootstrap',
runtime: lambda.Runtime.PROVIDED,
memorySize: 128,
timeout: cdk.Duration.seconds(10),
logRetention: logs.RetentionDays.ONE_MONTH,
Expand Down

0 comments on commit 7d0aba9

Please sign in to comment.