From 07df6bb6a81d4c17860e3cf466e2e16e882df460 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 11:32:54 +0200 Subject: [PATCH 01/21] apigw-lambda-rust --- apigw-lambda-rust/Makefile | 29 ----------------------------- apigw-lambda-rust/README.md | 10 +++++----- apigw-lambda-rust/template.yml | 8 +++++--- 3 files changed, 10 insertions(+), 37 deletions(-) delete mode 100644 apigw-lambda-rust/Makefile diff --git a/apigw-lambda-rust/Makefile b/apigw-lambda-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/apigw-lambda-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/apigw-lambda-rust/README.md b/apigw-lambda-rust/README.md index d0d7b6c27..b428883b6 100644 --- a/apigw-lambda-rust/README.md +++ b/apigw-lambda-rust/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -27,11 +27,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name @@ -70,7 +70,7 @@ Response: 1. Delete the stack ```bash - make delete + sam delete ``` 2. Confirm the stack has been deleted ```bash @@ -80,4 +80,4 @@ Response: ---- Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. -SPDX-License-Identifier: MIT-0 \ No newline at end of file +SPDX-License-Identifier: MIT-0 diff --git a/apigw-lambda-rust/template.yml b/apigw-lambda-rust/template.yml index 264278fd8..d8c02aeea 100644 --- a/apigw-lambda-rust/template.yml +++ b/apigw-lambda-rust/template.yml @@ -8,7 +8,7 @@ Globals: MemorySize: 128 Architectures: ["arm64"] Handler: bootstrap - Runtime: provided.al2 + Runtime: provided.al2023 Timeout: 29 Environment: Variables: @@ -46,7 +46,7 @@ Resources: LambdaFunction: Type: 'AWS::Serverless::Function' Properties: - CodeUri: ./build/handler + CodeUri: . Policies: - AWSLambdaBasicExecutionRole Events: @@ -56,8 +56,10 @@ Resources: Path: /do Method: POST RestApiId: !Ref MyApi + Metadata: + BuildMethod: rust-cargolambda Outputs: HttpApiEndpoint: Description: The default endpoint for the REST API. - Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/${StageName}/" \ No newline at end of file + Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/${StageName}/" From 9c9af5e82606f5aedff7fc97246c111505f95e7f Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 11:37:09 +0200 Subject: [PATCH 02/21] alb-lambda-rust --- alb-lambda-rust/Cargo.toml | 4 ++-- alb-lambda-rust/Makefile | 29 ----------------------------- alb-lambda-rust/README.md | 10 +++++----- alb-lambda-rust/template.yml | 6 ++++-- 4 files changed, 11 insertions(+), 38 deletions(-) delete mode 100644 alb-lambda-rust/Makefile diff --git a/alb-lambda-rust/Cargo.toml b/alb-lambda-rust/Cargo.toml index cb2ba174b..aace5d546 100644 --- a/alb-lambda-rust/Cargo.toml +++ b/alb-lambda-rust/Cargo.toml @@ -9,8 +9,8 @@ name = "handler" path = "src/bin/handler.rs" [dependencies] -lambda_http = "0.11" -lambda_runtime = "0.11" +lambda_http = "0.13" +lambda_runtime = "0.13" serde = {version = "1.0", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["macros"] } diff --git a/alb-lambda-rust/Makefile b/alb-lambda-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/alb-lambda-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/alb-lambda-rust/README.md b/alb-lambda-rust/README.md index ff7e7e7ce..21382f04e 100644 --- a/alb-lambda-rust/README.md +++ b/alb-lambda-rust/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -27,11 +27,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name @@ -70,7 +70,7 @@ Response: 1. Delete the stack ```bash - make delete + sam delete ``` 2. Confirm the stack has been deleted ```bash @@ -80,4 +80,4 @@ Response: ---- Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. -SPDX-License-Identifier: MIT-0 \ No newline at end of file +SPDX-License-Identifier: MIT-0 diff --git a/alb-lambda-rust/template.yml b/alb-lambda-rust/template.yml index 014d669d0..03b484188 100644 --- a/alb-lambda-rust/template.yml +++ b/alb-lambda-rust/template.yml @@ -28,11 +28,13 @@ Resources: MyLambdaFunction: Type: 'AWS::Serverless::Function' Properties: - CodeUri: ./build/handler + CodeUri: . Policies: - AWSLambdaBasicExecutionRole Tags: name: !Ref AWS::StackName + Metadata: + BuildMethod: rust-cargolambda MyLambdaFunctionPermission: Type: AWS::Lambda::Permission @@ -110,4 +112,4 @@ Resources: Outputs: DNSName: - Value: !GetAtt MyLoadBalancer.DNSName \ No newline at end of file + Value: !GetAtt MyLoadBalancer.DNSName From c5676d2db5db976d01976b562910923eda035212 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 11:39:27 +0200 Subject: [PATCH 03/21] apigw-http-api-lambda-rust --- apigw-http-api-lambda-rust/Cargo.toml | 4 ++-- apigw-http-api-lambda-rust/Makefile | 29 ------------------------- apigw-http-api-lambda-rust/README.md | 8 +++---- apigw-http-api-lambda-rust/template.yml | 7 ++++-- 4 files changed, 11 insertions(+), 37 deletions(-) delete mode 100644 apigw-http-api-lambda-rust/Makefile diff --git a/apigw-http-api-lambda-rust/Cargo.toml b/apigw-http-api-lambda-rust/Cargo.toml index 48a1ddc4e..db70b0c38 100644 --- a/apigw-http-api-lambda-rust/Cargo.toml +++ b/apigw-http-api-lambda-rust/Cargo.toml @@ -9,8 +9,8 @@ name = "handler" path = "src/bin/handler.rs" [dependencies] -lambda_http = "0.11" -lambda_runtime = "0.11" +lambda_http = "0.13" +lambda_runtime = "0.13" serde_json = "1" tokio = { version = "1", features = ["macros"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } diff --git a/apigw-http-api-lambda-rust/Makefile b/apigw-http-api-lambda-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/apigw-http-api-lambda-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/apigw-http-api-lambda-rust/README.md b/apigw-http-api-lambda-rust/README.md index 0fbf6ba99..d2b400e61 100644 --- a/apigw-http-api-lambda-rust/README.md +++ b/apigw-http-api-lambda-rust/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -27,11 +27,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name @@ -98,4 +98,4 @@ This pattern was contributed by Greg Davis. ---- Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. -SPDX-License-Identifier: MIT-0 \ No newline at end of file +SPDX-License-Identifier: MIT-0 diff --git a/apigw-http-api-lambda-rust/template.yml b/apigw-http-api-lambda-rust/template.yml index cc126603c..4c25bafbd 100644 --- a/apigw-http-api-lambda-rust/template.yml +++ b/apigw-http-api-lambda-rust/template.yml @@ -46,7 +46,10 @@ Resources: LambdaFunction: Type: 'AWS::Serverless::Function' Properties: - CodeUri: ./build/handler + CodeUri: . + Metadata: + BuildMethod: rust-cargolambda + # Function permissions grant an AWS service or another account permission to use a function FunctionResourcePermission: Type: 'AWS::Lambda::Permission' @@ -59,4 +62,4 @@ Resources: Outputs: HttpApiEndpoint: Description: The default endpoint for the HTTP API. - Value: !GetAtt HttpApi.ApiEndpoint \ No newline at end of file + Value: !GetAtt HttpApi.ApiEndpoint From 5e5bc5ace8942b1118f57cfffcacbb815cf5b0b0 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 11:39:57 +0200 Subject: [PATCH 04/21] apigw-lambda-rust update cargo --- apigw-lambda-rust/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apigw-lambda-rust/Cargo.toml b/apigw-lambda-rust/Cargo.toml index 4c121faf3..e77348427 100644 --- a/apigw-lambda-rust/Cargo.toml +++ b/apigw-lambda-rust/Cargo.toml @@ -9,8 +9,8 @@ name = "handler" path = "src/bin/handler.rs" [dependencies] -lambda_http = "0.11" -lambda_runtime = "0.11" +lambda_http = "0.13.0" +lambda_runtime = "0.13.0" serde = {version = "1.0", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["macros"] } From a62716683a7f9a639ceba8dbc2b17ef73c1769dc Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 11:45:30 +0200 Subject: [PATCH 05/21] appconfig-lambda-rust --- appconfig-lambda-rust/Cargo.toml | 4 ++-- appconfig-lambda-rust/Makefile | 29 ----------------------------- appconfig-lambda-rust/README.md | 6 +++--- appconfig-lambda-rust/template.yml | 6 ++++-- 4 files changed, 9 insertions(+), 36 deletions(-) delete mode 100644 appconfig-lambda-rust/Makefile diff --git a/appconfig-lambda-rust/Cargo.toml b/appconfig-lambda-rust/Cargo.toml index 3511b143e..5944f4709 100644 --- a/appconfig-lambda-rust/Cargo.toml +++ b/appconfig-lambda-rust/Cargo.toml @@ -10,8 +10,8 @@ path = "src/bin/handler.rs" [dependencies] aws-config = {version = "1", features = ["behavior-version-latest"] } -lambda_runtime = "0.11" -lazy_static = "1.4" +lambda_runtime = "0.13" +lazy_static = "1" reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "json"] } serde = {version = "1.0", features = ["derive"] } serde_json = "1" diff --git a/appconfig-lambda-rust/Makefile b/appconfig-lambda-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/appconfig-lambda-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/appconfig-lambda-rust/README.md b/appconfig-lambda-rust/README.md index 5a53d0437..6a20f1610 100644 --- a/appconfig-lambda-rust/README.md +++ b/appconfig-lambda-rust/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) * [AWS AppConfig integration with Lambda extensions](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-integration-lambda-extensions.html) ## Deployment Instructions @@ -28,11 +28,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name diff --git a/appconfig-lambda-rust/template.yml b/appconfig-lambda-rust/template.yml index f5fc542c7..0a77d3b08 100644 --- a/appconfig-lambda-rust/template.yml +++ b/appconfig-lambda-rust/template.yml @@ -76,7 +76,7 @@ Resources: PutObjectFunction: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Policies: - AWSLambdaBasicExecutionRole - Version: '2012-10-17' @@ -94,4 +94,6 @@ Resources: AWS_APPCONFIG_PROFILE: MyTestProfile AWS_APPCONFIG_EXTENSION_HTTP_PORT: 2772 Layers: - - arn:aws:lambda:eu-central-1:066940009817:layer:AWS-AppConfig-Extension-Arm64:2 \ No newline at end of file + - arn:aws:lambda:eu-central-1:066940009817:layer:AWS-AppConfig-Extension-Arm64:101 + Metadata: + BuildMethod: rust-cargolambda From c3f16bb8db888da045dc102dd6fe543806bdbe26 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 11:46:08 +0200 Subject: [PATCH 06/21] cloudfront-apigw-http-api-lambda-rust --- .../Cargo.toml | 2 +- .../Makefile | 29 ------------------- .../README.md | 8 ++--- .../template.yml | 7 +++-- 4 files changed, 10 insertions(+), 36 deletions(-) delete mode 100644 cloudfront-apigw-http-api-lambda-rust/Makefile diff --git a/cloudfront-apigw-http-api-lambda-rust/Cargo.toml b/cloudfront-apigw-http-api-lambda-rust/Cargo.toml index 222c8e187..a7d9e79b6 100644 --- a/cloudfront-apigw-http-api-lambda-rust/Cargo.toml +++ b/cloudfront-apigw-http-api-lambda-rust/Cargo.toml @@ -9,7 +9,7 @@ name = "handler" path = "src/bin/handler.rs" [dependencies] -lambda_http = "0.11" +lambda_http = "0.13" serde_json = "1" tokio = { version = "1", features = ["macros"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } diff --git a/cloudfront-apigw-http-api-lambda-rust/Makefile b/cloudfront-apigw-http-api-lambda-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/cloudfront-apigw-http-api-lambda-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/cloudfront-apigw-http-api-lambda-rust/README.md b/cloudfront-apigw-http-api-lambda-rust/README.md index eb2e70299..28a0f98c8 100644 --- a/cloudfront-apigw-http-api-lambda-rust/README.md +++ b/cloudfront-apigw-http-api-lambda-rust/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -27,11 +27,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name @@ -104,4 +104,4 @@ X-Cache: Hit from cloudfront ---- Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. -SPDX-License-Identifier: MIT-0 \ No newline at end of file +SPDX-License-Identifier: MIT-0 diff --git a/cloudfront-apigw-http-api-lambda-rust/template.yml b/cloudfront-apigw-http-api-lambda-rust/template.yml index 5448a7047..d909ed19b 100644 --- a/cloudfront-apigw-http-api-lambda-rust/template.yml +++ b/cloudfront-apigw-http-api-lambda-rust/template.yml @@ -46,7 +46,10 @@ Resources: LambdaFunction: Type: 'AWS::Serverless::Function' Properties: - CodeUri: ./build/handler + CodeUri: . + Metadata: + BuildMethod: rust-cargolambda + # Function permissions grant an AWS service or another account permission to use a function FunctionResourcePermission: Type: 'AWS::Lambda::Permission' @@ -123,4 +126,4 @@ Outputs: Description: "Distribution domain name" Value: !GetAtt CloudfrontDistribution.DomainName Export: - Name: DistributionDomainName \ No newline at end of file + Name: DistributionDomainName From 74647e60c3ace253fcdf06d64052ff0ee9428a45 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 11:46:42 +0200 Subject: [PATCH 07/21] cloudfront-failover-apigw-http-api-lambda-rust --- .../Cargo.toml | 2 +- .../Makefile | 20 ------------------- .../README.md | 5 ++--- .../api.yml | 7 +++++-- 4 files changed, 8 insertions(+), 26 deletions(-) delete mode 100644 cloudfront-failover-apigw-http-api-lambda-rust/Makefile diff --git a/cloudfront-failover-apigw-http-api-lambda-rust/Cargo.toml b/cloudfront-failover-apigw-http-api-lambda-rust/Cargo.toml index a9b3ba84c..d59674a98 100644 --- a/cloudfront-failover-apigw-http-api-lambda-rust/Cargo.toml +++ b/cloudfront-failover-apigw-http-api-lambda-rust/Cargo.toml @@ -9,7 +9,7 @@ name = "handler" path = "src/bin/handler.rs" [dependencies] -lambda_http = "0.11" +lambda_http = "0.13" serde_json = "1" tokio = { version = "1", features = ["macros"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } diff --git a/cloudfront-failover-apigw-http-api-lambda-rust/Makefile b/cloudfront-failover-apigw-http-api-lambda-rust/Makefile deleted file mode 100644 index 7d9665e40..000000000 --- a/cloudfront-failover-apigw-http-api-lambda-rust/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap diff --git a/cloudfront-failover-apigw-http-api-lambda-rust/README.md b/cloudfront-failover-apigw-http-api-lambda-rust/README.md index 07cfcf1eb..48a1e9f1a 100644 --- a/cloudfront-failover-apigw-http-api-lambda-rust/README.md +++ b/cloudfront-failover-apigw-http-api-lambda-rust/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -27,7 +27,7 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. Deploy the api on the PrimaryRegion region. ``` @@ -131,4 +131,3 @@ X-Cache: Hit from cloudfront Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: MIT-0 - diff --git a/cloudfront-failover-apigw-http-api-lambda-rust/api.yml b/cloudfront-failover-apigw-http-api-lambda-rust/api.yml index 5ed50b96d..4ed2d8bfc 100644 --- a/cloudfront-failover-apigw-http-api-lambda-rust/api.yml +++ b/cloudfront-failover-apigw-http-api-lambda-rust/api.yml @@ -60,7 +60,7 @@ Resources: LambdaFunction: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Policies: - AWSLambdaBasicExecutionRole Events: @@ -79,8 +79,11 @@ Resources: Tags: name: !Ref AWS::StackName env: !Ref StageName + Metadata: + BuildMethod: rust-cargolambda + Outputs: HttpApiEndpoint: Description: The api endpoint. - Value: !GetAtt HttpApi.ApiEndpoint \ No newline at end of file + Value: !GetAtt HttpApi.ApiEndpoint From 2356e9ba8be011894f16785f36662dc1db864bc4 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 14:35:36 +0200 Subject: [PATCH 08/21] cloudfront-lambda-url-rust --- cloudfront-lambda-url-rust/Cargo.toml | 2 +- cloudfront-lambda-url-rust/Makefile | 29 ------------------------- cloudfront-lambda-url-rust/README.md | 12 +++++----- cloudfront-lambda-url-rust/template.yml | 7 ++++-- 4 files changed, 12 insertions(+), 38 deletions(-) delete mode 100644 cloudfront-lambda-url-rust/Makefile diff --git a/cloudfront-lambda-url-rust/Cargo.toml b/cloudfront-lambda-url-rust/Cargo.toml index d29aa3b23..267288e5b 100644 --- a/cloudfront-lambda-url-rust/Cargo.toml +++ b/cloudfront-lambda-url-rust/Cargo.toml @@ -9,7 +9,7 @@ name = "handler" path = "src/bin/handler.rs" [dependencies] -lambda_http = "0.11" +lambda_http = "0.13" serde_json = "1" tokio = { version = "1", features = ["macros"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } diff --git a/cloudfront-lambda-url-rust/Makefile b/cloudfront-lambda-url-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/cloudfront-lambda-url-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/cloudfront-lambda-url-rust/README.md b/cloudfront-lambda-url-rust/README.md index 7694bc084..e97f20c2f 100644 --- a/cloudfront-lambda-url-rust/README.md +++ b/cloudfront-lambda-url-rust/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) * Make sure to run "rustup target add aarch64-unknown-linux-gnu;" ## Deployment Instructions @@ -28,11 +28,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name @@ -52,7 +52,7 @@ This pattern deploys an Amazon Cloudfront distribution, an AWS Lambda function U Once the application is deployed, retrieve the CloudFront value from CloudFormation Outputs. Either browse to the endpoint in a web browser or call the endpoint from Postman. -Example GET Request: https://{DistributionDomainName} +Example GET Request: curl --location 'https://{DistributionDomainName}t?allowed_query_string_param=Daniele' Response: ``` @@ -65,7 +65,7 @@ X-Cache: Miss from cloudfront If you try again: X-Cache: Hit from cloudfront -Example GET Request: https://{DistributionDomainName}?allowed_query_string_param=Daniele +Example GET Request: curl --location 'https://{DistributionDomainName}t?allowed_query_string_param=Daniele' Response: ``` @@ -92,7 +92,7 @@ X-Cache: Hit from cloudfront 1. Delete the stack ```bash - make delete + sam delete ``` 2. Confirm the stack has been deleted ```bash diff --git a/cloudfront-lambda-url-rust/template.yml b/cloudfront-lambda-url-rust/template.yml index f31f728b9..bc601bf56 100644 --- a/cloudfront-lambda-url-rust/template.yml +++ b/cloudfront-lambda-url-rust/template.yml @@ -23,9 +23,12 @@ Resources: LambdaFunction: Type: 'AWS::Serverless::Function' Properties: - CodeUri: ./build/handler + CodeUri: . FunctionUrlConfig: AuthType: NONE + Metadata: + BuildMethod: rust-cargolambda + # ########################################################################## # # CloudFront::CachePolicy # @@ -96,4 +99,4 @@ Outputs: Description: "Distribution domain name" Value: !GetAtt CloudfrontDistribution.DomainName Export: - Name: DistributionDomainName \ No newline at end of file + Name: DistributionDomainName From 23239053200476577aaa479886178f89d00bc113 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 14:36:08 +0200 Subject: [PATCH 09/21] dynamodb-streams-lambda-eventbridge-sam-rust --- .../Cargo.toml | 6 ++-- .../Makefile | 33 ------------------- .../README.md | 6 ++-- .../template.yml | 7 ++-- 4 files changed, 11 insertions(+), 41 deletions(-) delete mode 100644 dynamodb-streams-lambda-eventbridge-sam-rust/Makefile diff --git a/dynamodb-streams-lambda-eventbridge-sam-rust/Cargo.toml b/dynamodb-streams-lambda-eventbridge-sam-rust/Cargo.toml index 785fb804e..9460ca5b3 100644 --- a/dynamodb-streams-lambda-eventbridge-sam-rust/Cargo.toml +++ b/dynamodb-streams-lambda-eventbridge-sam-rust/Cargo.toml @@ -13,9 +13,9 @@ aws_lambda_events = { version = "0.15", default-features = false, features = ["d aws-types = "1" aws-config = {version = "1", features = ["behavior-version-latest"] } aws-sdk-eventbridge = "1" -lambda_runtime = "0.11" +lambda_runtime = "0.13" serde_json = "1" -serde = {version = "1.0", features = ["derive"] } +serde = {version = "1", features = ["derive"] } tokio = { version = "1", features = ["macros"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } -typed-builder = "0.18" +typed-builder = "0.20.0" diff --git a/dynamodb-streams-lambda-eventbridge-sam-rust/Makefile b/dynamodb-streams-lambda-eventbridge-sam-rust/Makefile deleted file mode 100644 index 93c3547f0..000000000 --- a/dynamodb-streams-lambda-eventbridge-sam-rust/Makefile +++ /dev/null @@ -1,33 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build-MainFunction: - cp -v "./build/handler/"* "$(ARTIFACTS_DIR)" - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - - -delete: - sam delete diff --git a/dynamodb-streams-lambda-eventbridge-sam-rust/README.md b/dynamodb-streams-lambda-eventbridge-sam-rust/README.md index ede2e41b8..baa60c1ef 100644 --- a/dynamodb-streams-lambda-eventbridge-sam-rust/README.md +++ b/dynamodb-streams-lambda-eventbridge-sam-rust/README.md @@ -15,7 +15,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.67.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) * [AWS AppConfig integration with Lambda extensions](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-integration-lambda-extensions. ## Deployment Instructions @@ -30,11 +30,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. From the command line, initialize terraform to to downloads and installs the providers defined in the configuration: ``` - make build + sam build ``` 4. From the command line, apply the configuration in the main.tf file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name diff --git a/dynamodb-streams-lambda-eventbridge-sam-rust/template.yml b/dynamodb-streams-lambda-eventbridge-sam-rust/template.yml index 624e7657f..059430237 100644 --- a/dynamodb-streams-lambda-eventbridge-sam-rust/template.yml +++ b/dynamodb-streams-lambda-eventbridge-sam-rust/template.yml @@ -40,7 +40,7 @@ Resources: Handler: bootstrap Runtime: provided.al2 Timeout: 6 - CodeUri: ./build/handler + CodeUri: . Policies: - EventBridgePutEventsPolicy: EventBusName: !Ref MyBus @@ -63,6 +63,9 @@ Resources: BUS_NAME: !Ref MyBus RUST_BACKTRACE: 1 RUST_LOG: info + Metadata: + BuildMethod: rust-cargolambda + MyFunctionLogGroup: Type: AWS::Logs::LogGroup @@ -82,4 +85,4 @@ Outputs: MyFunctionArn: Value: !GetAtt MyFunction.Arn Export: - Name: !Sub ${AWS::StackName}-MyFunctionArn \ No newline at end of file + Name: !Sub ${AWS::StackName}-MyFunctionArn From 8cd4fbd5463c2ba5317291b518fe0ceacaa0a343 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 14:36:26 +0200 Subject: [PATCH 10/21] eventbridge-lambda-rust --- eventbridge-lambda-rust/Cargo.toml | 4 ++-- eventbridge-lambda-rust/Makefile | 29 ---------------------------- eventbridge-lambda-rust/README.md | 6 +++--- eventbridge-lambda-rust/template.yml | 7 +++++-- 4 files changed, 10 insertions(+), 36 deletions(-) delete mode 100644 eventbridge-lambda-rust/Makefile diff --git a/eventbridge-lambda-rust/Cargo.toml b/eventbridge-lambda-rust/Cargo.toml index 92fcbaeab..8b590683b 100644 --- a/eventbridge-lambda-rust/Cargo.toml +++ b/eventbridge-lambda-rust/Cargo.toml @@ -9,7 +9,7 @@ name = "handler" path = "src/bin/handler.rs" [dependencies] -aws_lambda_events = { version = "0.7", default-features = false, features = ["cloudwatch_events"] } -lambda_runtime = "0.11" +aws_lambda_events = { version = "0.15", default-features = false, features = ["cloudwatch_events"] } +lambda_runtime = "0.13" tokio = { version = "1", features = ["macros"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } diff --git a/eventbridge-lambda-rust/Makefile b/eventbridge-lambda-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/eventbridge-lambda-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/eventbridge-lambda-rust/README.md b/eventbridge-lambda-rust/README.md index 9d95ab8ea..9be25bf8a 100644 --- a/eventbridge-lambda-rust/README.md +++ b/eventbridge-lambda-rust/README.md @@ -15,7 +15,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -29,11 +29,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name diff --git a/eventbridge-lambda-rust/template.yml b/eventbridge-lambda-rust/template.yml index c6879bf8b..f2dc07e7d 100644 --- a/eventbridge-lambda-rust/template.yml +++ b/eventbridge-lambda-rust/template.yml @@ -22,7 +22,7 @@ Resources: Type: AWS::Serverless::Function Description: Invoked by EventBridge rule Properties: - CodeUri: ./build/handler + CodeUri: . Events: Trigger: Type: EventBridgeRule @@ -31,10 +31,13 @@ Resources: source: - custom.myApp detail-type: - - transaction + - transaction detail: location: - "prefix": "EUR-" + Metadata: + BuildMethod: rust-cargolambda + Outputs: ConsumerFunction: From bc964a5ed8cf03acb3dc25f9bde9e4c3789a8693 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 14:36:50 +0200 Subject: [PATCH 11/21] lambda-dynamodb-rust --- lambda-dynamodb-rust/Cargo.toml | 4 ++-- lambda-dynamodb-rust/Makefile | 29 ----------------------------- lambda-dynamodb-rust/README.md | 6 +++--- lambda-dynamodb-rust/template.yml | 5 ++++- 4 files changed, 9 insertions(+), 35 deletions(-) delete mode 100644 lambda-dynamodb-rust/Makefile diff --git a/lambda-dynamodb-rust/Cargo.toml b/lambda-dynamodb-rust/Cargo.toml index 936126951..e7caef820 100644 --- a/lambda-dynamodb-rust/Cargo.toml +++ b/lambda-dynamodb-rust/Cargo.toml @@ -12,8 +12,8 @@ path = "src/bin/handler.rs" [dependencies] aws-config = {version = "1", features = ["behavior-version-latest"] } aws-sdk-dynamodb = "1" -lambda_runtime = "0.11" +lambda_runtime = "0.13" serde_json = "1" tokio = { version = "1", features = ["macros"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } -chrono = "0.4.19" +chrono = "0.4" diff --git a/lambda-dynamodb-rust/Makefile b/lambda-dynamodb-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/lambda-dynamodb-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/lambda-dynamodb-rust/README.md b/lambda-dynamodb-rust/README.md index b3eb63bfc..fea8a0da1 100644 --- a/lambda-dynamodb-rust/README.md +++ b/lambda-dynamodb-rust/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -27,11 +27,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name diff --git a/lambda-dynamodb-rust/template.yml b/lambda-dynamodb-rust/template.yml index ae548cf7f..d40bf15ac 100644 --- a/lambda-dynamodb-rust/template.yml +++ b/lambda-dynamodb-rust/template.yml @@ -23,7 +23,7 @@ Resources: LambdaPutDynamoDB: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Environment: Variables: TABLE_NAME: !Ref DynamoTable @@ -32,6 +32,9 @@ Resources: ## https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html - DynamoDBWritePolicy: TableName: !Ref DynamoTable + Metadata: + BuildMethod: rust-cargolambda + ########################################################################## # DynamoDB # From 8a59d9d3a0f1cdd9b8034ad506307e7820f292bc Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 14:37:11 +0200 Subject: [PATCH 12/21] lambda-eventbridge-rust --- lambda-eventbridge-rust/Cargo.toml | 2 +- lambda-eventbridge-rust/Makefile | 29 ---------------------------- lambda-eventbridge-rust/README.md | 8 ++++---- lambda-eventbridge-rust/template.yml | 4 +++- 4 files changed, 8 insertions(+), 35 deletions(-) delete mode 100644 lambda-eventbridge-rust/Makefile diff --git a/lambda-eventbridge-rust/Cargo.toml b/lambda-eventbridge-rust/Cargo.toml index f0c534d73..17a564ddc 100644 --- a/lambda-eventbridge-rust/Cargo.toml +++ b/lambda-eventbridge-rust/Cargo.toml @@ -12,7 +12,7 @@ path = "src/bin/handler.rs" aws-config = {version = "1", features = ["behavior-version-latest"] } aws-types = "1" aws-sdk-eventbridge = "1" -lambda_runtime = "0.11" +lambda_runtime = "0.13" serde = {version = "1.0", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["macros"] } diff --git a/lambda-eventbridge-rust/Makefile b/lambda-eventbridge-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/lambda-eventbridge-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/lambda-eventbridge-rust/README.md b/lambda-eventbridge-rust/README.md index 2439daa5d..39e3fd121 100644 --- a/lambda-eventbridge-rust/README.md +++ b/lambda-eventbridge-rust/README.md @@ -15,7 +15,7 @@ You are responsible for any AWS costs incurred. No warranty is implied in this e * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -29,11 +29,11 @@ You are responsible for any AWS costs incurred. No warranty is implied in this e ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name @@ -76,7 +76,7 @@ PutEventsOutput { failed_entry_count: 0, entries: Some([PutEventsResultEntry { e 1. Delete the stack ```bash - make delete + sam delete ``` 2. Confirm the stack has been deleted. ```bash diff --git a/lambda-eventbridge-rust/template.yml b/lambda-eventbridge-rust/template.yml index fde788df4..d472ac2cd 100644 --- a/lambda-eventbridge-rust/template.yml +++ b/lambda-eventbridge-rust/template.yml @@ -27,13 +27,15 @@ Resources: PublisherFunction: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Policies: - EventBridgePutEventsPolicy: EventBusName: !Ref EventBusName Environment: Variables: EVENT_BUS_NAME: !Ref EventBusName + Metadata: + BuildMethod: rust-cargolambda Outputs: PublisherFunction: From 34fdb2c5e340d4b6af37c9a1c58f6f07bc5f89ea Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 14:37:25 +0200 Subject: [PATCH 13/21] lambda-rekognition-rust --- lambda-rekognition-rust/Cargo.toml | 2 +- lambda-rekognition-rust/Makefile | 29 ---------------------------- lambda-rekognition-rust/README.md | 6 +++--- lambda-rekognition-rust/template.yml | 5 ++++- 4 files changed, 8 insertions(+), 34 deletions(-) delete mode 100644 lambda-rekognition-rust/Makefile diff --git a/lambda-rekognition-rust/Cargo.toml b/lambda-rekognition-rust/Cargo.toml index e0a3db1b0..7a9a4f16b 100644 --- a/lambda-rekognition-rust/Cargo.toml +++ b/lambda-rekognition-rust/Cargo.toml @@ -11,7 +11,7 @@ path = "src/bin/handler.rs" [dependencies] aws-config = {version = "1", features = ["behavior-version-latest"] } aws-sdk-rekognition = "1" -lambda_runtime = "0.11" +lambda_runtime = "0.13" serde = {version = "1.0", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["macros"] } diff --git a/lambda-rekognition-rust/Makefile b/lambda-rekognition-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/lambda-rekognition-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/lambda-rekognition-rust/README.md b/lambda-rekognition-rust/README.md index 46e097a67..e43d28552 100644 --- a/lambda-rekognition-rust/README.md +++ b/lambda-rekognition-rust/README.md @@ -15,7 +15,7 @@ You are responsible for any AWS costs incurred. No warranty is implied in this e * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -33,11 +33,11 @@ cd lambda-rekognition-rust 3. Install dependencies and build (docker and cross build are required): ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name diff --git a/lambda-rekognition-rust/template.yml b/lambda-rekognition-rust/template.yml index 4f0899d5f..d0a1ed87d 100644 --- a/lambda-rekognition-rust/template.yml +++ b/lambda-rekognition-rust/template.yml @@ -48,7 +48,7 @@ Resources: MyFunction: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Policies: - S3ReadPolicy: BucketName: !Ref SourceBucket @@ -73,6 +73,9 @@ Resources: reason: - PutObject - CompleteMultipartUpload + Metadata: + BuildMethod: rust-cargolambda + Outputs: SourceBucketName: From 7077126e206dd193f5c5cdeaaa8a044884abcd50 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 14:37:38 +0200 Subject: [PATCH 14/21] lambda-s3-rust --- lambda-s3-rust/Cargo.toml | 2 +- lambda-s3-rust/Makefile | 29 ----------------------------- lambda-s3-rust/README.md | 6 +++--- lambda-s3-rust/template.yml | 7 +++++-- 4 files changed, 9 insertions(+), 35 deletions(-) delete mode 100644 lambda-s3-rust/Makefile diff --git a/lambda-s3-rust/Cargo.toml b/lambda-s3-rust/Cargo.toml index 3c73d475f..666c8b2e8 100644 --- a/lambda-s3-rust/Cargo.toml +++ b/lambda-s3-rust/Cargo.toml @@ -11,7 +11,7 @@ path = "src/bin/handler.rs" [dependencies] aws-config = {version = "1", features = ["behavior-version-latest"] } aws-sdk-s3 = "1" -lambda_runtime = "0.11" +lambda_runtime = "0.13" serde_json = "1" tokio = { version = "1", features = ["macros"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } diff --git a/lambda-s3-rust/Makefile b/lambda-s3-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/lambda-s3-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/lambda-s3-rust/README.md b/lambda-s3-rust/README.md index cd21b5dfa..6868aa58c 100644 --- a/lambda-s3-rust/README.md +++ b/lambda-s3-rust/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -27,11 +27,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name diff --git a/lambda-s3-rust/template.yml b/lambda-s3-rust/template.yml index 5d6133561..39e2df091 100644 --- a/lambda-s3-rust/template.yml +++ b/lambda-s3-rust/template.yml @@ -28,10 +28,13 @@ Resources: PutObjectFunction: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Environment: Variables: BUCKET_NAME: !Ref DestinationBucket Policies: - S3CrudPolicy: - BucketName: !Ref DestinationBucket \ No newline at end of file + BucketName: !Ref DestinationBucket + Metadata: + BuildMethod: rust-cargolambda + From 7189bcd6f17a1afa7bd747e6026cab17fa0f79c6 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 14:37:51 +0200 Subject: [PATCH 15/21] lambda-sfn-rust --- lambda-sfn-rust/Cargo.toml | 2 +- lambda-sfn-rust/Makefile | 29 ----------------------------- lambda-sfn-rust/README.md | 8 ++++---- lambda-sfn-rust/template.yml | 7 +++++-- 4 files changed, 10 insertions(+), 36 deletions(-) delete mode 100644 lambda-sfn-rust/Makefile diff --git a/lambda-sfn-rust/Cargo.toml b/lambda-sfn-rust/Cargo.toml index c0a961397..6d056690b 100644 --- a/lambda-sfn-rust/Cargo.toml +++ b/lambda-sfn-rust/Cargo.toml @@ -11,7 +11,7 @@ path = "src/bin/handler.rs" [dependencies] aws-config = {version = "1", features = ["behavior-version-latest"] } aws-sdk-sfn = "1" -lambda_runtime = "0.11" +lambda_runtime = "0.13" serde = {version = "1.0", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["macros"] } diff --git a/lambda-sfn-rust/Makefile b/lambda-sfn-rust/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/lambda-sfn-rust/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/lambda-sfn-rust/README.md b/lambda-sfn-rust/README.md index b85a94d55..54a4fae27 100644 --- a/lambda-sfn-rust/README.md +++ b/lambda-sfn-rust/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -27,11 +27,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name @@ -64,4 +64,4 @@ response.json --cli-binary-format raw-in-base64-out ---- Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. -SPDX-License-Identifier: MIT-0 \ No newline at end of file +SPDX-License-Identifier: MIT-0 diff --git a/lambda-sfn-rust/template.yml b/lambda-sfn-rust/template.yml index ebbc62303..77cd1682f 100644 --- a/lambda-sfn-rust/template.yml +++ b/lambda-sfn-rust/template.yml @@ -23,13 +23,16 @@ Resources: LambdaProxy: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Policies: - StepFunctionsExecutionPolicy: StateMachineName: !GetAtt StateMachine.Name Environment: Variables: STATE_MACHINE_ARN: !GetAtt StateMachine.Arn + Metadata: + BuildMethod: rust-cargolambda + ########################################################################## # STEP FUNCTION # ########################################################################## @@ -68,4 +71,4 @@ Outputs: LambdaFuncton: Value: !Ref LambdaProxy - Description: Lambda Function Name \ No newline at end of file + Description: Lambda Function Name From 72645ec85c1dad2e025d90a49c00fb65378ec15b Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 14:38:20 +0200 Subject: [PATCH 16/21] lambda-sqs-rust-sam --- lambda-sqs-rust-sam/Cargo.toml | 2 +- lambda-sqs-rust-sam/Makefile | 29 ----------------------------- lambda-sqs-rust-sam/README.md | 8 ++++---- lambda-sqs-rust-sam/template.yml | 7 +++++-- 4 files changed, 10 insertions(+), 36 deletions(-) delete mode 100644 lambda-sqs-rust-sam/Makefile diff --git a/lambda-sqs-rust-sam/Cargo.toml b/lambda-sqs-rust-sam/Cargo.toml index bcfb90fb4..241f396d7 100644 --- a/lambda-sqs-rust-sam/Cargo.toml +++ b/lambda-sqs-rust-sam/Cargo.toml @@ -12,7 +12,7 @@ path = "src/bin/handler.rs" [dependencies] aws-config = {version = "1", features = ["behavior-version-latest"] } aws-sdk-sqs = "1" -lambda_runtime = "0.11" +lambda_runtime = "0.13" serde_json = "1" tokio = { version = "1", features = ["macros"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } diff --git a/lambda-sqs-rust-sam/Makefile b/lambda-sqs-rust-sam/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/lambda-sqs-rust-sam/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/lambda-sqs-rust-sam/README.md b/lambda-sqs-rust-sam/README.md index 4b90644b5..a1497fdb5 100644 --- a/lambda-sqs-rust-sam/README.md +++ b/lambda-sqs-rust-sam/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -27,11 +27,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name @@ -76,7 +76,7 @@ aws sqs receive-message --queue-url ENTER_YOUR_QUEUE_URL 1. Delete the stack ```bash - make delete + sam delete ``` ---- Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. diff --git a/lambda-sqs-rust-sam/template.yml b/lambda-sqs-rust-sam/template.yml index 120c24521..44f28870b 100644 --- a/lambda-sqs-rust-sam/template.yml +++ b/lambda-sqs-rust-sam/template.yml @@ -27,7 +27,7 @@ Resources: QueuePublisherFunction: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Environment: Variables: SQS_QUEUE_NAME: !Ref MySqsQueue @@ -36,6 +36,9 @@ Resources: ## https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html - SQSSendMessagePolicy: QueueName: !GetAtt MySqsQueue.QueueName + Metadata: + BuildMethod: rust-cargolambda + Outputs: QueuePublisherFunction: @@ -52,4 +55,4 @@ Outputs: SQSqueueURL: Description: SQS queue URL - Value: !Ref MySqsQueue \ No newline at end of file + Value: !Ref MySqsQueue From c6827226aeb9cbff687b55c74db7aed510f0d698 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 15:36:05 +0200 Subject: [PATCH 17/21] s3-eventbridge-rust-sam --- s3-eventbridge-rust-sam/Cargo.toml | 5 +-- s3-eventbridge-rust-sam/Makefile | 29 --------------- s3-eventbridge-rust-sam/README.md | 8 ++--- s3-eventbridge-rust-sam/src/bin/handler.rs | 41 ++++++++++------------ s3-eventbridge-rust-sam/template.yml | 5 ++- 5 files changed, 29 insertions(+), 59 deletions(-) delete mode 100644 s3-eventbridge-rust-sam/Makefile diff --git a/s3-eventbridge-rust-sam/Cargo.toml b/s3-eventbridge-rust-sam/Cargo.toml index 2e674116b..afe82f930 100644 --- a/s3-eventbridge-rust-sam/Cargo.toml +++ b/s3-eventbridge-rust-sam/Cargo.toml @@ -12,8 +12,9 @@ path = "src/bin/handler.rs" [dependencies] aws-config = {version = "1", features = ["behavior-version-latest"] } aws-sdk-s3 = "1" -lambda_runtime = "0.11" +lambda_runtime = "0.13" serde_json = "1" serde = {version = "1.0", features = ["derive"] } -tokio = { version = "1", features = ["macros"] } +tokio = { version = "1", features = ["full"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } +aws_lambda_events = { version = "0.15", default-features = false, features = ["s3"] } diff --git a/s3-eventbridge-rust-sam/Makefile b/s3-eventbridge-rust-sam/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/s3-eventbridge-rust-sam/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/s3-eventbridge-rust-sam/README.md b/s3-eventbridge-rust-sam/README.md index 31bef6f82..bd0c7ba3e 100644 --- a/s3-eventbridge-rust-sam/README.md +++ b/s3-eventbridge-rust-sam/README.md @@ -15,7 +15,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -29,11 +29,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name @@ -95,7 +95,7 @@ aws s3 cp './events/example.jpg' s3://*SourceBucketName* 2. Run the following command to check to get the logs from the deployed Lambda function (use the function name from the stack output): ```bash -sam logs -n *FunctionName* --region *YourRegion* +aws logs filter-log-events --log-group-name /aws/lambda/*FunctionName* --region *YourRegion* ``` ## Cleanup diff --git a/s3-eventbridge-rust-sam/src/bin/handler.rs b/s3-eventbridge-rust-sam/src/bin/handler.rs index d2efb2519..05fe70547 100644 --- a/s3-eventbridge-rust-sam/src/bin/handler.rs +++ b/s3-eventbridge-rust-sam/src/bin/handler.rs @@ -1,5 +1,5 @@ +use aws_lambda_events::s3::S3Entity; use lambda_runtime::{run, service_fn, Error, LambdaEvent}; -use serde::{Deserialize, Serialize}; #[tokio::main] async fn main() -> Result<(), Error> { @@ -12,7 +12,7 @@ async fn main() -> Result<(), Error> { let config = aws_config::load_from_env().await; let s3_client = aws_sdk_s3::Client::new(&config); - run(service_fn(|event: LambdaEvent| { + run(service_fn(|event: LambdaEvent| { function_handler(&s3_client, event) })) .await @@ -20,14 +20,27 @@ async fn main() -> Result<(), Error> { pub async fn function_handler( client: &aws_sdk_s3::Client, - event: LambdaEvent, + event: LambdaEvent, ) -> Result<(), Error> { println!("{:?}", event); + let bucket_name = event + .payload + .bucket + .name + .as_ref() + .ok_or("Bucket name is missing")?; + let object_key = event + .payload + .object + .key + .as_ref() + .ok_or("Object key is missing")?; + let resp = client .get_object() - .bucket(event.payload.bucket.name) - .key(event.payload.object.key) + .bucket(bucket_name) + .key(object_key) .send() .await?; @@ -36,21 +49,3 @@ pub async fn function_handler( Ok(()) } - -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -pub struct S3Event { - pub bucket: S3Bucket, - pub object: S3Object, - pub reason: String, -} - -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -pub struct S3Bucket { - pub name: String, -} - -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -pub struct S3Object { - pub key: String, - pub size: i64, -} diff --git a/s3-eventbridge-rust-sam/template.yml b/s3-eventbridge-rust-sam/template.yml index d8a77e1bf..ac3428828 100644 --- a/s3-eventbridge-rust-sam/template.yml +++ b/s3-eventbridge-rust-sam/template.yml @@ -48,7 +48,7 @@ Resources: MyFunction: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Policies: - S3ReadPolicy: BucketName: !Ref SourceBucket @@ -60,6 +60,9 @@ Resources: Pattern: source: - "aws.s3" + Metadata: + BuildMethod: rust-cargolambda + Outputs: SourceBucketName: From 733658bb5b5e01c240db9826a9992c78b8e9f0ae Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 15:40:05 +0200 Subject: [PATCH 18/21] remove custom struct for lambda-rekognition-rust --- lambda-rekognition-rust/Cargo.toml | 1 + lambda-rekognition-rust/README.md | 2 +- lambda-rekognition-rust/src/bin/handler.rs | 41 ++++++++++------------ 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/lambda-rekognition-rust/Cargo.toml b/lambda-rekognition-rust/Cargo.toml index 7a9a4f16b..568071e25 100644 --- a/lambda-rekognition-rust/Cargo.toml +++ b/lambda-rekognition-rust/Cargo.toml @@ -16,3 +16,4 @@ serde = {version = "1.0", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["macros"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } +aws_lambda_events = { version = "0.15", default-features = false, features = ["s3"] } diff --git a/lambda-rekognition-rust/README.md b/lambda-rekognition-rust/README.md index e43d28552..9efe7c3ac 100644 --- a/lambda-rekognition-rust/README.md +++ b/lambda-rekognition-rust/README.md @@ -72,7 +72,7 @@ aws s3 cp './image/helloworld1.png' s3://*SourceBucketName* 2. Run the following command to check to get the logs from the deployed Lambda function (use the function name from the stack output): ```bash -sam logs -n *FunctionName* --region *YourRegion* +aws logs filter-log-events --log-group-name /aws/lambda/*FunctionName* --region *YourRegion* ``` below are snippets from the logs diff --git a/lambda-rekognition-rust/src/bin/handler.rs b/lambda-rekognition-rust/src/bin/handler.rs index ebeeb6476..b33ba1252 100644 --- a/lambda-rekognition-rust/src/bin/handler.rs +++ b/lambda-rekognition-rust/src/bin/handler.rs @@ -1,6 +1,6 @@ +use aws_lambda_events::s3::S3Entity; use aws_sdk_rekognition::types::Image; use lambda_runtime::{run, service_fn, Error, LambdaEvent}; -use serde::{Deserialize, Serialize}; #[tokio::main] async fn main() -> Result<(), Error> { @@ -13,7 +13,7 @@ async fn main() -> Result<(), Error> { let config = aws_config::load_from_env().await; let rekognition_client = aws_sdk_rekognition::Client::new(&config); - run(service_fn(|event: LambdaEvent| { + run(service_fn(|event: LambdaEvent| { function_handler(&rekognition_client, event) })) .await @@ -21,13 +21,26 @@ async fn main() -> Result<(), Error> { pub async fn function_handler( client: &aws_sdk_rekognition::Client, - event: LambdaEvent, + event: LambdaEvent, ) -> Result<(), Error> { println!("{:?}", event); + let bucket_name = event + .payload + .bucket + .name + .as_ref() + .ok_or("Bucket name is missing")?; + let object_key = event + .payload + .object + .key + .as_ref() + .ok_or("Object key is missing")?; + let s3_object = aws_sdk_rekognition::types::S3Object::builder() - .bucket(event.payload.bucket.name) - .name(event.payload.object.key) + .bucket(bucket_name) + .name(object_key) .build(); let params = Image::builder().s3_object(s3_object).build(); @@ -46,21 +59,3 @@ pub async fn function_handler( Ok(()) } - -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -pub struct S3Event { - pub bucket: S3Bucket, - pub object: S3Object, - pub reason: String, -} - -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -pub struct S3Bucket { - pub name: String, -} - -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -pub struct S3Object { - pub key: String, - pub size: i64, -} From d6486d372c9ac47b3514e61a8eea441dd5f63f7b Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 15:42:44 +0200 Subject: [PATCH 19/21] sns-sqs-lambda-rust-sam --- sns-sqs-lambda-rust-sam/Cargo.toml | 4 ++-- sns-sqs-lambda-rust-sam/Makefile | 29 ---------------------------- sns-sqs-lambda-rust-sam/README.md | 6 +++--- sns-sqs-lambda-rust-sam/template.yml | 6 ++++-- 4 files changed, 9 insertions(+), 36 deletions(-) delete mode 100644 sns-sqs-lambda-rust-sam/Makefile diff --git a/sns-sqs-lambda-rust-sam/Cargo.toml b/sns-sqs-lambda-rust-sam/Cargo.toml index 2fe77dcdd..f2cdf4787 100644 --- a/sns-sqs-lambda-rust-sam/Cargo.toml +++ b/sns-sqs-lambda-rust-sam/Cargo.toml @@ -10,9 +10,9 @@ name = "handler" path = "src/bin/handler.rs" [dependencies] -aws_lambda_events = { version = "0.7", default-features = false, features = ["sqs", "sns"] } +aws_lambda_events = { version = "0.15", default-features = false, features = ["sqs", "sns"] } futures = "0.3" -lambda_runtime = "0.11" +lambda_runtime = "0.13" serde = {version = "1", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["macros"] } diff --git a/sns-sqs-lambda-rust-sam/Makefile b/sns-sqs-lambda-rust-sam/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/sns-sqs-lambda-rust-sam/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/sns-sqs-lambda-rust-sam/README.md b/sns-sqs-lambda-rust-sam/README.md index 2e975099d..79f07af58 100644 --- a/sns-sqs-lambda-rust-sam/README.md +++ b/sns-sqs-lambda-rust-sam/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) * Make sure to run "rustup target add aarch64-unknown-linux-gnu;" ## Deployment Instructions @@ -28,11 +28,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name diff --git a/sns-sqs-lambda-rust-sam/template.yml b/sns-sqs-lambda-rust-sam/template.yml index d2dab7762..f5fa3debf 100644 --- a/sns-sqs-lambda-rust-sam/template.yml +++ b/sns-sqs-lambda-rust-sam/template.yml @@ -57,13 +57,16 @@ Resources: MyFunction: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Events: MySQSEvent: Type: SQS Properties: Queue: !GetAtt MySqsQueue.Arn BatchSize: 10 + Metadata: + BuildMethod: rust-cargolambda + Outputs: MySqsQueueName: @@ -81,4 +84,3 @@ Outputs: MySnsTopicArn: Description: SNS topic ARN Value: !Ref MySnsTopic - From b1842571edc139f359d10d3dad6a7ffe8a82f0b0 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 15:43:21 +0200 Subject: [PATCH 20/21] sqs-lambda-rust-sam --- sqs-lambda-rust-sam/Cargo.toml | 4 ++-- sqs-lambda-rust-sam/Makefile | 29 ----------------------------- sqs-lambda-rust-sam/README.md | 6 +++--- sqs-lambda-rust-sam/template.yml | 6 ++++-- 4 files changed, 9 insertions(+), 36 deletions(-) delete mode 100644 sqs-lambda-rust-sam/Makefile diff --git a/sqs-lambda-rust-sam/Cargo.toml b/sqs-lambda-rust-sam/Cargo.toml index cefff91bf..07cb2db9a 100644 --- a/sqs-lambda-rust-sam/Cargo.toml +++ b/sqs-lambda-rust-sam/Cargo.toml @@ -10,7 +10,7 @@ name = "handler" path = "src/bin/handler.rs" [dependencies] -aws_lambda_events = { version = "0.7", default-features = false, features = ["sqs"] } -lambda_runtime = "0.11" +aws_lambda_events = { version = "0.15", default-features = false, features = ["sqs"] } +lambda_runtime = "0.13" tokio = { version = "1", features = ["macros"] } tracing-subscriber = { version = "0.3", features = ["fmt", "json"] } diff --git a/sqs-lambda-rust-sam/Makefile b/sqs-lambda-rust-sam/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/sqs-lambda-rust-sam/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/sqs-lambda-rust-sam/README.md b/sqs-lambda-rust-sam/README.md index d8dfe357b..9920eefd6 100644 --- a/sqs-lambda-rust-sam/README.md +++ b/sqs-lambda-rust-sam/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) ## Deployment Instructions @@ -27,11 +27,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name diff --git a/sqs-lambda-rust-sam/template.yml b/sqs-lambda-rust-sam/template.yml index 4687fc2a6..42e44fd10 100644 --- a/sqs-lambda-rust-sam/template.yml +++ b/sqs-lambda-rust-sam/template.yml @@ -27,13 +27,15 @@ Resources: QueueConsumerFunction: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Events: MySQSEvent: Type: SQS Properties: Queue: !GetAtt MySqsQueue.Arn BatchSize: 10 + Metadata: + BuildMethod: rust-cargolambda Outputs: QueueConsumerFunction: @@ -50,4 +52,4 @@ Outputs: SQSqueueURL: Description: SQS queue URL - Value: !Ref MySqsQueue \ No newline at end of file + Value: !Ref MySqsQueue From b487ae61b0386260bde54104b5bb4dd1f5394387 Mon Sep 17 00:00:00 2001 From: Daniele Frasca Date: Sat, 12 Oct 2024 16:06:51 +0200 Subject: [PATCH 21/21] sqs-lambda-partial-batch-rust-sam --- sqs-lambda-partial-batch-rust-sam/Cargo.toml | 4 +- sqs-lambda-partial-batch-rust-sam/Makefile | 29 ---------- sqs-lambda-partial-batch-rust-sam/README.md | 8 +-- .../src/bin/handler.rs | 57 ++++++------------- .../template.yml | 5 +- 5 files changed, 26 insertions(+), 77 deletions(-) delete mode 100644 sqs-lambda-partial-batch-rust-sam/Makefile diff --git a/sqs-lambda-partial-batch-rust-sam/Cargo.toml b/sqs-lambda-partial-batch-rust-sam/Cargo.toml index 02bca8eda..3bae5feec 100644 --- a/sqs-lambda-partial-batch-rust-sam/Cargo.toml +++ b/sqs-lambda-partial-batch-rust-sam/Cargo.toml @@ -10,9 +10,9 @@ name = "handler" path = "src/bin/handler.rs" [dependencies] -aws_lambda_events = { version = "0.7", default-features = false, features = ["sqs"] } +aws_lambda_events = { version = "0.15", default-features = false, features = ["sqs"] } futures = "0.3" -lambda_runtime = "0.11" +lambda_runtime = "0.13" serde = {version = "1", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["macros"] } diff --git a/sqs-lambda-partial-batch-rust-sam/Makefile b/sqs-lambda-partial-batch-rust-sam/Makefile deleted file mode 100644 index dfb2adea7..000000000 --- a/sqs-lambda-partial-batch-rust-sam/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -FUNCTIONS := handler -ARCH := aarch64-unknown-linux-gnu -ARCH_SPLIT = $(subst -, ,$(ARCH)) - -build: -ifeq ("$(shell zig targets | jq -r .native.cpu.arch)-$(shell zig targets | jq -r .native.os)-$(shell zig targets | jq -r .native.abi)", "$(word 1,$(ARCH_SPLIT))-$(word 3,$(ARCH_SPLIT))-$(word 4,$(ARCH_SPLIT))") - @echo "Same host and target. Using native build" - cargo build --release --target $(ARCH) -else - @echo "Different host and target. Using zigbuild" - cargo zigbuild --release --target $(ARCH) -endif - - rm -rf ./build - mkdir -p ./build - ${MAKE} ${MAKEOPTS} $(foreach function,${FUNCTIONS}, build-${function}) - -build-%: - mkdir -p ./build/$* - cp -v ./target/$(ARCH)/release/$* ./build/$*/bootstrap - -deploy: - if [ -f samconfig.toml ]; \ - then sam deploy; \ - else sam deploy -g; \ - fi - -delete: - sam delete \ No newline at end of file diff --git a/sqs-lambda-partial-batch-rust-sam/README.md b/sqs-lambda-partial-batch-rust-sam/README.md index 7a739d31a..d32d87412 100644 --- a/sqs-lambda-partial-batch-rust-sam/README.md +++ b/sqs-lambda-partial-batch-rust-sam/README.md @@ -13,7 +13,7 @@ Important: this application uses various AWS services and there are costs associ * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed * [Rust](https://www.rust-lang.org/) 1.56.0 or higher -* [cargo-zigbuild](https://github.com/messense/cargo-zigbuild) and [Zig](https://ziglang.org/) for cross-compilation +* [CargoLambda](https://www.cargo-lambda.info/guide/installation.html) * Make sure to run "rustup target add aarch64-unknown-linux-gnu;" ## Deployment Instructions @@ -28,11 +28,11 @@ Important: this application uses various AWS services and there are costs associ ``` 3. Install dependencies and build: ``` - make build + sam build ``` 4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` - make deploy + sam deploy ``` 5. During the prompts: * Enter a stack name @@ -117,7 +117,7 @@ do_something MyStruct { name: "Daniele", surname: "Frasca" } 1. Delete the stack ```bash - make delete + sam delete ``` 2. Confirm the stack has been deleted ```bash diff --git a/sqs-lambda-partial-batch-rust-sam/src/bin/handler.rs b/sqs-lambda-partial-batch-rust-sam/src/bin/handler.rs index ff80511b0..d54f8190d 100644 --- a/sqs-lambda-partial-batch-rust-sam/src/bin/handler.rs +++ b/sqs-lambda-partial-batch-rust-sam/src/bin/handler.rs @@ -1,8 +1,10 @@ -use aws_lambda_events::event::sqs::SqsEvent; +use aws_lambda_events::{ + event::sqs::SqsEvent, + sqs::{BatchItemFailure, SqsBatchResponse}, +}; use futures::future::join_all; use lambda_runtime::{run, service_fn, Error, LambdaEvent}; -use serde::{Deserialize, Serialize}; -use serde_json::Value; +use serde::Deserialize; use std::sync::{Arc, Mutex}; #[tokio::main] @@ -19,32 +21,29 @@ async fn main() -> Result<(), Error> { .await } -pub async fn function_handler(event: LambdaEvent) -> Result { +pub async fn function_handler(event: LambdaEvent) -> Result { println!("Input {:?}", event); - let failed_message: Arc>> = Arc::new(Mutex::new(Vec::new())); + let failed_message = Arc::new(Mutex::new(Vec::with_capacity(event.payload.records.len()))); let mut tasks = Vec::with_capacity(event.payload.records.len()); for record in event.payload.records.into_iter() { let failed_message = failed_message.clone(); - tasks.push(tokio::spawn(async move { if let Some(body) = &record.body { let request = serde_json::from_str::(body); if let Ok(request) = request { do_something(&request).await.map_or_else( |_e| { - failed_message - .lock() - .unwrap() - .push(record.message_id.unwrap().clone()); + failed_message.lock().unwrap().push(BatchItemFailure { + item_identifier: record.message_id.unwrap_or_default(), + }); }, |_| (), ); } else { - failed_message - .lock() - .unwrap() - .push(record.message_id.unwrap().clone()); + failed_message.lock().unwrap().push(BatchItemFailure { + item_identifier: record.message_id.unwrap_or_default(), + }); } } })); @@ -52,21 +51,11 @@ pub async fn function_handler(event: LambdaEvent) -> Result Result<(), Error> { @@ -79,15 +68,3 @@ pub struct MyStruct { pub name: String, pub surname: String, } - -#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)] -pub struct BatchItemFailures { - #[serde(rename = "batchItemFailures")] - pub batch_item_failures: Vec, -} - -#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)] -pub struct ItemIdentifier { - #[serde(rename = "itemIdentifier")] - pub item_identifier: String, -} diff --git a/sqs-lambda-partial-batch-rust-sam/template.yml b/sqs-lambda-partial-batch-rust-sam/template.yml index 837059760..6b9cbd38d 100644 --- a/sqs-lambda-partial-batch-rust-sam/template.yml +++ b/sqs-lambda-partial-batch-rust-sam/template.yml @@ -34,7 +34,7 @@ Resources: MyFunction: Type: AWS::Serverless::Function Properties: - CodeUri: ./build/handler + CodeUri: . Events: MySQSEvent: Type: SQS @@ -43,6 +43,8 @@ Resources: BatchSize: 10 FunctionResponseTypes: - ReportBatchItemFailures + Metadata: + BuildMethod: rust-cargolambda Outputs: MySqsQueueName: @@ -54,4 +56,3 @@ Outputs: MySqsQueueURL: Description: SQS queue URL Value: !Ref MySqsQueue -