Skip to main content

Using the AWS CLI to Create an AWS Lambda Function

Hands-On Lab

 

Photo of

Training Architect

Length

00:30:00

Difficulty

Intermediate

Welcome to this Learning Activity for creating a Lambda function using the AWS CLI This activity provides some hands-on experience with creating and customizing Lambda functions, all from within the CLI. The primary focus will be on these AWS features: 1. Lambda 2. AWS CLI 3. CloudWatch Function Log Streams AWS Lambda allows you to create functions and only have to worry about managing your code. Using the CLI allows you to "ditch the clicks," all of those mouse gestures required when you create a function within the console. CLI FTW!

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using the AWS CLI to Create an AWS Lambda Function

Introduction

This hands-on lab provides experience with creating and customizing Lambda functions, all from within the CLI. The primary focus will be on Lambda, AWS CLI, and CloudWatch log streams. AWS Lambda allows you to create functions and only have to worry about managing your code. Using the CLI allows you to "ditch the clicks" — all those mouse gestures required when you create a function within the console.

Solution

Log in to the live AWS environment using the credentials provided. Make sure you're in the N. Virginia (us-east-1) region throughout the lab.

All code used in the lesson is available to download on GitHub. You do not need to rewrite it.

Verify Lab Resources and Configure AWS CLI

  1. Navigate to S3, and verify at least two buckets are listed.

  2. Navigate to IAM > Roles.

  3. Click the listed lambda_exec_role_LA.

  4. Copy its Role ARN and paste it into a text file, as we'll need this later in the lab.

  5. Navigate to EC2 > Instances.

  6. Verify that the listed instance's public IP matches the public IP of the public instance provided on the lab page.

  7. Open a terminal session.

  8. Log in to the provided instance via SSH:

    ssh cloud_user@<PUBLIC IP>
  9. Make sure the AWS CLI works:

    aws help

    We should then see a man page, which means the AWS CLI is installed.

  10. Exit out of it by entering q.

  11. View the Lambda-specific man pages:

    aws lambda help
  12. Exit out of it by entering q.

  13. List all Lambda functions in the region:

    aws lambda list-functions --region us-east-1

    We should see an empty list.

Create a Lambda Function Using the AWS CLI

  1. Create the file:

    vim lambda_function.js
  2. Enter the following (also found in the lambda_function.js file on GitHub):

    // Here is where we load the SDK for JavaScript
    const AWS = require('aws-sdk');
    
    // We need to set the region.
    AWS.config.update({region: 'us-east-1'});
    
    // Creating S3 service object
    const s3 = new AWS.S3({apiVersion: '2006-03-01'});
    
    exports.handler = (event, context, callback) => {
        // Here we list all S3 Buckets
        s3.listBuckets(function(err, data) {
           if (err) {
              console.log("Error:", err);
           } else {
              console.log("List of all S3 Buckets", data.Buckets);
           }
        });
    };
  3. Write and quit the file.

  4. Verify it's there:

    ls
  5. Zip the file:

    zip lambda_function.zip lambda_function.js
  6. Verify they both exist:

    ll
  7. Create the Lambda function (replacing <ROLE ARN> with the IAM role ARN you copied in the AWS console earlier):

    aws lambda create-function 
    --region us-east-1 
    --function-name "ListS3Buckets" 
    --runtime "nodejs8.10" 
    --role "<ROLE ARN>" 
    --handler "lambda_function.handler" 
    --zip-file fileb:///home/cloud_user/lambda_function.zip
  8. In the AWS console, navigate to Lambda > Functions. We should see the ListS3Buckets function we just created. Note its current description.

  9. Back in the terminal, update the function:

    aws lambda update-function-configuration 
    --region us-east-1 
    --function-name "ListS3Buckets" 
    --description "Creating our S3 function via CLI." 
    --timeout 5 
    --memory-size 256
  10. In the AWS console, refresh the Lambda functions page. We should see an updated description.

  11. Click the function to view its code.

  12. Scroll to the Basic settings section to verify it has 256 MB memory and a five-second timeout.

Invoke Your Function Using AWS CLI

  1. In the terminal, invoke the function:

    aws lambda invoke 
    --region us-east-1 
    --function-name "ListS3Buckets" OUTFILE.log
  2. Back in the AWS console, click the Monitoring tab for our function. We should see an invocation.

  3. Navigate to CloudWatch > Logs.

  4. Click the listed log group.

  5. Click the listed log stream. There, we should see the log activity.

  6. Expand the print statement. There, we should see that we're printing "List of all S3 Buckets," followed by our bucket names and their creation dates.

  7. In the terminal, check the output of the OUTFILE.log file:

    cat OUTFILE.log

    We should see null since we aren't returning any response object — we're only printing to the CloudWatch logs, which we see in the AWS console.

Conclusion

Congratulations on successfully completing this hands-on lab!