Working with AWS Lambda functions using the AWS Toolkit - AWS Cloud9

Working with AWS Lambda functions using the AWS Toolkit

The AWS Toolkit provides support for AWS Lambda functions. Using the AWS Toolkit, you can author code for Lambda functions that are part of serverless applications. In addition, you can invoke Lambda functions either locally or on AWS.

Lambda is a fully managed compute service that runs your code in response to events generated by custom code or from various AWS services, such as Amazon Simple Storage Service (Amazon S3), Amazon DynamoDB, Amazon Kinesis, Amazon Simple Notification Service (Amazon SNS), and Amazon Cognito.

The AWS Toolkit replaces the functionality provided by the Lambda Plugin in AWS Cloud9. To use the Lambda plugin, you must disable the AWS Toolkit. For more information, see Disabling AWS Toolkit.

Important

If you want to build a Lambda application that uses the resources provided by the Serverless Application Model (SAM), see Working with AWS serverless applications using the AWS Toolkit.

Invoking remote Lambda functions

Using the AWS Toolkit you can interact with AWS Lambda functions in various ways, as described later in this topic.

The AWS Toolkit replaces the functionality provided by the Lambda Plugin in AWS Cloud9. To use the Lambda plugin, you must disable the AWS Toolkit. For more information, see Disabling AWS Toolkit.

For more information about Lambda, see the AWS Lambda Developer Guide.

Note

If you have already created Lambda functions by using the AWS Management Console or in some other way, you can invoke them from the Toolkit. To create a new function with AWS Toolkit that you can deploy to AWS Lambda, you must first create a serverless application.

Prerequisites

  • Ensure that the credentials you configured in include appropriate read/write access to the AWS Lambda service. If in the AWS Explorer, under Lambda, you see a message similar to "Error loading Lambda resources", check the permissions attached to those credentials. Changes that you make to permissions will take a few minutes to affect the AWS Explorer in AWS Toolkit.

Invoking a Lambda function

Important

Calling API methods using the AWS Toolkit may result in changes to resources that can't be undone. For example, if you call a POST method, the API's resources are updated if the call is successful.

You can invoke a Lambda function on AWS using the AWS Toolkit.

  1. In the AWS Explorer, choose the name of the Lambda function you want to invoke, and then open its context menu.

  2. Choose Invoke on AWS.

  3. In the Invoke function window that opens, choose an option for the payload your Lambda function needs. (The payload is the JSON that you want to provide to your Lambda function as input.) You can choose Browse to select a file to use as payload or use the drop-down field to pick a template for the payload. In this case, the Lambda function might a string as an input, as shown in the text box.

Choose Invoke to call the Lambda and pass in the payload.

You'll see the output of the Lambda function in the AWS Lambda tab.

Importing, uploading, and deleting Lambda functions

The AWS Toolkit replaces the Lambda Plugin, which previously provided the options for importing and uploading Lambda functions in AWS Cloud9 IDE. To use the Lambda plugin, you must disable the AWS Toolkit. For more information, see Disabling AWS Toolkit.

Importing a Lambda function

Importing a Lambda function allows you to import the project files that describe the function and work with them in the AWS Cloud9 IDE.

To import a Lambda function

  1. In the AWS Explorer, under the Lambda node, right-click the function to import, and choose Import.

  2. When asked to Select a workspace folder for your new project, you can do one of the following:

    • Choose the folder that's suggested to create a subfolder with the same name as your Lambda project

    • Choose Select a different folder to open a dialog box to browse for and select a different parent folder for your project subfolder.

    The IDE opens a new editor window

Uploading a Lambda function

You can update existing Lambda functions with local code. Updating code in this way does not use the AWS Serverless Application Model CLI for deployment and does not create an AWS CloudFormation stack. This functionality can upload a Lambda function with any runtime supported by Lambda.

  1. In the AWS Explorer, open the context menu (right-click) the name of the Lambda function you want to import.

  2. Choose Upload Lambda...

  3. Choose from the three options for uploading your Lambda function.

    Upload a premade .zip archive

    1. Choose ZIP Archive from the menu.

    2. Choose a .zip file from your AWS Cloud9 file system and choose Open.

    3. Confirm the upload with the modal dialog. This uploads the .zip file and is immediately updates the Lambda following deployment.

    Upload a directory as is

    1. Choose Directory from the menu.

    2. Choose a directory from your AWS Cloud9 file system and choose Open.

    3. Choose No when prompted to build the directory.

    4. Confirm the upload with the modal dialog. This uploads the directory as is and immediately updates the Lambda following deployment.

    Build and upload a directory

    1. Choose Directory from the menu.

    2. Choose a directory from your AWS Cloud9 file system and choose Open.

    3. Choose Yes when prompted to build the directory.

    4. Confirm the upload with the modal dialog. This builds the code in the directory using the AWS SAM CLI sam build command and immediately updates the Lambda following deployment.

Deleting a Lambda function

You can also delete a Lambda function using the same context menu.

Warning

Do not use this procedure to delete Lambda functions that are associated with AWS CloudFormation (for example, the Lambda function that was created when creating a serverless application earlier in this guide). These functions must be deleted through the AWS CloudFormation stack.

  1. In the AWS Explorer, choose the name of the Lambda function you want to delete, and then open its context menu.

  2. Choose Delete.

  3. In the message that appears, choose Yes to confirm the delete.

After the function is deleted, it's no longer listed in the AWS Explorer view.