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.

Downloading, 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.

Downloading a Lambda function

Downloading a Lambda function allows you to download the project files that describe the function from the AWS Cloud and work with them in the AWS Cloud9 IDE.

To download a Lambda function

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

  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

Configuring a downloaded Lambda function for running and debugging

To successfully run and debug your downloaded Lambda function as a serverless application, you need a launch configuration to be defined in your launch.json file. A Lambda function that was created in the AWS Management Console or in the deprecated AWS Resources window of AWS Cloud9 may not included in a launch configuration, so you need to add it manually.

To add your Lambda function to launch configuration

  1. After you've downloaded the Lambda function, open the Environment window to view its folders and files.

  2. Next, check that your Lambda function is included in a /home/ec2-user/.c9/launch.json file. If it isn't present, do the following to add a CodeLens link to your function's code:

    1. Open the source code file that defines the Lambda function (a .js or .py file, for example), and check if there's a CodeLens link that allows you to add your lambda function to a launch.json file. (A CodeLens appears above the function and includes the Add Debug Config link.)

    2. Choose Go (the magnifying glass icon) on the left of the IDE, and type "sam hint" to display the AWS: Toggle SAM hints in source files command. Choose the command to run it.

    3. Close your Lambda source code file and then reopen it.

    4. If the CodeLens is available in the source code after you reopen the file, choose Add Debug Config to add the launch configuration.

  3. If you're unable to add a CodeLens even after toggling the SAM hint option, do the following to add the launch configuration:

    1. Choose Go (the magnifying glass icon) on the left of the IDE, and type "config" to display the AWS: SAM Debug Configuration Editor command. Choose the command to run it.

    2. The SAM Debug Configuration Editor displays, which allows you to define launch configuration properties. For information, see the step for configuring launch properties in Using SAM templates to run and debug serverless applications.

      Note

      If your Lambda function doesn't have a template.yaml that's required for SAM applications, you'll need to add one. For more information, see Create your AWS SAM template.

    3. After you've finished entering the required config information in the editor, your launch configuration is added to the launch.json file.

After you've defined a launch configuration for your Lambda function, you can run it by doing the following:

  1. At the top of the IDE, choose the arrow beside Auto and select the relevant launch configuration.

  2. Next, choose Run.

Uploading a Lambda function

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

There are several interface options for uploading Lambda functions using the AWS Toolkit.

  1. In the Environment window for your project files, right-click the template.yaml for the Lambda application you want to upload and choose Upload Lambda.

    Alternatively, press Ctrl+P to open the Go to Anything pane and type "lambda" to access the AWS: Upload Lambda command. Then choose it to start the upload process.

  2. Next select an AWS Region that you want to upload to.

  3. Now choose an option 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.

    Upload a directory as is

    1. Choose Directory from the menu.

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

  4. Specify the Lambda function handler that processes events. When your function is invoked, Lambda runs this handler method.

    Note

    When selecting your Lambda function, you can select from the list that's displayed. If you don't know which function to choose, you can enter the ARN (Amazon Resource Number) of a Lambda function that's available in the Toolkit.

    A dialog displays asking whether you want this code to be published as the latest version of the Lambda function. Choose Yes to confirm publication.

  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.

Deploying a Lambda function for remote access

You can make your local functions available remotely by deploying them as serverless SAM applications.

To deploy a Lambda function as a SAM application

  1. In AWS Explorer, right-click the Lambda node, and choose Deploy SAM Application.

  2. In the command pane, select the YAML template that defines your function as a serverless application.

  3. Next, select an Amazon S3 bucket for the Lambda deployment. You can also choose to create a bucket for the deployment.

  4. Now enter the name of an AWS CloudFormation stack that you're deploying to. If you specify an existing stack, the command updates the stack. If you specify a new stack, the command creates it.

    After you enter the name of the stack, your Lambda function starts to deploy as a SAM application. After a successful deployment, the SAM Lambda application should be available remotely, allowing developers working in other AWS Cloud9 development environments to download or invoke it.

If you want to create a Lambda function from scratch, we recommend following the steps to Create a serverless application with the AWS Toolkit.

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.