AWS Developer Blog

Working with Lambda Functions and Visual Studio Team Services

In previous posts, we talked about our new AWS Tools for Microsoft Visual Studio Team Services (VSTS), which provides AWS tasks you can add to your build definition. We also talked about our AWS Elastic Beanstalk task that you can use to deploy .NET web applications. In our initial release, there are also two tasks to interact with AWS Lambda.

Deployment .NET Core Lambda functions

To deploy Lambda .NET Core functions, we provide the AWS Lambda .NET Core Deployment task. This task uses our dotnet CLI integration, which means your Lambda project needs a DotNetCliToolReference reference to Amazon.Lambda.Tools. If you created your Lambda project from one of the templates provided by AWS, the reference is already set up. If the reference is not set, add the following to the Lambda project’s .csproj file.

<ItemGroup>
  <DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="1.7.0" />
</ItemGroup>

In addition to the usual credential and region properties, the task requires two parameters. The first is the Path to Lambda Project, which you should set to the file path of the .csproj file for the Lambda project. If you’re still using Visual Studio 2015, you can set this to the directory containing the project.json file

The other required parameter is the Command property. When you use the Lambda integration with the dotnet CLI there are two commands for deployment. The deploy-function command will package a single function and upload it to the Lambda service. The other command, deploy-serverless, packages a project that can contain a collection of functions and uses AWS CloudFormation to deploy the Lambda functions. The Command property controls whether you use deploy-function or deploy-serverless.

All the other parameters are optional because they could be set already in the project’s aws-lambda-tools-defaults.json file.That means if required parameters for the deploy-function or deploy-serverless commands are missing from both aws-lambda-tools-defaults.json and the VSTS build definition, the task will fail.

Note: We highly recommend that you update the version of Amazon.Lambda.Tools to version 1.7.0 and later. When you use the dotnet CLI integration and there are parameters missing from the command line, the integration will prompt for them. In the context of a VSTS build, prompting would obviously be inappropriate. Version 1.7.0 added the ability to suppress prompting for missing parameters.

Invoking the Lambda function

Once you have your Lambda function deployed, you can use the AWS Lambda Invoke Function to execute Lambda functions from your build definition. To invoke a function, you need to set the following properties:

  • Function Name – The name of the Lambda function.
  • Payload – Optional JSON document to pass to the Lambda function.
  • Invocation Type – Selector you use to decide if the function is called synchronously, which will return the results, or asynchronously.

Conclusion

By using these tasks, you can automate .NET Core Lambda deployment and trigger Lambda functions as part of your build to continue your build process through AWS. Hopefully, you will find these tasks useful. As always, we greatly appreciate your feedback and you can reach out to us on our VSTS GitHub repository.