Skip to content

Instantly share code, notes, and snippets.

@philschmid
Created March 11, 2022 09:25
Show Gist options
  • Select an option

  • Save philschmid/0f236adee2427fdbd7c44dda6e060dbf to your computer and use it in GitHub Desktop.

Select an option

Save philschmid/0f236adee2427fdbd7c44dda6e060dbf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from aws_cdk import core as cdk
import os
from pathlib import Path
import shutil
import subprocess
from aws_cdk import (
aws_iam as iam,
aws_sagemaker as sagemaker,
aws_lambda as lambda_,
aws_apigateway as _apigw,
)
from aws_cdk.aws_logs import RetentionDays
# Environment
# CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION are set based on the
# AWS profile specified using the --profile option.
my_environment = cdk.Environment(account=os.environ["CDK_DEFAULT_ACCOUNT"], region=os.environ["CDK_DEFAULT_REGION"])
class RustLambdaStack(cdk.Stack):
def __init__(self, scope: cdk.Construct, construct_id: str, target_architecture="arm", **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
##############################
# Lambda Function #
##############################
architecture = lambda_.Architecture.ARM_64
lambda_handler_path = Path("build")
# create function
lambda_fn = lambda_.Function(
self,
"rust-lambda",
description="Rust lambda",
code=lambda_.Code.from_asset(str(lambda_handler_path.absolute())),
timeout=cdk.Duration.seconds(60),
runtime=lambda_.Runtime.PROVIDED_AL2,
handler="not.required",
log_retention=RetentionDays.ONE_WEEK,
architectures=[architecture],
# environment={"RUST_BACKTRACE": "1"},
)
##############################
# API Gateway #
##############################
api = _apigw.LambdaRestApi(
self,
"api-gateway",
proxy=True,
handler=lambda_fn,
default_cors_preflight_options=_apigw.CorsOptions(
allow_origins=_apigw.Cors.ALL_ORIGINS, allow_methods=_apigw.Cors.ALL_METHODS
),
)
route = api.root.add_resource("add")
route.add_method("POST") # POST /add
app = cdk.App()
rust_lambda = RustLambdaStack(app, "RustLambdaStack", env=my_environment)
app.synth()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment