Learn How to Easily Integrate Lambda Annotations and Other AWS Services

Created on November 12, 2023 at 11:19 am

Learn how to use the Lambda Annotations Framework LAW to integrate with other AWS Services ORG like S3 ORG , SQS ORG , SNS ORG , DynamoDB etc. In this blog post we will see an example using S3 ORG .

The Lambda Annotations Framework WORK_OF_ART is a programming model that makes it easier to build AWS Lambda Functions ORG using .NET.

In previous blog posts, we learned how to get started using the Lambda Annotations Framework LAW , build a CRUD API Endpoint PRODUCT , and set up Dependency Injection when creating Lambda Functions ORG using the Annotations Framework.

In all the above scenarios we used the Annotations Framework LAW along with building Lambda Functions ORG for the API Gateway.

However, you can also use the Annotations Framework LAW when building Lambda Functions ORG that integrate with other AWS Services ORG .

In this blog post, let’s see a quick example of using the Lambda Annotations Framework LAW when building a Lambda Function ORG with Amazon ORG

S3 PRODUCT service.

For E.g., we need to run a Lambda Function ORG every time a new file is uploaded to an Amazon ORG

S3 PRODUCT bucket and have the function code read the file and process it. This could be an image post-processing Lambda Function ORG , a report ingestion service, a bulk data upload service, etc.

This article is sponsored by AWS ORG and is part of my AWS Series.

AWS Lambda ORG and Amazon S3 Integration ORG

Amazon Simple Storage Service ORG ( S3 ORG ) is an object storage service that allows you to store any data.

Any time an object is created or modified in S3 ORG , it raises event notifications. We can use these notifications in specific scenarios to perform additional business logic or application processing.

To learn more about this in detail check my Amazon ORG

S3 PRODUCT and AWS Lambda Triggers ORG in .NET, blog post.

Lambda Functions that integrate with Amazon ORG

S3 PRODUCT services, take in a S3Event class type, to bind to the Amazon ORG

S3 PRODUCT event.

public async Task LAW FunctionHandler(S3Event evnt, ILambdaContext context) { … var file = await this.S3Client .GetObjectAsync(s3Event.Bucket.Name, s3Event.Object.Key); … }

This happens by default using the basic Lambda JSON Serializer ORG and there is nothing specific that the Annotations Framework LAW can provide us here.

However, the features that are useful from the Annotations Framework LAW when integrating with other AWS Services ORG are Dependency Injection and Serverless Template for easy deployment.

Dependency Injection For Lambda Functions

Lambda Annotations Framework provides an easy out-of-the-box mechanism to set up and work with .NET Dependency Injection framework.

Instead of hard-coding the instance creation in the Function ORG constructor, you can inject in the dependencies that the Function ORG needs using the Function ORG constructor or the Handler Function ORG .

To enable the Annotations Framework LAW we need to apply the LambdaFunction FAC attribute to the Function Handler ORG as shown below.

The below code shows examples of injecting the IAmazonS3 ORG instance both using the constructor and also using the Function Handler ORG .

public Function(IAmazonS3 ORG s3Client) { S3Client = s3Client; } [LambdaFunction(ResourceName = "MyLambdaFunction")] public async Task FunctionHandler PERSON ( [FromServices] IImageServices imageServices, S3Event evnt, ILambdaContext context) { … }

To inject via the FunctionHandler ORG method, we need to add FromServices PRODUCT attribute, to tell Annotations Framework to resolve the type from the DI PRODUCT container.

[ Amazon ORG .Lambda.Annotations. LambdaStartup PERSON ] public class Startup PRODUCT { public void ConfigureServices(IServiceCollection services) { services. AddAWSService ORG < Amazon ORG .S3.IAmazonS3>(); } }

Serverless Template File For Deployment

Enable the Annotations Framework also generates the CloudFormation ORG template file, which can be used to automate resource deployment.

In this scenario, since we are integrating with Amazon S3 ORG , we can further define the S3 ORG bucket and also the required triggers to wire up the S3 ORG event notification to trigger the Lambda Function LAW .

"Bucket": { "Type": " AWS::S3::Bucket PERSON ", " Properties WORK_OF_ART ": { " BucketName WORK_OF_ART ": { "Ref": " BucketName WORK_OF_ART " }, "NotificationConfiguration": { "LambdaConfigurations": [ { "Event": "s3:ObjectCreated:*", "Filter": { "S3Key": { "Rules": [ { "Name": "prefix", "Value": "test/" }, { "Name": "suffix", "Value": ".txt" } ] } }, "Function": { " Fn::GetAtt WORK_OF_ART ": [ "MyLambdaFunction", "Arn" ] } } ] } } }, " S3InvokeLambdaPermission FAC ": { "Type": "AWS::Lambda::Permission", " Properties WORK_OF_ART ": { "Action": "lambda: InvokeFunction ORG ", " FunctionName WORK_OF_ART ": { "Ref": "MyLambdaFunction" }, "Principal": "s3.amazonaws.com", " SourceArn ORG ": { "Fn::Sub": " arn TIME :aws:s3:::${BucketName}" } } }, " LambdaRole PRODUCT ": { "Type": " AWS::IAM::Role PRODUCT ", " Properties WORK_OF_ART ": { "AssumeRolePolicyDocument": { "Version": " 2012-10-17 DATE ", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": [ "sts:AssumeRole" ] } ] }, "Path": "/", "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" ], "Policies": [ { "PolicyName": "s3", "PolicyDocument": { "Statement": [ { "Effect": "Allow", "Action": [ "s3:Get*" ], "Resource": [ { "Fn::Sub": " arn TIME :aws:s3:::${BucketName}" }, { "Fn::Sub": " arn TIME :aws:s3:::${BucketName}/*" } ] } ] } } ] } }

The above template is in addition to the automatically generated template for the Lambda Function ORG .

It sets up the S3 ORG bucket ( Bucket ), the Lambda IAM Role ORG ( LambdaRole PRODUCT ) giving it permission to read from the S3 ORG bucket and also permission for the S3 ORG bucket to invoke the Lambda Function LAW ( S3InvokeLambdaPermission ).

This makes it easy to deploy the required resources and permissions together along with changes to the Lambda Function ORG .

Similar to Amazon ORG

S3 PRODUCT , you can also use the same approach to integrate with other AWS Services ORG like SQS ORG , SNS ORG , DynamoDB, etc.

Connecting to blog.lzomedia.com... Connected... Page load complete